G
Guest
I have an application where...
1) I know the class name of the object I want to instantiate.
2) The class uses the class factory approach to create objects.
3) The factory method is a static member of the class. The name of this
method
is always "New" plus the class name.
So I can use reflection to create my object (in C#)...
// create the fully qualified name of the class
// Note dfla.AttributeType.BpaType is a string
string qualTypeName = "DescriptorLib." + dfla.AttributeType.BpaType;
// get the Type
Type valType = Type.GetType(qualTypeName);
// create the static method name to call... "New" plus the class name
string methodName = "New" + dfla.AttributeType.BpaType;
// invoke the static method - it takes no parms and returns a "valType" object
object val = valType.InvokeMember
(methodName,BindingFlags.InvokeMethod,null,null, new
object[] {});
All this works, but what I want is a "valType" object. How do I cast object
"val" to "valType"?
// d.att is a "valType" object attribute
d.att = val as valType; // Doesn't work. Compiler doesn't recognize valType
d.att = (valType)val; // Doesn't work. Compiler doesn't recognize valType
Any ideas anyone?
Thanks.
BBM
1) I know the class name of the object I want to instantiate.
2) The class uses the class factory approach to create objects.
3) The factory method is a static member of the class. The name of this
method
is always "New" plus the class name.
So I can use reflection to create my object (in C#)...
// create the fully qualified name of the class
// Note dfla.AttributeType.BpaType is a string
string qualTypeName = "DescriptorLib." + dfla.AttributeType.BpaType;
// get the Type
Type valType = Type.GetType(qualTypeName);
// create the static method name to call... "New" plus the class name
string methodName = "New" + dfla.AttributeType.BpaType;
// invoke the static method - it takes no parms and returns a "valType" object
object val = valType.InvokeMember
(methodName,BindingFlags.InvokeMethod,null,null, new
object[] {});
All this works, but what I want is a "valType" object. How do I cast object
"val" to "valType"?
// d.att is a "valType" object attribute
d.att = val as valType; // Doesn't work. Compiler doesn't recognize valType
d.att = (valType)val; // Doesn't work. Compiler doesn't recognize valType
Any ideas anyone?
Thanks.
BBM