如何从动态生成的程序集中引用GAC程序集?

cor*_*ttk 4 c# assemblies reference dynamically-generated

我正在用C#教自己中间语言(IL)生成,而且我已经被困了几个小时 - 看起来像是如何System.Windows.Forms.dll从动态程序集中引用(例如) ,我正在使用AppDomain.CurrentDomain.DefineDynamicAssemblySystem.Reflection.Emit...基于http://olondono.blogspot.com/2008/02/creating-code-at-runtime.html上最优秀的例子.

我有一个基本的TransferObjectDllGenerator工作,但现在我想从(仅)生成的程序集中引用现有的库,并且无法弄清楚如何.

这个问题引导我参加这个AppDomain.CurrentDomain.AssemblyResolve活动.我尝试实现一个事件处理程序,但它永远不会触发,所以我想我已经做了一些基本上愚蠢的事情,比如把事件处理程序放在完全错误的地方?

任何指向正确方向的人都会非常感激.

这是我的主线...有趣的是 // <<-- Commented thus

using System;
using System.Reflection;
//using System.Windows.Forms; (and removed the project's Reference to System.Windows.Forms)

namespace ILGen
{
/// <summary>
/// Generates .\bin\$whatever\PersonLibrary.dll containing MSIL equivalent to:
///   namespace PersonLibrary {
///     public class Person {
///       public string FirstName { get; set; }
///       public string LastName { get; set; }
///       public Person() { }
///       public Person(string firstname, string lastname) {
///         FirstName = firstname;
///         LastName = lastname;
///       }
///     } //end-class
///   } //end-namespace
/// </summary>
public class Program
{
    public static void Main(String[] args) {
        AppDomain.CurrentDomain.AssemblyResolve += MyAssemblyResolver; // <<-- Hook the "resolve this assembly" event.
        try {
            var dll = new TransferObjectDllGenerator("PersonLibrary");
            dll.AddClass("Person", new[] {
                new Property("FirstName", typeof(string))
              , new Property("LastName", typeof(string))
              , new Property("OK", Type.GetType("System.Windows.Forms.Button")) // <<-- References System.Windows.Forms.dll; Type.GetType returns null.
            });
            Console.WriteLine("Generated: " + dll.Save());
        } finally {
            AppDomain.CurrentDomain.AssemblyResolve -= MyAssemblyResolver; // <<-- Unhook the "resolve this assembly" event.
        }
    }

    static Assembly MyAssemblyResolver(object sender, ResolveEventArgs args) // <<-- Handle the "resolve this assembly" event.
    {
        if ( args.Name.StartsWith("System.Windows.Forms.") ) // <<-- Breakpoint here, which is never reached.
            return Assembly.LoadFrom(@"C:\Windows\winsxs\msil_system.windows.forms_b77a5c561934e089_6.0.6001.22230_none_1a2132e45d2f30fc\System.Windows.Forms.dll");
        return null;
    }
} // end-class
} // end-namespace
Run Code Online (Sandbox Code Playgroud)

如果您需要/想要TransferObjectDllGenerator代码,请大声说...我还没有发布它,因为它有点太大(恕我直言)的论坛帖子.

在此先感谢您的时间.

干杯.基思.


编辑:为将来找到此主题的任何人提供一个工作示例.

无需提供自定义AssemblyResolve事件处理程序.这是一个phurphy.我们只需要提供程序集的完全限定名称,其中一个具有程序集名称,命名空间,版本和GUID.

using System;
using System.Reflection;

namespace ILGen
{
public class Program
{
    public static void Main(String[] args) {
        var dll = new TransferObjectDllGenerator("PersonLibrary"); 
        // We need to provide the fully-qualified-assembly-name to 
        // make the standard assembly-resolver search the GAC. 
        // Thanks to Julien Lebosquain for pointing this out.
        Type buttonType = Type.GetType(
            "System.Windows.Forms.Button, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); 
        dll.AddClass("Person", new[] {
            new Property("FirstName", typeof(string))
          , new Property("LastName", typeof(string))
          , new Property("OK", buttonType) 
        });
        Console.WriteLine("Generated: " + dll.Save());
    }
} // end-class
} // end-namespace
Run Code Online (Sandbox Code Playgroud)

Jul*_*ain 5

Type.GetType 如果未指定程序集名称,则只搜索当前程序集和mscorlib中的类型名称.

尝试使用AssemblyQualifiedName该类型.这里是.NET 4:

Type.GetType("System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
Run Code Online (Sandbox Code Playgroud)

默认解析程序将在GAC和应用程序域专用路径中进行搜索,因此您无需执行任何操作即可获得所需的行为.如果您必须自定义分辨率,AssemblyResolve或者在.NET 4中采用解析器功能的Type.GetType重载是可行的方法.请注意,从GAC或winsxs路径手动解析通常是一个坏主意:让框架执行其解析作业.