使用反射来从一个类的列表,获得属性的值 [英] Using reflection to get values from properties from a list of a class

查看:95
本文介绍了使用反射来从一个类的列表,获得属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个列表,它是一个主要目标的一部分,内部的对象获取值。



我有一个包含可以是各种属性的主要对象集合。



现在,我试图找出如何访问该对象中包含的一个通用的清单。

  ///<总结> 
///代码内部类
///< /总结>
公共类theClass描述
{
公共theClass描述();

串TheValue {搞定;组; }
} //注意这个类是用于序列化,这样就不会编译原样

///<总结>
///代码的主类
///< /总结>
公共类MainClass
{
公共MainClass();

公开名单< theClass描述> {的thelist获得;组; }
公共字符串SomeOtherProperty {搞定;组; }
公共类SomeOtherClass {搞定;设置}
}


公开名单< MainClass> CompareTheValue(列表<对象> MyObjects,串ValueToCompare)
{
//我有deserialised为列表
变种ObjectsToReturn =新的List<的对象; MainClass>();
的foreach(在MyObjects VAR mObject)
{

//获取属性
的PropertyInfo piTheList = mObject.GetType()的getProperty(的thelist);

对象oTheList = piTheList.GetValue(为MyObject,NULL);


//现在,我有列表对象我提取内部类
//得到我想要
的PropertyInfo piTheValue = oTheList属性的值。属性类型
.GetGenericArguments()[0]
.GetProperty(TheValue);

//获取TheValue出的thelist的和比较它与
// ValueToCompare
//如果匹配,然后添加到列表中的平等被返回

//最终,我会写LINQ查询都要经过列表做比较。
ObjectsToReturn.Add(objectsToReturn);

}
返回ObjectsToReturn;
}



我试图使用的SetValue() 对这个MyObject的,但它有错误了(意译):




对象类型


 私人布尔isCollection(的PropertyInfo p)
{

{
变种T = p.PropertyType.GetGenericTypeDefinition();
返回的typeof(收集和LT;>)。IsAssignableFrom(T)||
的typeof(集合).IsAssignableFrom(T);
}

{
返回FALSE;
}
}
}


解决方案

要获取/使用反射你需要一个实例设置。要通过列表中的项目循环试试这个:

 的PropertyInfo piTheList = MyObject.GetType()的getProperty(的thelist) ; //获取属性

IList的oTheList = piTheList.GetValue(为MyObject,NULL)作为IList的;

//现在,我有列表对象我提取内部类得到我想要

的PropertyInfo piTheValue = piTheList.PropertyType.GetGenericArguments属性的值() 0] .GetProperty(TheValue);

的foreach(在oTheList VAR的listItem)
{
对象theValue = piTheValue.GetValue(的listItem,NULL);
piTheValue.SetValue(的listItem,新,NULL); //< - 设置一个适当的值
}


I am trying to get the values from objects inside a list which is part of a main object.

I have the main object which contains various properties which can be collections.

Right now I am trying to figure out how to access a generic list which is contained in the object.

///<summary>
///Code for the inner class
///</summary>
public class TheClass
{
    public TheClass();

    string TheValue { get; set; }
} //Note this class is used for serialization so it won't compile as-is

///<summary>
///Code for the main class
///</summary>
public class MainClass
{
    public MainClass();

    public List<TheClass> TheList { get; set; }
    public string SomeOtherProperty { get; set; }
    public Class SomeOtherClass { get; set }
}


public List<MainClass> CompareTheValue(List<object> MyObjects, string ValueToCompare)
{ 
    //I have the object deserialised as a list
    var ObjectsToReturn = new List<MainClass>();
    foreach(var mObject in MyObjects)
    {

        //Gets the properties
        PropertyInfo piTheList = mObject.GetType().GetProperty("TheList");

        object oTheList = piTheList.GetValue(MyObject, null);


        //Now that I have the list object I extract the inner class 
        //and get the value of the property I want
        PropertyInfo piTheValue = oTheList.PropertyType
                                          .GetGenericArguments()[0]
                                          .GetProperty("TheValue");

        //get the TheValue out of the TheList and compare it for equality with
        //ValueToCompare
        //if it matches then add to a list to be returned

        //Eventually I will write a Linq query to go through the list to do the comparison.
        ObjectsToReturn.Add(objectsToReturn);

    }
return ObjectsToReturn;
}

I've tried to use a SetValue() with MyObject on this, but it errors out with (paraphrased):

object is not of type

private bool isCollection(PropertyInfo p)
{
    try
    {
        var t = p.PropertyType.GetGenericTypeDefinition();
        return typeof(Collection<>).IsAssignableFrom(t) ||
               typeof(Collection).IsAssignableFrom(t);
    }
    catch
    {
        return false;
    }
    }
}

解决方案

To Get/Set using reflection you need an instance. To loop through the items in the list try this:

PropertyInfo piTheList = MyObject.GetType().GetProperty("TheList"); //Gets the properties

IList oTheList = piTheList.GetValue(MyObject, null) as IList;

//Now that I have the list object I extract the inner class and get the value of the property I want

PropertyInfo piTheValue = piTheList.PropertyType.GetGenericArguments()[0].GetProperty("TheValue");

foreach (var listItem in oTheList)
{
    object theValue = piTheValue.GetValue(listItem, null);
    piTheValue.SetValue(listItem,"new",null);  // <-- set to an appropriate value
}

这篇关于使用反射来从一个类的列表,获得属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆