2008-09-25 18 views

答えて

8

あなたの質問はあまり具体的ではありません。より多くの情報でそれを更新するならば、私はこの答えをさらに細かく説明します。

ここでは、手動の手順の概要を示します。

  1. DefineDynamicAssembly
  2. とアセンブリを作成
  3. DefineTypeとタイプを作成DefineDynamicModule
  4. でモジュールを作成します。あなたのタイプをインターフェースにするには、TypeAttributes.Interfaceを必ず渡してください。
  5. 元のインターフェイスのメンバーを繰り返し処理し、新しいインターフェイスで同様のメソッドを構築し、必要に応じて属性を適用します。
  6. TypeBuilder.CreateTypeを呼び出して、インターフェイスの構築を完了します。動的属性を持つインターフェイスでアセンブリを作成するには
+0

ナー、それはクールです。 Reflection.Emitを使う必要はありませんでしたので、誰かが私の邪悪なマスタープランにつまずくブロックを見つけられるかどうかを見たいと思っていました。 –

12

using System.Reflection; 
using System.Reflection.Emit; 

// Need the output the assembly to a specific directory 
string outputdir = "F:\\tmp\\"; 
string fname = "Hello.World.dll"; 

// Define the assembly name 
AssemblyName bAssemblyName = new AssemblyName(); 
bAssemblyName.Name = "Hello.World"; 
bAssemblyName.Version = new system.Version(1,2,3,4); 

// Define the new assembly and module 
AssemblyBuilder bAssembly = System.AppDomain.CurrentDomain.DefineDynamicAssembly(bAssemblyName, AssemblyBuilderAccess.Save, outputdir); 
ModuleBuilder bModule = bAssembly.DefineDynamicModule(fname, true); 

TypeBuilder tInterface = bModule.DefineType("IFoo", TypeAttributes.Interface | TypeAttributes.Public); 

ConstructorInfo con = typeof(FunAttribute).GetConstructor(new Type[] { typeof(string) }); 
CustomAttributeBuilder cab = new CustomAttributeBuilder(con, new object[] { "Hello" }); 
tInterface.SetCustomAttribute(cab); 

Type tInt = tInterface.CreateType(); 

bAssembly.Save(fname); 

次を作成します。

namespace Hello.World 
{ 
    [Fun("Hello")] 
    public interface IFoo 
    {} 
} 

方法TypeBuilder.DefineMethodを呼び出すことにより、MethodBuilderクラスを使用の追加。

関連する問題