c#donet 5链接一个库与动态dll
当我尝试使用.NET C#库类链接到另一个由反射
加载的另一个库类的函数时,我会面临问题:
private void Button_Click(object sender, RoutedEventArgs e)
{
// Call static
var lib = new StaticLib.StaticClass();
var a = lib.Test(1); // Works fine
// Call dynamic
var DLL = Assembly.LoadFile(Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + "DynamicLib.dll");
var theType = DLL.GetType("DynamicLib.DynamicClass");
var inst = Activator.CreateInstance(theType);
var method_1 = theType.GetMethod("GetInvert_1");
var b = method_1.Invoke(inst, new object[] { 1 }); // Works fine
var method_2 = theType.GetMethod("GetInvert_2");
var c = method_2.Invoke(inst, new object[] { 1 }); // Exception
}
动态库: <
namespace DynamicLib
{
public class DynamicClass
{
public static int GetInvert_1(int value)
{
return value * (-1);
}
public static int GetInvert_2(int value)
{
var lib = new StaticLib.StaticClass();
return lib.Test(value);
}
}
}
> < strong>动态库使用的静态库:
namespace StaticLib
{
public class StaticClass
{
public int Test(int value)
{
return value * (-1);
}
}
}
- tha call var lib = new staticlib.staticclass(); var a = lib.test(1); :完美工作
- var a = method_1.invoke(c,new object [] {1}); 完美工作。
- 调用 var b = method_2.invoke(c,新对象[] {1}); 在调用的正确时刻崩溃。如果我将呼叫删除到staticlibray.staticclass.test,则没有问题。
例外Interne 1&nbsp;: filenotfoundException&nbsp;:无法加载文件或汇编'staticlib,版本= 1.0.0.0,&gt; culture =中性,publickeytoken = null'。 le fichierspécifiéest可侵入。
这让我感到困惑,因为通过动态库,我无法调用功能测试。文件staticlib.dll与其他文件相同。如果我删除文件staticlib.dll,我什至无法启动该应用程序。因此,问题与dynamiclib.dll和staticlink.dll之间的链接有关,
谢谢您的帮助
I face an issue when i try to use a function from a .NET C# library class link to another library class loaded by Reflection
Here the Application call :
private void Button_Click(object sender, RoutedEventArgs e)
{
// Call static
var lib = new StaticLib.StaticClass();
var a = lib.Test(1); // Works fine
// Call dynamic
var DLL = Assembly.LoadFile(Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + "DynamicLib.dll");
var theType = DLL.GetType("DynamicLib.DynamicClass");
var inst = Activator.CreateInstance(theType);
var method_1 = theType.GetMethod("GetInvert_1");
var b = method_1.Invoke(inst, new object[] { 1 }); // Works fine
var method_2 = theType.GetMethod("GetInvert_2");
var c = method_2.Invoke(inst, new object[] { 1 }); // Exception
}
The Dynamic library :
namespace DynamicLib
{
public class DynamicClass
{
public static int GetInvert_1(int value)
{
return value * (-1);
}
public static int GetInvert_2(int value)
{
var lib = new StaticLib.StaticClass();
return lib.Test(value);
}
}
}
The Static library used by the dynamic library :
namespace StaticLib
{
public class StaticClass
{
public int Test(int value)
{
return value * (-1);
}
}
}
- Tha call var lib = new StaticLib.StaticClass(); var a = lib.Test(1); : Works perfectly
- The call var a = method_1.Invoke(c, new object[] { 1 }); works perfectly.
- The call var b = method_2.Invoke(c, new object[] { 1 }); crashes at the right moment of Invoke. If i remove the call to StaticLibray.StaticClass.test, there is no problem.
Exception interne 1 :
FileNotFoundException : Could not load file or assembly 'StaticLib, Version=1.0.0.0, >Culture=neutral, PublicKeyToken=null'. Le fichier spécifié est introuvable.
It makes me confused, because through the Dynamic library, i can not call the function Test. The file StaticLib.dll is in the same directory than the other files. If i remove the file StaticLib.dll, i can even not start the application. So the problem comes to the link between DynamicLib.dll and StaticLink.Dll
thank for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据MCNETS的说法,这是解决方案:
如果不存在,请创建App.config文件,并添加:
小心:
According to McNets, here is the solution :
Create the app.config file if it does not exist, and add :
Be carefull :
For late versions of .NET, use appdomain