Type反射遍历类的属性

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <!--实际生产中这里就是固定的配置方式(前面是实体属性,后面是约定验证方式)-->
    <add key="Setting1" value="name|sex|address,Account"/>
    <add key="Setting2" value="cardID|phone,Identity"/>
  </appSettings>
</configuration>

  

namespace ConsoleApplication1
{
    public class User
    {

        public string name { get; set; }
        public int age { get; set; }
        public string address { get; set; }
        public string cardID { get; set; }
        public string phone { get; set; }

    }

    public class VerfiyInfo
    {
        public string info { get; set; }
        public string method { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {

            User user = new User()
            {
                name = "Tom",
                address = "New York",
                age = 20,
                cardID = "3203821922222",
                phone = "18014925250"
            };
            VerfiyInfo verfiyInfo = GetVerfiy<VerfiyInfo>(user);

            Console.WriteLine(verfiyInfo.info + ":" + verfiyInfo.method);
            Console.ReadKey();
        }
        public static T GetVerfiy<T>(User model)
        {
            string appSetting = System.Configuration.ConfigurationSettings.AppSettings["Setting1"];
            if (string.IsNullOrEmpty(appSetting))
            {
                throw new Exception("无配置信息");
            }
            string[] strA = appSetting.Split(‘,‘);
            string strB = strA[0];
            string result = string.Empty;
            Type tp = model.GetType();
            PropertyInfo[] pros = tp.GetProperties();
            foreach (PropertyInfo pro in pros)
            {
                strB = strB.Replace(pro.Name, pro.GetValue(model).ToString());

            }
            string strJson = "{\"info\":\"" + strB + "\",\"method\":\"" + strA[1] + "\"}";
            T rModel = JsonConvert.DeserializeObject<T>(strJson);
            return rModel;

        }
    }
}

  

时间: 2024-08-05 08:57:47

Type反射遍历类的属性的相关文章

iOS 反射获取类的属性列表

// 获取对象所有属性: - (NSArray*)propertyKeys { unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:outCount]; for (i = 0; i < outCount; i+

C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值

转自goldeneyezhang原文 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值总结: 对应某个类的实例化的对象tc, 遍历获取所有属性(子成员)的方法(采用反射): Type t = tc.GetType();//获得该类的Type //再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了 foreach (PropertyInfo pi

从数据库读取数据后利用反射为对应的实体类的属性赋值

1.连接数据库并关闭连接(jdbctools.java) package com.xiaojie.dao; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.

C#反射技术的简单操作(读取和设置类的属性、属性值)

public class A { public int Property1 { get; set; } } static void Main(){ A aa = new A(); Type type = aa.GetType();//获取类型 System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1"); propertyInfo.SetValue(aa, 5, null);//给对应属性赋值 i

java中如何遍历实体类的属性和数据类型以及属性值

1 package com.walkerjava.test; 2 3 import java.lang.reflect.Field; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 6 import java.util.Date; 7 8 /*** 9 * 遍历实体类的属性和数据类型以及属性值 10 * 11 * @author LiBaozhen 12 * @dat

C#反射技术的简单操作(读取和设置类的属性)

public class A { public int Property1 { get; set; } } static void Main(){ A aa = new A(); Type type = aa.GetType();//获取类型 System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1"); propertyInfo.SetValue(aa, 5, null);//给对应属性赋值 i

java 中利用反射机制获取和设置实体类的属性值

JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制. JAVA反射(放射)机制:"程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言".从这个观点看,Perl,Python,Ruby是动态语言,C++,Java,C#不是动态语言.但是JAVA有着一个非常突出的动态相关机制:Reflection,用在Java身上指的是我们可

C#反射读取和设置类的属性

C#反射技术的简单操作(读取和设置类的属性) http://www.cnblogs.com/william-lin/archive/2013/06/05/3118233.html 泛型方法通过反射创建类的实例 /// <summary> /// 获取web服务实例 /// </summary> /// <typeparam name="T">服务代理类</typeparam> /// <returns></returns

java中循环遍历实体类的属性和数据类型以及属性值

package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Date; /*** * 遍历实体类的属性和数据类型以及属性值 * * @author LiBaozhen * @date 2013-1-4 上午10:25:02 * @co