开发者

System.Reflection

I developed an application named as AAA in that applic开发者_Python百科ation im referring an assembly named as BBB. At certain condition im loading that BBB assemby into my application using Assembly.LoadFromFile() function. Now i need to access certain object instances of AAA in BBB assembly at run time.Is it possible to accomplish this task? Thanks in Advance .


Are you trying to create new instances of your objects? If so, this should work:

Assembly ass = Assembly.LoadFrom("BBB.dll");
Object myObject = ass.CreateInstance("BBB.MyObject");

Note this assumes that your object has a default constructor - if you need to pass parameters into the constructor, you can do something like this (assuming a constructor which takes a string as its argument):

Assembly ass = Assembly.LoadFrom("BBB.dll");
Type t= ass.GetType("MyObject");
ConstructorInfo c = t.GetConstructor(new Type[]{typeof(string)});
Object myObject2 = c.Invoke(new object[] { "myParam" }


Let AAA hand a reference to the object to some variable in BBB after it has loaded.

To access object ooo from BBB, you should provide BBB with a (possibly indirect) reference to ooo. AAA may use reflection to discover the appropriate entry point for BBB and then provide it with ooo.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜