I want to define a ref type variable, like this:
SomeType v=null;
and this “SomeType” is defined in another assembly which is loaded at runtime with
Assembly asm=AppDomain.CurrentDomain.Load(...);
I know how to create an object of “SomeType”, just do as below:
Object=asm.CreateInstance(...);
but how to just define a ref-type variable only? Many thanks…
In order to define a type at compile time, you must reference the assembly that contains the type at compile time. If you are binding at runtime, you cannot define an item of that type. If you are loading the type at runtime, you must reflect against it to use it.
There are some options that may help you, however.
I suggest you extract an interface from your runtime bound type and put that in a separate assembly that you can bind against at compile time. You can then declare a variable of that interface and work against it without reflection, using reflection only for the creation of the type.
For example:
Another option available in .NET 4 is to use duck typing with the
dynamickeyword. This will allow you to use myFoo.MyMethod() syntax on an item to which you do not have a compile time reference.For example: