2008-09-25 3 views

답변

8

귀하의 질문은 그다지 구체적이지 않습니다. 자세한 정보로 업데이트하면 추가 답변으로이 답변을 완성 해 보겠습니다.

다음은 관련된 수동 단계의 개요입니다.

  1. 는 DefineType와 타입을 만들기를 DefineDynamicAssembly
  2. 으로 DefineDynamicModule
  3. 가진 모듈 어셈블리를 만드는. 유형을 인터페이스로 만들려면 TypeAttributes.Interface을 전달해야합니다.
  4. 원래 인터페이스의 멤버를 반복하고 새 인터페이스에서 유사한 메서드를 빌드하고 필요에 따라 특성을 적용합니다.
  5. TypeBuilder.CreateType으로 전화를 걸어 인터페이스 구축을 완료하십시오.
+0

Nah, 멋지다. 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 클래스를 사용합니다.

관련 문제