Adobe Flex – getDefinitionByName failing with Error #1065
I was trying to instantiate a class using the class name as a string, like this:
var pluginInfoRef:Class = getDefinitionByName(“com.foo.bar.MyClassName”) as Class;
But kept getting this error:
ReferenceError: Error #1065: Variable MyClassName is not defined.
Which really has nothing to do with the problem since “MyClassName” is not a variable. I think the problem here is the SWF compiler in Flex Builder is optimizing out any imports that don’t get instantiated, so at run-time, the class really doesn’t exist in the SWF. The work around I came up with is to add this line:
private static const forceReference:MyClassName = null;
This causes the SWF compiler to pull the class into the SWF and getDefinitionByName started to work. Now, I’d like to have that 20 minutes back.