dynamic casting
-
Hi, I have an object "obj" that is a TypeA. I have a Type object that refers to TypeA. How can I cast obj to TypeA? TypeA is dynamically created from a loaded assembly, so I need to be able to do it dynamically. Or altenatively is there another way to get at obj's fields defined by the TypeA interface ? thanks, Matt
-
Hi, I have an object "obj" that is a TypeA. I have a Type object that refers to TypeA. How can I cast obj to TypeA? TypeA is dynamically created from a loaded assembly, so I need to be able to do it dynamically. Or altenatively is there another way to get at obj's fields defined by the TypeA interface ? thanks, Matt
You gonna have to explain better, perhaps a code segment. Dynamic casting is an urban legend, and is never required! (yes it does feel like u need it sometimes, but its not necessary, really) top secret xacc-ide 0.0.1
-
Hi, I have an object "obj" that is a TypeA. I have a Type object that refers to TypeA. How can I cast obj to TypeA? TypeA is dynamically created from a loaded assembly, so I need to be able to do it dynamically. Or altenatively is there another way to get at obj's fields defined by the TypeA interface ? thanks, Matt
maybe typeof() helps. it will give the type of the object. you should have cases of what types could obj be, it can't be just anything. unless you should go for Generics in C# V2.0 find about generics here: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=370
-
Hi, I have an object "obj" that is a TypeA. I have a Type object that refers to TypeA. How can I cast obj to TypeA? TypeA is dynamically created from a loaded assembly, so I need to be able to do it dynamically. Or altenatively is there another way to get at obj's fields defined by the TypeA interface ? thanks, Matt
Not possible. Because of the Common Type System (CTS) - part of the .NET Framework - the type must be known at compile time or the necessary IL instructions cannot be emitted as part of the module that gets embedded into the primary assembly. The best alternative is to develop a base class from which all of your loaded types (common, especially in plug-in style applications) and cast to that base class, relying on polymorphism to define your distinct types' functionality (that derive from that base class, of course).
Microsoft MVP, Visual C# My Articles