开发者

strange problem with reflection and static method

Visual Studio 2008 - framework 3.5 - Visual Basic

Hi! I have a problem with a static method invoked by reflection. On the loading of my win-wpf I create a copy of "A4Library.dll" with the name "_temp.dll", in the same directory of the original. Then, on a button-click event, I invoke a static method on the _temp.dll in this way:

Dim AssemblyFileName As String = Directory.GetCurrentDirectory() & "\_temp.dll"
Dim oAssembly As Assembly = Assembly.LoadFrom(AssemblyFileName)
Dim TypeName As String = "MyLibrary.MyService"
Dim t As Type = oAssembly.GetType(TypeName)
Dim mi As MethodInfo = t.GetMethod("MyMethod", BindingFlags.Static AndAlso BindingFlags.Public)
Dim bResponse As Boolean = mi.Invoke(Nothing, New Object() {MyPar1, MyPar2})

But this works well only if I the .exe file is not in the same directory of the .dll files, otherwise I obtain this error (translated):

InnerException {"Cast impossible of [A]MyType on [B]MyType. The type A is originated from ... in the context 'Default' in the position 'F:\MyPath\A4Library.dll'. The type B is originated from ... in the context 'LoadFrom' in the position 'F:\MyPath_temp.dll'."}

It's strange: it seems to be a conflict with the same method in the original .dll, but I can't undertend why it looks at the original and not at the copy. If the .exe file relative to the princi开发者_如何转开发pal assembly is placed in another directory, all runs well.

I neet to have the .exe in the same folder of the .dll, how can I solve the problem?

Thank you! Pileggi


Why create a copy of assembly before executing static method? If creating a copy is needed, load that assembly in another AppDomain and execute the method in there.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜