[英]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. 我将
Int32Field
和StringField
在内部执行的操作的详细信息,但足以说明这两种类型都有一个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.