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


C# MethodBase.GetMethodBody方法代码示例

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


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

示例1: PrintMethod

    // print out the method (can be used for methodinfo and constructorinfo)
    // method attributes, custom attrubtes (entry point) and methodbody
    void PrintMethod(MethodBase mi)
    {
        try
        {
            sw.Write(".method ");
            sw.Write(" (" + ProcessAttributeString(mi.Attributes.ToString()) + ") ");
            sw.WriteLine(PrintMethodHead(mi));

            // print out the .override
            if (typeof(MethodInfo).IsInstanceOfType(mi))
                if (((MethodInfo)mi).GetBaseDefinition().Equals(mi))
                    sw.Write(".override " + mi.DeclaringType + "." + mi.Name);

            sw.WriteLine("{");
            // method custom attribute
            PrintCustomAttributes(CustomAttributeData.GetCustomAttributes(mi));

            // parameter custom attributes
            // only method info has a return type
            if (typeof(MethodInfo).IsInstanceOfType(mi))
            {
                ParameterInfo ret = ((MethodInfo)mi).ReturnParameter;
                if ((ret != null) && (CustomAttributeData.GetCustomAttributes(ret).Count != 0))
                {
                    sw.WriteLine(".param [0]");
                    PrintCustomAttributes(CustomAttributeData.GetCustomAttributes(ret));
                }
            }
            ParameterInfo[] pis = mi.GetParameters();
            for (int i = 0; i < pis.Length; i++)
            {
                if ((CustomAttributeData.GetCustomAttributes(pis[i]).Count != 0) /*|| (pi.Attributes & ParameterAttributes.HasDefault) != 0)*/)
                {
                    sw.Write(".param [" + (i + 1) + "]");
                    // default value on parameter Info
                    /*if ((pi.Attributes & ParameterAttributes.HasDefault) != 0)
                    {
                        sw.WriteLine(" = " + pis[i].GetRawConstantValue());
                    }
                    else*/
                    sw.WriteLine();
                    if (CustomAttributeData.GetCustomAttributes(pis[i]).Count != 0)
                        PrintCustomAttributes(CustomAttributeData.GetCustomAttributes(pis[i]));
                }
            }
            if (mi.Equals(mi.DeclaringType.Assembly.EntryPoint))
                sw.WriteLine(".entrypoint");

            // print out the method body content
            MethodBody mbd = mi.GetMethodBody();
            if (mbd != null)
            {
                // .maxstack
                sw.WriteLine(".maxstack " + mbd.MaxStackSize);
                // .locals
                sw.Write(".locals ");
                if (mbd.InitLocals)
                    sw.Write("int ");
                sw.Write("(");
                foreach (LocalVariableInfo lv in mbd.LocalVariables)
                    sw.Write(lv.ToString() + " ");
                sw.WriteLine(")");
                // printout the opcode
                ILReader.ILReader ilr = new ILReader.ILReader(mi, CurrentAssembly);
                foreach (ILReader.Opcode opc in ilr.GetOpcodes())
                    sw.WriteLine(opc.DisplayIL());
            }

            sw.WriteLine("} // end of method " + mi.Name);
        }
        catch (Exception e)
        {
            Console.WriteLine("Unexpected Exception At " + mi.ReflectedType + "::" + mi);
            Console.WriteLine(e);
        }
    }
开发者ID:dbremner,项目名称:clrinterop,代码行数:78,代码来源:AssemPrinter.cs


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