4

私はC#で中間言語(IL)の生成を自分自身に教えていますがSystem.Windows.Forms.dll、動的アセンブリから(たとえば)参照する方法について、数時間のように見えます。 、を使用AppDomain.CurrentDomain.DefineDynamicAssemblyして生成しています... http://olondono.blogspot.com/2008/02/creating-code-at-runtime.htmlSystem.Reflection.Emitの最も優れた例に基づいています

基本的なTransferObjectDllGeneratorが機能していますが、生成されたアセンブリ内から(のみ)既存のライブラリを参照したいので、その方法がわかりません。

このSOの質問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

TransferObjectDllGeneratorコードが必要/必要な場合は、叫んでください...フォーラムの投稿には少し大きすぎる(IMHO)ため、投稿していません。

よろしくお願いします。

乾杯。キース。


編集:将来このスレッドを見つけた人に実用的な例を提供するため。

カスタムAssemblyResolveイベントハンドラーを提供する必要はありません。それはファーフィーでした。アセンブリの完全修飾名を指定する必要があります...アセンブリ名、名前空間、バージョン、および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
4

1 に答える 1

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")

既定のリゾルバーは GAC とアプリ ドメインのプライベート パスを検索するため、目的の動作を得るために何もする必要はありません。解像度をカスタマイズする必要がある場合、AssemblyResolveまたは.NET 4 でリゾルバー関数を使用する新しいType.GetTypeオーバーロードが適しています。GAC または winsxs パスから手動で解決することは、通常は悪い考えであることに注意してください。フレームワークに解決作業を任せてください。

于 2011-07-02T10:59:16.653 に答える