Cast to a dynamic type? 
Author Message
 Cast to a dynamic type?

Hi all, I'm creating at run time with reflection.emit a
dynamic assembly "DynAssembly" with a class "SimpleClass"
in it like this:
//
public class SimpleClass {
   public void SimpleMethod() {}
Quote:
}

//

Compiling following code:
//
Assembly asm = MethodThatCreatesDymamicAassembly();
Object obj = customProxy.getTrasparentProxy();
SimpleClass sc = (SimpleClass) obj;
sc.SomeMethod();
//
I obviously get an error because compiler don't find a
reference to SimpleClass, which is just been created
dynamically.
How can I cast 'obj' to 'SimpleClass'?



Tue, 29 Jun 2004 01:36:27 GMT  
 Cast to a dynamic type?
Enrico,

    It's not possible, but for very good reason.  If you are creating a
class dynamically then how will you know when typing the code what to cast
it to?  What you need to do is have your dynamic class follow a well-defined
contract, either by making it implement an already-defined interface, or by
extending from an already-defined base class, overriding virtual methods.
After you do that, you can cast to the interface or the base class, and
access your functionality from there.

    Hope this helps.

--
                              - Nicholas Paldino [.NET MVP]


Quote:
> Hi all, I'm creating at run time with reflection.emit a
> dynamic assembly "DynAssembly" with a class "SimpleClass"
> in it like this:
> //
> public class SimpleClass {
>    public void SimpleMethod() {}
> }
> //

> Compiling following code:
> //
> Assembly asm = MethodThatCreatesDymamicAassembly();
> Object obj = customProxy.getTrasparentProxy();
> SimpleClass sc = (SimpleClass) obj;
> sc.SomeMethod();
> //
> I obviously get an error because compiler don't find a
> reference to SimpleClass, which is just been created
> dynamically.
> How can I cast 'obj' to 'SimpleClass'?



Tue, 29 Jun 2004 03:38:55 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. dynamic casting (type known at runtime)

2. dynamic type casting

3. Storing a type for dynamic casts...

4. Dynamic Type Casting at Runtime

5. Type Casting between Base and Derived Type (Serialization of objects with Type members)

6. type casting to / from pointers to incomplete data types

7. type casting to / from pointers to incomplete data types

8. cast cstring type to integer type

9. Casting to/from STL types to MFC types

10. Why can't overload type cast to base type (object)

11. Strong typing....(static/dynamic typing)

12. Dynamic Casting

 

 
Powered by phpBB® Forum Software