c#donet 5链接一个库与动态dll

发布于 2025-01-30 08:55:46 字数 2152 浏览 5 评论 0原文

当我尝试使用.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2025-02-06 08:55:46

根据MCNETS的说法,这是解决方案:

如果不存在,请创建App.config文件,并添加:

<configuration>
  <runtime>
    <loadFromRemoteSources enabled="true"/>
  </runtime>
</configuration>

小心:

.NET Core,.NET 5或更高版本中不支持

CAS。 CAS不受c#的版本的支持。

According to McNets, here is the solution :

Create the app.config file if it does not exist, and add :

<configuration>
  <runtime>
    <loadFromRemoteSources enabled="true"/>
  </runtime>
</configuration>

Be carefull :

CAS is not supported in .NET Core, .NET 5, or later versions. CAS is not supported by versions of C# later than 7.0.

For late versions of .NET, use appdomain

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文