2015-02-04 3 views

답변

2

the documentation for DefineProperty을 보면 여전히 setter (및 getter) 메서드를 직접 정의해야합니다. 이 부분은 set 방법과 관련,하지만 당신은 아마 너무 get 방법을 수행해야합니다 :

// Backing field 
FieldBuilder customerNameBldr = myTypeBuilder.DefineField(
    "customerName", 
    typeof(string), 
    FieldAttributes.Private); 

// Property 
PropertyBuilder custNamePropBldr = myTypeBuilder.DefineProperty(
    "CustomerName", 
    PropertyAttributes.HasDefault, 
    typeof(string), 
    null); 

// Attributes for the set method. 
MethodAttributes getSetAttr = MethodAttributes.Public | 
           MethodAttributes.SpecialName | 
           MethodAttributes.HideBySig; 

// Set method 
MethodBuilder custNameSetPropMthdBldr = myTypeBuilder.DefineMethod(
    "set_CustomerName", 
    getSetAttr,  
    null, 
    new Type[] { typeof(string) }); 

ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator(); 

// Content of the set method 
custNameSetIL.Emit(OpCodes.Ldarg_0); 
custNameSetIL.Emit(OpCodes.Ldarg_1); 
custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr); 
custNameSetIL.Emit(OpCodes.Ret); 

// Apply the set method to the property. 
custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr); 
+0

유는 GET 방법 위원장 코드를 추가 할 수 있을까요? 나는 'br.s IL_0009'전화와 혼동을 느낀다 ... – Jaster

+0

@Jaster 내가 준 링크에있다. – Rawling