DLL 函数调用不起作用

sub*_*ham 2 c# dll

我创建了一个名为 ClassLibrary1.dll 的 DLL。
它在Class1类中只包含一个函数iscall()

//Function of DLL
public bool iscalled()
  {
     return true;
  }
Run Code Online (Sandbox Code Playgroud)

现在我创建了一个新的 WINFORM 项目,并在那里添加了我自己的 dll ClassLibrary1的引用。

下面是winForm代码的代码片段

[DllImport("ClassLibrary1.dll")]
public static extern bool iscalled();


public void mydllcall1()
 {          
     bool ud = iscalled();
     MessageBox.Show(ud.ToString());
 }
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,遇到一个错误说明

无法在 DLL“ClassLibrary1.dll”中找到名为“iscall”的入口点

我正在寻找一些解决方案。

感谢致敬

苏哈姆·库马尔,Nathcorp

AAA*_*ddd 5

您不能调用DLLImport.net 程序集。(DLLImport 属性用于标准动态链接库)。您需要改为使用Assembly.Load或类似

如何:将程序集加载到应用程序域中

有多种方法可以将程序集加载到应用程序域中。推荐的方法是使用类的静态(在 Visual Basic 中为 Shared)Load 方法System.Reflection.Assembly。可以加载程序集的其他方式包括:

  • Assembly 类的 LoadFrom 方法加载给定文件位置的程序集。使用此方法加载程序集使用不同的加载上下文。

  • ReflectionOnlyLoadReflectionOnlyLoadFrom方法加载组件进入只反射上下文。加载到此上下文中的程序集可以被检查但不能执行,从而允许检查针对其他平台的程序集。

例子

public static void Main()
{
    // Use the file name to load the assembly into the current
    // application domain.
    Assembly a = Assembly.Load("example");
    // Get the type to use.
    Type myType = a.GetType("Example");
    // Get the method to call.
    MethodInfo myMethod = myType.GetMethod("MethodA");
    // Create an instance.
    object obj = Activator.CreateInstance(myType);
    // Execute the method.
    myMethod.Invoke(obj, null);
}
Run Code Online (Sandbox Code Playgroud)

进一步阅读

Assembly.Load 方法 (AssemblyName)