DynamicMethod 생성자 (String, Type, Type[])
2013-03-11
메서드 이름, 반환 형식 및 매개 변수 형식을 지정하여 익명으로 호스팅된 동적 메서드를 만듭니다.
어셈블리: mscorlib(mscorlib.dll)
매개 변수
- name
- 형식: System.String
동적 메서드의 이름입니다. 이는 길이가 0인 문자열이 될 수 있지만 null은 될 수 없습니다.
- returnType
- 형식: System.Type
동적 메서드의 반환 형식을 지정하는 Type 개체이거나, 메서드에 반환 형식이 없는 경우에는 null입니다.
- parameterTypes
- 형식:
System.Type
[]
동적 메서드의 매개 변수 형식을 지정하는 Type 개체로 이루어진 배열이거나, 메서드에 매개 변수가 없는 경우에는 null입니다.
| 예외 | 조건 |
|---|---|
| ArgumentException | parameterTypes의 요소가 null 또는 Void인 경우 |
| ArgumentNullException | name이 null인 경우 |
| NotSupportedException | returnType이 Type.IsByRef가 true를 반환하는 형식인 경우 |
이 생성자를 사용하여 만든 동적 메서드는 기존 형식이나 모듈 대신 익명 어셈블리에 연결됩니다. Windows Phone 응용프로그램에서는 동적 메서드를 기존 형식이나 모듈에 연결할 수 없습니다. 익명 어셈블리는 동적 메서드에 샌드박스 환경을 제공할 목적으로, 즉 다른 코드로부터 격리할 목적으로만 사용됩니다.
동적 메서드의 MSIL(Microsoft Intermediate Language)에 대해 JIT(Just-In-Time) 가시성 검사가 수행됩니다. 즉, 동적 메서드의 코드에서 public 클래스의 public 메서드에 액세스할 수 있습니다. 메서드가 private, protected 또는 internal(Visual Basic에서는 Friend)인 형식이나 멤버에 액세스하면 예외가 발생합니다.
이 생성자는 메서드 특성으로 MethodAttributes.Public 및 MethodAttributes.Static을 지정하고 호출 규칙으로 CallingConventions.Standard를 지정합니다.
다음 예제에서는 이 생성자를 사용하여 단순한 동적 메서드를 내보내고 실행하는 방법을 보여 줍니다. 메서드에는 형식 문자열과 정수의 두 가지 매개 변수가 있습니다. 메서드는 이러한 두 개의 인수를 사용하여 String.Format(String, Object) 메서드 오버로드를 호출하고 결과를 반환합니다.
참고: |
|---|
이 예제를 실행하려면 Windows Phone용 정적 TextBlock 컨트롤이 있는 예제 빌드를 참조하세요. |
using System.Reflection.Emit; using System.Reflection; using System; public class Example { // Declare a delegate that can be used to execute the dynamic method. private delegate string Caller(string msg, int number); public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Create an array that specifies the types of the parameters // of the dynamic method. This method has a String parameter // and an Integer parameter. Type[] paramTypes = { typeof(string), typeof(int) }; // Create an unnamed dynamic method with a return type of // Integer and with two parameters whose types are specified by // the array paramTypes. The dynamic method is anonymously // hosted. DynamicMethod method = new DynamicMethod("", typeof(string), paramTypes); // Get a MethodInfo for the overload of String.Format that // takes a format string and an object for insertion in the format // string. The dynamic method uses this overload to format the // return string. MethodInfo stringFormat = typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(object) }); // Get an ILGenerator and emit a body for the dynamic method. ILGenerator il = method.GetILGenerator(); // Load the arguments onto the execution stack. The second // argument is an Integer, so it must be boxed before it can be // passed to a parameter of type Object. il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Box, typeof(int)); // Call the String.Format method. The return value from that call // is placed on the execution stack, so the dynamic method simply // returns. il.Emit(OpCodes.Call, stringFormat); il.Emit(OpCodes.Ret); // Create a delegate that represents the dynamic method. This // action completes the dynamic method, and any further attempts // to change the method have no effect. Caller callMethod = (Caller) method.CreateDelegate(typeof(Caller)); // Invoke the delegate and display the result. string ret = callMethod("The second argument is: {0}.", 1969); outputBlock.Text += String.Format("Invoking the delegate returned the string '{0}'.", ret); } } /* This code example produces the following output: Invoking the delegate returned the string 'The second argument is: 1969.'. */
참고: