简体   繁体   English

我如何反思并从内部类中获取价值?

[英]How can I reflect over and get the value from this inner class?

A user is passing me a string with a class name and another string with a field from that class that I'm supposed to use to extract some information with. 用户向我传递了一个带有类名的字符串,以及另一个带有该类的字段的字符串,我应该将其用于提取一些信息。 Here are a couple of sample classes; 这是几个示例类; they all inherit from the same base class: 它们都继承自相同的基类:

public class PersonRow : Row
{
   public static readonly RowFields Fields = new RowFields.Init();

   public class RowFields : RowFieldBase
   {
      public Int32Field Id;
      public StringField FirstName;
      public StringField LastName;
   }

}


public class AutomobileRow : Row
{
  public static readonly RowFields Fields = new RowFields.Init();

  public class RowFields : RowFieldBase
  {
     public Int32Field Id;
     public StringField PaintColor;
     public StringField EngineSize;
  }
}

The user will give me something like 'PersonRow' and 'FirstName', from which I need to dive into the FirstName field and extract the value of a member in it called Expression . 用户将给我类似“ PersonRow”和“ FirstName”的名称,我需要从中潜入FirstName字段并提取其中一个名为Expression的成员的值。

I'll spare the details of what Int32Field and StringField are doing internally, but suffice it to say both these types have an Expression member which is a string I need to use. 我将Int32FieldStringField在内部执行的操作的详细信息,但足以说明这两种类型都有一个Expression成员,这是我需要使用的字符串。

I'm trying to use reflection to do this, and have gotten this far: 我正在尝试使用反射来做到这一点,并且到目前为止:

var myClassType = Type.GetType("PersonRow");
var myFields = myClassType.GetField("Fields", BindingFlags.Public | BindingFlags.Static);
var fieldListing = myFields.GetValue(null);

...at this point fieldListing is an object which, underneath the hood, is of type PersonRow.RowFields . ...此时, fieldListing是一个对象,该对象在引擎盖下为PersonRow.RowFields类型。 However I'm kind of stuck in going past this, in that I'm not sure how to enumerate over that array and extract the Expression value from the field I'm interested in? 但是我有点想超越这一点,因为我不确定如何枚举该数组并从我感兴趣的字段中提取Expression值?

I've slightly modified and stubbed out what appears to be your model, and ended up with this: 我稍作修改,然后删除了看起来像您的模型的东西,最后得到了以下结果:

public class PersonRow
{
    public static readonly RowFields Fields = new RowFields();

    public class RowFields
    {
        public StringField FirstName = new StringField();
        public StringField LastName = new StringField();
    }
}

public class StringField
{
    public StringField()
    {
        expr = Expression.Constant("It worked!");
    }
    public Expression expr { get; set; }
    public string str { get; set; }
}

Now, essentially we're looking for the value of PersonRow.Fields.FirstName.expr , which can be done with the following code: 现在,基本上,我们正在寻找PersonRow.Fields.FirstName.expr的值,可以通过以下代码完成:

var personRowType = typeof(PersonRow);

var fieldsField = personRowType.GetField("Fields", BindingFlags.Public | BindingFlags.Static);
var fieldsObj = fieldsField.GetValue(null);

var firstNameField = fieldsField.FieldType.GetField("FirstName");
var firstNameObj = firstNameField.GetValue(fieldsObj);

var exprProp = firstNameField.FieldType.GetProperty("expr");
var exprObj = (ConstantExpression)exprProp.GetValue(firstNameObj);

Here, exprObj will correctly be a ConstantExpression with the value "It worked!" 在这里, exprObj将正确地是一个ConstantExpression表达式,其值为"It worked!"

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

相关问题 如何从一个类的属性值反映一个类? - How to reflect a class from its property value? 如何获取xml中节点的内部值 - How can i get the inner value of a node in xml CompositionContainer:如何从导出的值中获取源类 - CompositionContainer: How can I get the source class from an exported value 如何从xmltag内部获取价值? - How get value from inner a xmltag? 如何获得一个值以扩展Excel中的多行? - How can I get a value to expand over multiple rows in Excel? 如何从元数据类中获取具有属性集的类的属性值? - How can I get the value of a property for a class with an attribute set from a metadata class? 如何使用抽象基类继承内部类? - How can I inherit an inner class using an abstract base class? 如何获取(字符串,列表)的键值对列表的内部列表值? - How can I get the inner list value of list of keyvaluepair of (string, list)? C# 为什么不能从 ioc 获取正确的实例,当我使用反射注册类时 - C# Why cannot get the correct instance from ioc, when i use reflect to regist class 在定义扩展列表的类时,如何从其内部方法引用列表? - When defining a class that extends a list, how can I refer to the list from its inner methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM