例如有一个类,名为:TransactionOrder
public class TransactionOrder { public string TransactionOrderlID { get; set; } public System.Guid MemberID { get; set; } public System.Guid TransactionTypeID { get; set; } public decimal PayCoin { get; set; } }
定义一个方法GetPropertyInfoArray:
/// <summary> /// 获取类下所有属性名称 /// </summary> /// <returns></returns> static PropertyInfo[] GetPropertyInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(TransactionOrder); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch (Exception ex) { Console.WriteLine(ex.Message); } return props; }
调用方法:
var prop = GetPropertyInfoArray(); foreach (var item in prop) { Console.WriteLine(item.Name + "=o." + item.Name+","); }