关于反射GetType().GetProperties()的疑惑
1.
? ? 如果在網上搜一下.Net把List轉換為DataTable,基本上都搜出類似如下一段代碼:
DataTable dt = new DataTable();
?2 if (_list != null)
?3 {
? ? ? //通過反射獲取list中的字段?
?4 ? ?System.Reflection.PropertyInfo[] p = _list[0].GetType().GetProperties();
?5 ? ?foreach (System.Reflection.PropertyInfo pi in p)
?6 ? ? {
?7 ? ? ? dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
?8 ? ? }
?9 ? ?for (int i = 0; i < _list.Count; i++)
10 ? ?{
11 ? ? ? ?IList TempList = new ArrayList();
12 ? ? ? ? //將IList中的一條記錄寫入ArrayList
13 ? ? ? ?foreach (System.Reflection.PropertyInfo pi in p)
14 ? ? ? ?{
15 ? ? ? ? ?object oo = pi.GetValue(_list[i], null);
16 ? ? ? ? ?TempList.Add(oo);
17 ? ? ? ?}
18 ? ? ? object[] itm = new object[p.Length];
19 ? ? ?for (int j = 0; j < TempList.Count; j++)
20 ? ? ?{
21 ? ? ? ?itm.SetValue(TempList[j], j);
22 ? ? ?}
23 ? ? ?dt.LoadDataRow(itm, true);
? ? ? ?}
24 }
? ? 這段代碼的思路是沒有問題;但是實際運行會有問題;因為;
System.Reflection.PropertyInfo[] p = _list[0].GetType().GetProperties();
對于System.Int32類型,不會返回內容;p為空;不會添加列;
對于System.String類型,返回的值是2個;
2 做一個小程序來說明;
全部代碼;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace GetTypeGetPropertys
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? PropertyInfo[] myPropertyInfo;
? ? ? ? ? ? // Get the properties of 'Type' class object.
? ? ? ? ? ? myPropertyInfo = Type.GetType("System.Type").GetProperties();
? ? ? ? ? ? for (int i = 0; i < myPropertyInfo.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //Console.WriteLine(myPropertyInfo[i].ToString());
? ? ? ? ? ? ? ? listBox1.Items.Add(myPropertyInfo[i].ToString());
? ? ? ? ? ? }
? ? ? ? ? ? PropertyInfo[] props = Type.GetType("System.Int32").GetProperties();
? ? ? ? ? ? foreach (PropertyInfo prop in props)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? listBox2.Items.Add(prop.ToString());
? ? ? ? ? ? }
? ? ? ? ? ? PropertyInfo[] props2 = Type.GetType("System.String").GetProperties(); ? ? ? ? ? ?
? ? ? ? ? ? for (int i = 0; i < props2.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? listBox3.Items.Add(props2[i].ToString());
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
界面和運行效果:
? ? 上述三個列表框返回的分別是System.Type、System.Int32、System.String調用GetProperties()返回的結果;微軟對于GetProperties()方法的返回值有如下說明:
返回值
類型:System.Reflection.PropertyInfo[]表示當前?Type?的所有公共屬性的?PropertyInfo?對象數組。
- 或 -
如果當前?Type?沒有公共屬性,則為?PropertyInfo?類型的空數組。
實現
_Type.GetProperties()
3 所以現在要綁定一個List到DataGridView,代碼只能如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace dataGridDemo
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? List<int> li = new List<int>();
? ? ? ? ? ? //List<string> li = new List<string>();
? ? ? ? ? ? for (int i = 0; i < 32; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //li.Add(i.ToString());
? ? ? ? ? ? ? ? li.Add(i);
? ? ? ? ? ? }
? ? ? ? ? ? DataTable tb = new DataTable();
? ? ? ? ? ? tb = ToDataTable(li);
? ? ? ? ? ? dataGridView1.DataSource = tb;
? ? ? ? }
? ? ? ? private DataTable ToDataTable<T>(List<T> items)
? ? ? ? {
? ? ? ? ? ? var tb = new DataTable(typeof(T).Name);
? ? ? ? ? ? //PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
? ? ? ? ? ? //PropertyInfo[] props = typeof(T).GetProperties();
? ? ? ? ? ? PropertyInfo[] props = items[0].GetType().GetProperties();
? ? ? ? ? ? //PropertyInfo[] props = Type.GetType("System.Int32").GetProperties();
? ? ? ? ? ? if (props.Count() == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? tb.Columns.Add(typeof(T).Name, typeof(T));
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? foreach (PropertyInfo prop in props)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Type t = GetCoreType(prop.PropertyType);
? ? ? ? ? ? ? ? ? ? tb.Columns.Add(prop.Name, t);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? foreach (T item in items)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (props.Count() == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? var values = new object();
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < 32; i++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? //values = item.GetValue();
? ? ? ? ? ? ? ? ? ? ? ? values=i;
? ? ? ? ? ? ? ? ? ? ? ? tb.Rows.Add(values);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? var values = new object[props.Length];
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < props.Length; i++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? values[i] = props[i].GetValue(item, null);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? tb.Rows.Add(values);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return tb;
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// Determine of specified type is nullable
? ? ? ? /// </summary>
? ? ? ? public static bool IsNullable(Type t)
? ? ? ? {
? ? ? ? ? ? return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// Return underlying type if type is Nullable otherwise return the type
? ? ? ? /// </summary>
? ? ? ? public static Type GetCoreType(Type t)
? ? ? ? {
? ? ? ? ? ? if (t != null && IsNullable(t))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (!t.IsValueType)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return t;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return Nullable.GetUnderlyingType(t);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return t;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
運行結果;
總結
以上是生活随笔為你收集整理的关于反射GetType().GetProperties()的疑惑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图解PE文件实例研究
- 下一篇: 电力系统软件应用