开发者

How to define a ref-type variable from a assembly loaded at runtime?

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:

// defined in MyFoo.Contract.dll, or something like that. Your code 
// references this assembly at compile time. 
public interface IMyFoo
{
   // members
}

// defined in MyFoo.dll, or something like that. Your code loads 
// this assembly at runtime. MyFoo.dll references MyFoo.Contract.dll
// at compile time.
public class MyFoo : IMyFoo
{
  // class def
}

// elsewhere...
Assembly asm = AppDomain.CurrentDomain.Load(...);
object myFooInstance = asm.CreateInstance(...);
IMyFoo myFoo = (IMyFoo)myFooInstance;

Another option available in .NET 4 is to use duck typing with the dynamic keyword. This will allow you to use myFoo.MyMethod() syntax on an item to which you do not have a compile time reference.

For example:

dynamic myFoo = asm.CreateInstance(...);
myFoo.MyMethod(); 
// bound at runtime, but the compiler will allow it! No nasty 
// Invoke() calls. 


You'll have to define some kind of base class or interface known by the project you're building, and make the runtime loaded type implement that well known interface or inherit from the well known base class.


You have a number of options. As Randolpho suggests, you can couple directly to the type by referencing it. Another option would be to create an abstraction, such as an abstract class or an interface, it its own assembly. The dynamically-loaded and client assemblies can then both reference the assembly with the abstraction, with the dynamically loaded type implementing the interface and the client type downcasting to and using the interface.

If you only care about the type and the reference but not about the interface, then all you need is a variable of type object.

It is not clear from your question precisely what you need.


One solution would be to have an interface that you DO reference at compile-time. Then, make sure that the type you reference at runtime implements that interface.

Then you can write code using that interface and have the nice compile time checking and easy coding, while still being able to load your other type dynamically!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜