Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用propertyInfo获取列表类型属性的值_C#_Reflection_Asp.net Mvc 5_Action Filter - Fatal编程技术网

C# 如何使用propertyInfo获取列表类型属性的值

C# 如何使用propertyInfo获取列表类型属性的值,c#,reflection,asp.net-mvc-5,action-filter,C#,Reflection,Asp.net Mvc 5,Action Filter,如何获取列表类型的属性值,我在模型中的属性是列表类型,例如,我的模型包含列表属性 private static void Getproperties(Object Model) { Type objType = Model.GetType(); PropertyInfo[] properties = objType.GetProperties(); foreach (PropertyInfo prop

如何获取列表类型的属性值,我在模型中的属性是列表类型,例如,我的模型包含列表属性

            private static void Getproperties(Object Model) {
            Type objType = Model.GetType();
            PropertyInfo[] properties = objType.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object propValue;
                //Checking if property is indexed                 
                if(property.GetIndexParameters().Length ==0)
               propValue  = property.GetValue(Model,null);
                else
                {  
                 // want to retrieve collection stored in property index not passing 0 index returning element at 0 location ,how can i get whole list  
                 propValue = property.GetValue(objectModel,new Object[]{0});                       
                 }
                var elems = propValue as IList;
                 .... }
列表

想要创建一个过滤器并添加到操作中,我可以在其中检查潜在的Xss攻击,无论模型是否包含任何此类攻击

您实际上不需要第二个参数的重载。您真正需要的是将
.GetValue()
方法返回的
对象
转换回type
列表
。例如:

List<User>
class-MyClass
{
公共列表MyProperty{get{return new List(){3,4,5,6};}
}
班级计划
{        
静态void Main()
{
MyClass实例=新建MyClass();
PropertyInfo=instance.GetType().GetProperty(“MyProperty”);
列出另一个=info.GetValue(实例)作为列表;
for(int i=0;i
输出:
3 4 5 6

检查这个

class MyClass
{
    public List<int> MyProperty { get { return new List<int>() { 3, 4, 5, 6 }; } }
}

class Program
{        
    static void Main()
    {
        MyClass instance = new MyClass();
        PropertyInfo info = instance.GetType().GetProperty("MyProperty");

        List<int> another = info.GetValue(instance) as List<int>;

        for (int i = 0; i < another.Count; i++)
        {
            Console.Write(another[i] + "   ");
        }
    }
}
List sbColors=new List();
类型colorType=typeof(System.Drawing.Color);
PropertyInfo[]PropInfo列表=colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach(PropInfo列表中的PropertyInfo c)
{
sbColors.Add(c.Name);
}

如果实例和属性名称未知,我想根据模型密码将其创建为泛型。如果我理解正确,您想通过反射从属性获取T参数,并将值强制转换为该类型。问题是泛型类型参数是在编译时推断出来的。所有带有反射的操作都是实时操作,因此这表明我们不能使用泛型来处理动态获取的数据。
      List<string> sbColors = new List<string>();
      Type colorType = typeof(System.Drawing.Color);
      PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
      foreach (PropertyInfo c in propInfoList)
      {
         sbColors.Add(c.Name);
      }