none
获取方法参数列表,得到一个parameterInfo类型的实例,如何根据parameterInfo实例获取参数的值? RRS feed

答案

  • 没有办法,除非用Profiling API. 参数的值为运行时堆栈传递。

    It is not possible to get the current parameter values without using the Profiling API.

    MethodInfo objects are per-method, not per-call. There is no way to connect a MethodInfo with a given stack frame.

    In addition, in Release builds, the parameter locals can be optimized out, so the values to not necessarily exist.

    The DefaultValue property can be non-null in VB parameters, which already supports default values.

    https://stackoverflow.com/questions/2147368/using-reflection-to-analyze-parameters-and-their-values

    You can't, basically - at least not without hooking into the debugger/profiling API.

    In theory there could be some way of the StackFrame class exposing parameter values, but it doesn't - and I suspect that in order to do so, you'd have to remove several optimisations.

    https://stackoverflow.com/questions/1867482/c-sharp-getting-value-of-parms-using-reflection


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年4月3日 0:34

全部回复

  • Hi,

    欢迎在MSDN论坛发帖。

    据我所知GetParameters 这个方法是返回一个ParameterInfo 对象的数组,来代表方法的参数。

    你有尝试使用这个方法吗? 你可以参考下面部分代码;

    if (mi.MemberType==MemberTypes.Method)
                        {
                            foreach ( ParameterInfo pi in ((MethodInfo) mi).GetParameters() )
                            {
                                Display(indent+1, "Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name);
                            }
                        }

    如果你遇到什么错误,希望贴出错误信息,这样会更快的解决错误。

    关于ParameterInfo 类,你可以参考下面的文档。

    https://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo(v=vs.110).aspxhttps://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo_methods(v=vs.110).aspx

    Best Regards,

    Hart


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年4月2日 6:52
    版主
  • Hi,

    欢迎在MSDN论坛发帖。

    据我所知GetParameters 这个方法是返回一个ParameterInfo 对象的数组,来代表方法的参数。

    你有尝试使用这个方法吗? 你可以参考下面部分代码;

    if (mi.MemberType==MemberTypes.Method)
                        {
                            foreach ( ParameterInfo pi in ((MethodInfo) mi).GetParameters() )
                            {
                                Display(indent+1, "Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name);
                            }
                        }

    如果你遇到什么错误,希望贴出错误信息,这样会更快的解决错误。

    关于ParameterInfo 类,你可以参考下面的文档。

    https://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo(v=vs.110).aspxhttps://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo_methods(v=vs.110).aspx

    Best Regards,

    Hart


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    这个我使用过了,pi.ParameterType是参数的类型,pi.Name,是参数名, 而我想要找的是参数所装载的内容,也就是值,我测试了一下调用ParameterInfo类中方法中所有返回值是object类型, 但是发现都不是参数的值


    2018年4月2日 10:20
  • 没有办法,除非用Profiling API. 参数的值为运行时堆栈传递。

    It is not possible to get the current parameter values without using the Profiling API.

    MethodInfo objects are per-method, not per-call. There is no way to connect a MethodInfo with a given stack frame.

    In addition, in Release builds, the parameter locals can be optimized out, so the values to not necessarily exist.

    The DefaultValue property can be non-null in VB parameters, which already supports default values.

    https://stackoverflow.com/questions/2147368/using-reflection-to-analyze-parameters-and-their-values

    You can't, basically - at least not without hooking into the debugger/profiling API.

    In theory there could be some way of the StackFrame class exposing parameter values, but it doesn't - and I suspect that in order to do so, you'd have to remove several optimisations.

    https://stackoverflow.com/questions/1867482/c-sharp-getting-value-of-parms-using-reflection


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年4月3日 0:34