我想问一个问题来了解AppDomain和Activator之间的区别,我通过appdomain.CreateInstance加载了我的dll。但我意识到更多的方法是创建实例。因此,我应该在什么时候或在哪里选择这种方法?Example1:
// 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);Example2:
public WsdlClassParser CreateWsdlClassParser()
{
this.CreateAppDomain(null);
string AssemblyPath = Assembly.GetExecutingAssembly().Location;
WsdlClassParser parser = null;
try
{
parser = (WsdlClassParser) this.LocalAppDomain.CreateInstanceFrom(AssemblyPath,
typeof(Westwind.WebServices.WsdlClassParser).FullName).Unwrap() ;
}
catch (Exception ex)
{
this.ErrorMessage = ex.Message;
}
return parser;
}Example3:
private static void InstantiateMyTypeSucceed(AppDomain domain)
{
try
{
string asmname = Assembly.GetCallingAssembly().FullName;
domain.CreateInstance(asmname, "MyType");
}
catch (Exception e)
{
Console.WriteLine();
Console.WriteLine(e.Message);
}
}你能解释为什么我需要更多的方法吗?或者有什么不同?
发布于 2012-03-13 21:14:54
从sscli2.0源代码中可以看出,类中的"CreateInstance“方法调用总是将调用委托给。
(几乎是静态的) Activator类的唯一目的是“创建”各种类的实例,而引入AppDomain是为了完全不同的(也许是更雄心勃勃的)目的,例如:
由于isolation;
,因此
正如zmbq所指出的,第一个和第三个例子很简单。我猜你的第二个例子来自这个,其中作者展示了如何使用AppDomain卸载过期的代理。
发布于 2012-03-13 17:11:26
第一个方法从程序集'example‘创建一个Example类型的实例,并在其上调用MethodA。
第三个在不同的AppDomain中创建MyType的实例
我不确定第二个,我不知道this是什么,但它似乎在当前的应用程序域中创建了一个类-也就是说,它类似于第一个。
https://stackoverflow.com/questions/9680966
复制相似问题