我有一个应用程序,我需要在其中创建AppDomain和Load Assembly并执行Assembly中的方法。
这是我的代码
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,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'中找不到入口点。
任何人都可以向我解释上述错误的含义及解决方法。
谢谢,
答案 0 :(得分:6)
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