2013-05-01 3 views
0

DynamicMethod을 사용하여 다음과 같은 방법을 생성하고 싶습니다.DynamicMethod.DefineParameter가 항상 null을 반환하는 이유는 무엇입니까?

public string HelloWorld([CustomAttribute]string name) 
{ 
    return name; 
} 

다음을 시도했지만 DefineParameter는 항상 null을 반환합니다. 내 사용자 지정 특성을 매개 변수에 어떻게 할당 할 수 있습니까?

class Program 
{ 
    static void Main(string[] args) 
    { 
     var method = new DynamicMethod("HelloWorld", typeof (string), new[] {typeof (string)}); 

     var parameterBuilder = method.DefineParameter(1, ParameterAttributes.In, "text"); 
     parameterBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(CustomAttribute).GetConstructor(Type.EmptyTypes), new object[] {})); 

     var il = method.GetILGenerator(); 
     il.Emit(OpCodes.Ldarg_0); 
     il.Emit(OpCodes.Ret); 

     var temp = (Func<string,string>)method.CreateDelegate(typeof (Func<string, string>)); 
     Console.WriteLine(temp("Hello World")); 
    } 
} 

public class CustomAttribute : Attribute 
{   
} 
+0

[DynamicMethod에서 생성 된 메서드에 사용자 지정 특성을 추가하는 방법?] (http://stackoverflow.com/questions/1145123/how-to-add-custom-attributes-to-a-dynamicmethod 생성 된 메소드) –

답변

0

모르겠어요 하지만 링크 페이지에, 워드 프로세서가 지원되지 않는 말 :

동적 방법과 그 매개 변수가 지명 될 필요는 없지만, 디버깅을 돕기 위해 이름을 지정할 수 있습니다. 사용자 지정 특성은 동적 메서드 또는 해당 매개 변수에서 지원되지 않습니다.

관련 문제