.NET 獲取類型中的屬性
作者:佚名
解決方案:通過反射的方式獲取類型中的所有屬性。
解決方案
通過反射的方式獲取類型中的所有屬性。
引用命名空間
- using System.Reflection;
實體類
- public class User
- {
- private string id;
- public string Id { get { return id; } set { id = value; } }
- private string name;
- public string Name { get { return name; } set { name = value; } }
- }
獲取方法
- private PropertyInfo[] GetPropertyInfoArray()
- {
- PropertyInfo[] props = null;
- try
- {
- Type type = typeof(User);
- object obj = Activator.CreateInstance(type);
- props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
- }
- catch (Exception ex)
- { }
- return props;
- }
責(zé)任編輯:陳四芳
來源:
M守護(hù)神