根据反射获取对象属性以及相应的值

 以ItemNo构造类,进行举例运行。

GetValue获取值,有两个重载,一般使用的是两个参数的,第二个参数是如果选中的Model的属性是列表索引化的话,可以放索引值,不是索引化属性,则放null

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ceshitype
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] colEName = { "ID", "Name", "Gender", "Age" };
            var itemNo = new ItemNo() { ID = 1, Name = "one", Gender = "man", Age = 18 };
            for (int i = 0; i < colEName.Length; i++)
            {
                object obj = null;
                string value = "";
                Type itemNoType = itemNo.GetType();//获取model类型,Type是个类,里面涉及到的东西很多
                obj = itemNoType.GetProperty(colEName[i]).GetValue(itemNo, null);//GetProperty获取属性,还有一个是GetProperties获取所有属性
                value = Convert.ToString(obj);//将obj
                Console.WriteLine("ItemNo下:{0}的值为:{1},类型为{2}", colEName[i], value, itemNoType.GetProperty(colEName[i]).PropertyType);
            }
            Console.ReadKey();
        }
    }

    public class ItemNo
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int Age { get; set; }
    }
}

 https://msdn.microsoft.com/library/b05d59ty.aspx

官方例子中有给出索引属性的方式

using System;
using System.Reflection;

class Example
{
    public static void Main()
    {
        string test = "abcdefghijklmnopqrstuvwxyz";

        // Get a PropertyInfo object representing the Chars property.
        PropertyInfo pinfo = typeof(string).GetProperty("Chars");

        // Show the first, seventh, and last letters
        ShowIndividualCharacters(pinfo, test, 0, 6, test.Length - 1);

        // Show the complete string.
        Console.Write("The entire string: ");
        for (int x = 0; x < test.Length; x++)
        {
            Console.Write(pinfo.GetValue(test, new Object[] {x}));
        }
        Console.WriteLine();
    }

    static void ShowIndividualCharacters(PropertyInfo pinfo, 
                                         object value,
                                         params int[] indexes)
    {
       foreach (var index in indexes) 
          Console.WriteLine("Character in position {0,2}: '{1}'",
                            index, pinfo.GetValue(value, new object[] { index }));
       Console.WriteLine();                          
    }                                      
}

 

 
 

参照着尝试了一下,如果在itemno类中增加个string TEST进行测试的话,会报参数计数不准确

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ceshitype
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] colEName = { "ID", "Name", "Gender", "Age", "Test" };
            var itemNo = new ItemNo() { ID = 1, Name = "one", Gender = "man", Age = 18, Test = "Test" };
            int x = 0;
            for (int i = 0; i < colEName.Length; i++)
            {
                object obj = null;
                string value = "";
                Type itemNoType = itemNo.GetType();
                if (i==4)
                {
                    for (int j = 0; j < "Test".Length; j++)
                    {
                        obj = itemNoType.GetProperty(colEName[i]).GetValue(itemNo, new object[] { j });
                    }
                }
                else
                {
                    obj = itemNoType.GetProperty(colEName[i]).GetValue(itemNo, null);
                }
                
                value = Convert.ToString(obj);
                Console.WriteLine("ItemNo下:{0}的值为:{1},类型为{2}", colEName[i], value, itemNoType.GetProperty(colEName[i]).PropertyType);
            }
            Console.ReadKey();
        }
    }

    public class ItemNo
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int Age { get; set; }
        public string Test { get; set; }
    }
}

 

下面这种和官方例子差不多的写法,倒是正常运行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ceshitype
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] colEName = { "ID", "Name", "Gender", "Age" };
            var itemNo = new ItemNo() { ID = 1, Name = "one", Gender = "man", Age = 18 };
            for (int i = 0; i < colEName.Length; i++)
            {
                object obj = null;
                string value = "";
                Type itemNoType = itemNo.GetType();
                obj = itemNoType.GetProperty(colEName[i]).GetValue(itemNo, null);
                value = Convert.ToString(obj);
                Console.WriteLine("ItemNo下:{0}的值为:{1},类型为{2}", colEName[i], value, itemNoType.GetProperty(colEName[i]).PropertyType);
            }
            Console.WriteLine("************************");
            string x = "Test";
            Type type = x.GetType();
            for (int i = 0; i < x.Length; i++)
            {
                Console.WriteLine( type.GetProperty("Chars").GetValue(x, new object[] {i }));

            }
            Console.ReadKey();
        }
    }

    public class ItemNo
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int Age { get; set; }
    }
}

 

 

type.GetProperty("").GetValue(models[i], null);

标签类

public class Tag : ModelBase
    {
        public Tag() 
        {
            SortID = 99;
        }
        /// <summary>
        /// 产品标签名
        /// </summary>
        [Required(ErrorMessage = "标签名不能为空")]
        public string Title { get; set; }
        /// <summary>
        /// 该标签是否显示
        /// </summary>
        public bool IsShow { get; set; }
        /// <summary>
        /// 排序,初始99
        /// </summary>
        public int SortID { get; set; }

        public virtual ICollection<ProductTag> ProductTags { get; set; }
    }

var model=new Tag();

Type type = models[i].GetType();

 type.GetProperty("Title").GetValue(model, null);

根据属性名获取相应的值

 

你可能感兴趣的:(根据反射获取对象属性以及相应的值)