简体   繁体   English

如何使用Reflection获取基础构造函数调用参数

[英]How to get the base constructor calling parameters with Reflection

How to get the hard-coded parameter that the sub-class constructor is used to call the base-class constructor? 如何获取子类构造函数用于调用基类构造函数的硬编码参数?

public class BaseMessage
{
    public BaseMessage(string format, params string[] parameteres)
    {
    }
}

public class HelloMessage : BaseMessage
{
    public HelloMessage(string name) : base("Hello {0}", name)
    {
    }
}

public class IntroductionMessage : BaseMessage
{
    public IntroductionMessage(string name, string myName) : base("Hello {0}, I am {1}", name, myName)
    {
    }
}

I would like to get all the hard-coded formatting string for the sub-classes of BaseMessage, ie "Hello {0}" and "Hello {0}, I am {1}" 我想得到BaseMessage的子类的所有硬编码格式字符串,即“Hello {0}”和“Hello {0},我是{1}”

At the reflection level , the only place this exists is in the method body of the constructor, which gets compiled as ( HelloMessage ): 在反射级别 ,唯一存在的位置是构造函数的方法体,它被编译为( HelloMessage ):

.method public hidebysig specialname rtspecialname instance void
    .ctor(string name) cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: ldstr "Hello {0}"
    L_0006: ldc.i4.1 
    L_0007: newarr string
    L_000c: dup 
    L_000d: ldc.i4.0 
    L_000e: ldarg.1 
    L_000f: stelem.ref 
    L_0010: call instance void BaseMessage::.ctor(string, string[])
    L_0015: ret 
}

or ( IntroductionMessage ): 或( IntroductionMessage ):

.method public hidebysig specialname rtspecialname instance void
    .ctor(string name, string myName) cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: ldstr "Hello {0}, I am {1}"
    L_0006: ldc.i4.2 
    L_0007: newarr string
    L_000c: dup 
    L_000d: ldc.i4.0 
    L_000e: ldarg.1 
    L_000f: stelem.ref 
    L_0010: dup 
    L_0011: ldc.i4.1 
    L_0012: ldarg.2 
    L_0013: stelem.ref 
    L_0014: call instance void BaseMessage::.ctor(string, string[])
    L_0019: ret 
}

So; 所以; via reflection you would have to get the method body ( MethodInfo.GetMethodBody().GetILAsByteArray() ) and manually deconstruct it (noting that you'll have compiled IL to deal with, not the text version I've shown). 通过反射你必须得到方法体( MethodInfo.GetMethodBody().GetILAsByteArray() )并手动解构它 (注意你将编译IL来处理,而不是我已经显示的文本版本)。 This is possible , but is massively over-complicated for what you probably want. 这是可能的 ,但是对于你可能想要的东西来说,这是非常复杂的。 There are runtime IL disassembly tools, but... again: massively overkill here. 运行IL拆卸工具,但是......又:大规模矫枉过正这里。

I would recommend either: 我会建议:

  • just look at what values arrive in format in the BaseMessage at runtime 只需查看在运行时BaseMessageformat
  • use a compile-time tool such as an analyzer - or parse the source with Roslyn - and get the data at build 使用编译时工具(如分析器) - 或使用Roslyn解析源代码 - 并在构建时获取数据
  • something involving attributes or properties; 涉及属性或属性的东西; it is pretty easy to get values from attributes or properties 从属性或属性中获取值非常容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM