本文整理汇总了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;
}
}