【问题标题】:Entry point not found in assembly在程序集中找不到入口点
【发布时间】:2011-09-18 06:46:38
【问题描述】:

我有一个应用程序,我需要在其中创建 AppDomain 并将程序集加载到其中并执行程序集中的方法。

这是我的代码

public class CreateAppDomain
{
     public void CreateAppDom()
        {
        AppDomain domain = AppDomain.CreateDomain("myDomain");
        domain.ExecuteAssembly(@"C:\Visual Studio 2005\Projects\A1\A1\bin\Debug\A1.dll");
        domain.CreateInstanceFrom(@"C:\Visual Studio 2005\Projects\A1\A1\bin\Debug\A1.dll","A1.Navigate");
        }

}

我上面的代码写在一个名为 CreateAppDomain.cs 的类文件中

在我的 Default.aspx 页面中,我创建了上述类的实例并调用了 create 方法。这里是代码

protected void Button1_Click(object sender, EventArgs e)
    {
        CreateAppDomain obj = new CreateAppDomain();
        obj.CreateAppDom();
        Response.Write("Application Domain Successfully created");
    }

当我运行 default.aspx 页面时,我收到一条错误提示

在程序集“A1,版本=1.0.0.0,文化=中性,PublicKeyToken=null”中找不到入口点。

谁能解释一下上述错误的含义和解决方法。

谢谢,

【问题讨论】:

    标签: c# asp.net appdomain .net-assembly


    【解决方案1】:

    AppDomain.ExecuteAssembly() 方法将程序集加载到指定域中,然后执行它的标准入口点,即static void Main(string[] args) 方法。

    查看here了解详情。

    你想要的可能是CreateInstanceAndUnwrap() method的重载之一

    编辑:

    我创建了 ConsoleApplication9,除了 ClassLibrary1 之外添加。在 ClassLibrary1 我有 Class1:

    namespace ClassLibrary1
    {
        public class Class1 : MarshalByRefObject
        {
            public void Go()
            {
                Console.WriteLine("My AppDomain's FriendlyName is: {0}", AppDomain.CurrentDomain.FriendlyName);
            }
        }
    }
    

    在 ConsoleApplication9 中:

    private static void Main(string[] args)
    {
        Console.WriteLine("Trying to run method in current domain...");
        var inCurrentDomain = new Class1();
        inCurrentDomain.Go();
    
        Console.WriteLine("\nTrying to run method in remote domain...");
        string asmName = typeof(Class1).Assembly.FullName;
        string typeName = typeof (Class1).FullName;
        Console.WriteLine("Class1's assembly name is: {0}\nType name: {1}", asmName, typeName);
    
        var remoteDomain = AppDomain.CreateDomain("My remote domain");
        var remoteObject = (Class1)remoteDomain.CreateInstanceAndUnwrap(asmName, typeName);
        Console.WriteLine("\nRemote instance created. Running Go() method:");
        remoteObject.Go();
    }
    

    运行时,我有:

    Trying to run method in current domain...
    My AppDomain's FriendlyName is: ConsoleApplication9.exe
    
    Trying to run method in remote domain...
    Class1's assembly name is: ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    Type name: ClassLibrary1.Class1
    
    Remote instance created. Running Go() method:
    My AppDomain's FriendlyName is: My remote domain
    

    【讨论】:

    • 是的,但是您在 .dll 中没有静态 void Main,它只是在应用程序(CLI 或 GUI)中,例如。 .exe 文件。
    • 你是对的。入口点在元数据中设置并且可以更改。无论如何,据我所知,这不是 OP 想要的。他想在远程域中创建一些实例并调用它,而不是执行它命名的某个方法。
    • Thanx.. 我尝试使用 CreateInstanceAndUnwrap() 而不是 createInstanceFrom() 但运气不好我得到了同样的错误
    • 我刚刚更新了我的答案。我的猜测是您错误地传递了 AssemblyName 和/或类型名称。我的示例从控制台应用程序直接引用到库,但这不是必需的。如果您没有参考,则无法将返回的结果转换为具体类。
    猜你喜欢
    • 1970-01-01
    • 2014-05-10
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多