当前位置: 首页>>代码示例>>C#>>正文


C# DynamicMethod.GetParameters方法代码示例

本文整理汇总了C#中System.Reflection.Emit.DynamicMethod.GetParameters方法的典型用法代码示例。如果您正苦于以下问题:C# DynamicMethod.GetParameters方法的具体用法?C# DynamicMethod.GetParameters怎么用?C# DynamicMethod.GetParameters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.DynamicMethod的用法示例。


在下文中一共展示了DynamicMethod.GetParameters方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DynamicCreateMethod


//.........这里部分代码省略.........
            Console.WriteLine("\r\nMethod Attributes: {0}", hello.Attributes);

            // Display the calling convention of the dynamic method, set when the dynamic method was created.
            Console.WriteLine("\r\nCalling convention: {0}", hello.CallingConvention);

            // Display the declaring type, which is always null for dynamic methods.
            if (hello.DeclaringType == null)
                Console.WriteLine("\r\nDeclaringType is always null for dynamic methods.");
            else
                Console.WriteLine("DeclaringType: {0}", hello.DeclaringType);

            // Display the default value for InitLocals.
            if (hello.InitLocals)
                Console.Write("\r\nThis method contains verifiable code.");
            else
                Console.Write("\r\nThis method contains unverifiable code.");
            Console.WriteLine(" (InitLocals = {0})", hello.InitLocals);

            // Display the module specified when the dynamic method was created.
            Console.WriteLine("\r\nModule: {0}", hello.Module);

            // Display the name specified when the dynamic method was created.
            // Note that the name can be blank.
            Console.WriteLine("\r\nName: {0}", hello.Name);

            // For dynamic methods, the reflected type is always null.
            if (hello.ReflectedType == null)
                Console.WriteLine("\r\nReflectedType is null.");
            else
                Console.WriteLine("\r\nReflectedType: {0}", hello.ReflectedType);
            if (hello.ReturnParameter == null)
                Console.WriteLine("\r\nMethod has no return parameter.");
            else
                Console.WriteLine("\r\nReturn parameter: {0}", hello.ReturnParameter);
            // If the method has no return type, ReturnType is System.Void.
            Console.WriteLine("\r\nReturn type: {0}", hello.ReturnType);

            // ReturnTypeCustomAttributes returns an ICustomeAttributeProvider
            // that can be used to enumerate the custom attributes of the
            // return value. At present, there is no way to set such custom
            // attributes, so the list is empty.
            if (hello.ReturnType == typeof(void))
                Console.WriteLine("The method has no return type.");
            else
            {
                ICustomAttributeProvider caProvider = hello.ReturnTypeCustomAttributes;
                object[] returnAttributes = caProvider.GetCustomAttributes(true);
                if (returnAttributes.Length == 0)
                    Console.WriteLine("\r\nThe return type has no custom attributes.");
                else
                {
                    Console.WriteLine("\r\nThe return type has the following custom attributes:");
                    foreach (object attr in returnAttributes)
                    {
                        Console.WriteLine("\t{0}", attr.ToString());
                    }
                }
            }

            Console.WriteLine("\r\nToString: {0}", hello.ToString());

            // Add parameter information to the dynamic method. (This is not
            // necessary, but can be useful for debugging.) For each parameter,
            // identified by position, supply the parameter attributes and a
            // parameter name.
            ParameterBuilder parameter1 = hello.DefineParameter(1,ParameterAttributes.In,"message");
            ParameterBuilder parameter2 = hello.DefineParameter(2,ParameterAttributes.In,"valueToReturn");

            // Display parameter information.
            ParameterInfo[] parameters = hello.GetParameters();
            Console.WriteLine("\r\nParameters: name, type, ParameterAttributes");
            foreach (ParameterInfo p in parameters)
            {
                Console.WriteLine("\t{0}, {1}, {2}",
                    p.Name, p.ParameterType, p.Attributes);
            }

            // Create a delegate that represents the dynamic method.
            //This action completes the method, and any further attempts to change the method will cause an exception.
            HelloDelegate hi = (HelloDelegate)hello.CreateDelegate(typeof(HelloDelegate));

            // Use the delegate to execute the dynamic method.
            Console.WriteLine("\r\nUse the delegate to execute the dynamic method:");
            int retval = hi("\r\nHello, World!", 42);
            Console.WriteLine("Invoking delegate hi(\"Hello, World!\", 42) returned: " + retval);

            // Execute it again, with different arguments.
            retval = hi("\r\nHi, Mom!", 5280);
            Console.WriteLine("Invoking delegate hi(\"Hi, Mom!\", 5280) returned: " + retval);

            Console.WriteLine("\r\nUse the Invoke method to execute the dynamic method:");
            // Create an array of arguments to use with the Invoke method.
            object[] invokeArgs = { "\r\nHello, World!", 42 };
            // Invoke the dynamic method using the arguments. This is much
            // slower than using the delegate, because you must create an
            // array to contain the arguments, and value-type arguments
            // must be boxed.
            object objRet = hello.Invoke(null, BindingFlags.ExactBinding, null, invokeArgs, new System.Globalization.CultureInfo("en-us"));
            Console.WriteLine("hello.Invoke returned: " + objRet);
        }
开发者ID:keily,项目名称:DotNetBasicStudy,代码行数:101,代码来源:Program.cs


注:本文中的System.Reflection.Emit.DynamicMethod.GetParameters方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。