|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using reflection to cast an object - how?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 BBM <b**@bbmcompany.com> wrote:
Show quote > I have an application where... What good would casting do if you don't know the type ahead of time? > > 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? Generally, the thing to do is cast to an interface that you know the type implements. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too why not just this?
(typeof(valType)) val = valType.InvokeMember (methodName,BindingFlags.InvokeMethod,null,null, new object[] {}); Show quote "BBM" <b**@bbmcompany.com> wrote in message news:A15C3620-54EE-4BFB-9C71-91C81ECCE865@microsoft.com... > > 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 Marcos Stefanakopolus <tarunt***@hotmail.com> wrote:
> why not just this? Because that won't compile either - (typeof(valType)) won't compile as > > (typeof(valType)) val = valType.InvokeMember > (methodName,BindingFlags.InvokeMethod,null,null, new > object[] {}); an expression because valType isn't the name of a type, and even if it did, the whole declaration wouldn't compile because "(typeof (valType))" isn't the name of a type, which is what is needed for a variable declaration. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too I have a work around.
If you always know that method "New" exists, all the object you create verify an interface or abstract class IMyClass That way you can all cast into a common IMyClass. You can also add all common properties or method in that interface or abstract class Hope that's help interface IMyClass { IMyClass New(); // instead of New + ClassName } public Class MyClass1 : IMyCLass { public IMyClass New(); } public Class MyClass2 : IMyCLass { public IMyClass New(); } Show quote "BBM" <b**@bbmcompany.com> a écrit dans le message de news:A15C3620-54EE-4BFB-9C71-91C81ECCE865@microsoft.com... > > 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 Thanks to all responders. Marcos, the InvokeMember method returns an
"object" so compilation fails if you try to assign it to another type without casting. I used the common interface solution. Thanks Jon and Romain. Show quote "BBM" wrote: > > 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 |
|||||||||||||||||||||||