本文整理匯總了C#中System.AppDomain.AssemblyResolve事件的典型用法代碼示例。如果您正苦於以下問題:C# AppDomain.AssemblyResolve事件的具體用法?C# AppDomain.AssemblyResolve怎麽用?C# AppDomain.AssemblyResolve使用的例子?那麽, 這裏精選的事件代碼示例或許可以為您提供幫助。您也可以進一步了解該事件所在類System.AppDomain
的用法示例。
在下文中一共展示了AppDomain.AssemblyResolve事件的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: MyType
public class MyType
{
public MyType()
{
Console.WriteLine();
Console.WriteLine("MyType instantiated!");
}
}
class Test
{
public static void Main()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
// This call will fail to create an instance of MyType since the
// assembly resolver is not set
InstantiateMyTypeFail(currentDomain);
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
// This call will succeed in creating an instance of MyType since the
// assembly resolver is now set.
InstantiateMyTypeFail(currentDomain);
// This call will succeed in creating an instance of MyType since the
// assembly name is valid.
InstantiateMyTypeSucceed(currentDomain);
}
private static void InstantiateMyTypeFail(AppDomain domain)
{
// Calling InstantiateMyType will always fail since the assembly info
// given to CreateInstance is invalid.
try
{
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
}
catch (Exception e)
{
Console.WriteLine();
Console.WriteLine(e.Message);
}
}
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);
}
}
private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
Console.WriteLine("Resolving...");
return typeof(MyType).Assembly;
}
}