简体   繁体   English

使用反射从字段中获取属性

[英]Using Reflection to get property from field

I have these classes:我有这些课程:

public class Foo
{
    public Bar bar;
}

public class Bar
{
    public List<int> Something { get; set; }
    public string Else { get; set; }
}

I have a lot of classes like Foo .我有很多像Foo这样的课程。 They all contain exactly one field.它们都包含一个字段。 This field, Bar in my example, will be a different type of object every time, it doesn't have to be of type Bar .这个字段,在我的例子中是Bar ,每次都会是不同类型的 object ,它不必是Bar类型。 Whatever object it is ( Bar or otherwise), this field will always be an object that has two properties, Something and Else .无论 object 是什么( Bar或其他),此字段将始终是一个 object 具有两个属性, SomethingElse

My challenge: given any object that's like Foo (with one Field that holds two properties), get the value of the two properties.我的挑战:给定任何类似于 Foo 的 object(一个字段包含两个属性),获取这两个属性的值。 Given this method:鉴于此方法:

public void DoStuff(object myObject)
{
    var asdf = myObject.GetType().GetFields().First();
}

I can get the FieldInfo for Bar.我可以获得 Bar 的 FieldInfo。 What I'm looking for is the values of Something and Else .我正在寻找的是SomethingElse的值。 The names of these properties never change.这些属性的名称永远不会改变。 Whatever the type is of the Field, it will always have two properties named Something and Else that will be of the same type.无论 Field 的类型是什么,它总是有两个属性,名为SomethingElse ,它们的类型相同。

Sadly, my problem involves auto generated code, and there are no shared interfaces or base types I can easily use.可悲的是,我的问题涉及自动生成的代码,并且没有我可以轻松使用的共享接口或基本类型。

Using this code you can retrieve the PropertyInfo s for the Something and for the Else property:使用此代码,您可以检索SomethingElse属性的PropertyInfo

Foo myObject = new Foo { bar = new Bar() };

// the FieldInfo for Foo.bar
var barField = myObject.GetType().GetFields().First();

// the Bar instance, i.e. myObject.bar
var barValue = barField.GetValue(myObject);

var somethingProperty = barField.FieldType.GetProperty("Something", BindingFlags.Instance | BindingFlags.Public);
var elseProperty = barField.FieldType.GetProperty("Else", BindingFlags.Instance | BindingFlags.Public);

as PsiHamster noted, you can then use GetValue and SetValue to retrieve and set the values of somethingProperty and elseProperty like this:正如PsiHamster所说,您可以使用GetValueSetValue来检索和设置somethingPropertyelseProperty的值,如下所示:

// get the value
var somethingValue = somethingProperty.GetValue(barValue);

// set the value
somethingProperty.SetValue(barValue, new List<int>());
//Get Bar FieldInfo
var barFieldInfo = myObject.GetType().GetFields().First();

// Get all public instance properties info
var barProps = barFieldInfo.FieldType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

// Get Something and Else properties info
var smthPropInfo = barProps.First(prop => prop.Name == nameof(Bar.Something));
var elsePropInfo = barProps.First(prop => prop.Name == nameof(Bar.Else));

// Get values
var bar = barFieldInfo.GetValue(myObject);
var smth = smthPropInfo.GetValue(bar);
var else = elsePropInfo.GetValue(bar);

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

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