아주 예~~전에 만들었던 소스인데, 컴퓨터 정리중 나옴.
마이크로소프트 개발자 사이트에서 본 내용에 살좀 더 붙이고 내가 쓰기좋게 개량한 것.
-------------------------------------------------------------------------------
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Collections.Specialized;
using System.Text;
using System.Data;
using System.Collections;
namespace xxx.xxx.xxx
{
/// <summary>
/// Summary description for PropertyBinder
/// </summary>
public class PropertyBinder : LoggingBase
{
public static void copyAllField(object src, object dest)
{
try
{
if (dest == null)
{
throw new Exception("dest is null..");
}
if (src == null)
{
throw new Exception("src is null..");
}
Type t = src.GetType();
Type destType = dest.GetType();
FieldInfo[] f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
FieldInfo[] destFields = destType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
for (int i = 0; i < f.Length; i++)
{
destFields[i].SetValue(dest, f[i].GetValue(src));
}
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 컨트롤에 입력된 값을 지정된 Object 에 대입한다.
/// CheckBox 는 bool 형태로 입력되며,
/// RadioButton 은 bool 형태와 Y / N 의 형태로 입력되며,
/// RadioButtonList 는 해당 List 의 값으로 입력되며,
/// CheckBoxList 는 선택된 CheckBox 의 Value 값들이 val1, val2, val3 의 형태로 입력되며,
/// DropDownList(Select 중 Single 형태)는 선택된 값이 입력되며,
/// DropDownList(Select 중 Multiple 형태)는 선택된 값들이 val1, val2, val3 의 형태로 입력된다.
/// </summary>
/// <param name="obj">Property Object</param>
/// <param name="container">웹 폼의 Page 객체</param>
public static void BindControlsToObject(object obj, Page page)
{
if (obj == null) return;
Type objType = obj.GetType();
PropertyInfo[] objPropertiesArray = objType.GetProperties();
MasterPage master = page.Master;
foreach (PropertyInfo objProperty in objPropertiesArray)
{
Control control;
if (master != null)
{
ContentPlaceHolder holder = (ContentPlaceHolder)page.Master.FindControl("ContentPlaceHolder1");
control = holder.FindControl(objProperty.Name);
}
else
{
control = page.FindControl(objProperty.Name);
}
if (control is ListControl)
{
ListControl listControl = (ListControl)control;
if (listControl.Items.Count > 1)
{
if (listControl is CheckBoxList)
{
CheckBoxList chkList = (CheckBoxList)listControl;
string val = "";
for (int i = 0; i < chkList.Items.Count; i++)
{
if (chkList.Items[i].Selected)
{
val += chkList.Items[i].Value + ",";
}
}
if ((val.Length - 1) == val.LastIndexOf(",") && val.Length > 0)
{
val = val.Substring(0, val.Length - 1);
}
objProperty.SetValue(obj, val, null);
}
else if (listControl is ListBox)
{
ListBox box = (ListBox)control;
string val = "";
for (int i = 0; i < box.Items.Count; i++)
{
if (box.Items[i].Selected)
{
val += box.Items[i].Value + ",";
}
}
if ((val.Length - 1) == val.LastIndexOf(",") && val.Length > 0)
{
val = val.Substring(0, val.Length - 1);
}
objProperty.SetValue(obj, val, null);
}
else if (listControl.SelectedItem != null)
{
objProperty.SetValue(obj, Convert.ChangeType(listControl.SelectedItem.Value, objProperty.PropertyType), null);
}
}
}
else if (control is CheckBox)
{
CheckBox ch = (CheckBox)control;
if (objProperty.PropertyType == typeof(bool))
{
objProperty.SetValue(obj, Convert.ChangeType(ch.Checked, objProperty.PropertyType), null);
}
else if (objProperty.PropertyType == typeof(string))
{
if (ch.Checked)
{
objProperty.SetValue(obj, Convert.ChangeType("Y", objProperty.PropertyType), null);
}
else
{
objProperty.SetValue(obj, Convert.ChangeType("N", objProperty.PropertyType), null);
}
}
}
else if (control is RadioButton)
{
RadioButton radio = (RadioButton)control;
if (objProperty.PropertyType == typeof(bool))
{
objProperty.SetValue(obj, Convert.ChangeType(radio.Checked, objProperty.PropertyType), null);
}
else if (objProperty.PropertyType == typeof(string))
{
objProperty.SetValue(obj, Convert.ChangeType(radio.Text, objProperty.PropertyType), null);
}
}
else if (control is TextBox)
{
TextBox tx = (TextBox)control;
objProperty.SetValue(obj, Convert.ChangeType(tx.Text, objProperty.PropertyType), null);
}
else if (control is Label)
{
Label lbl = (Label)control;
objProperty.SetValue(obj, Convert.ChangeType(lbl.Text, objProperty.PropertyType), null);
}
else if (control is Calendar)
{
Calendar c = (Calendar)control;
if (objProperty.PropertyType == typeof(DateTime))
{
objProperty.SetValue(obj, Convert.ChangeType(c.SelectedDate, objProperty.PropertyType), null);
}
else if (objProperty.PropertyType == typeof(string))
{
objProperty.SetValue(obj, Convert.ChangeType(c.SelectedDate.ToString(), objProperty.PropertyType), null);
}
}
}
}
#region Property 객체에 있는 값을 컨트롤에 대입한다.
/// <summary>
/// Property 객체에 있는 값을 컨트롤에 대입한다.
/// </summary>
/// <param name="obj">각 프라퍼티에 값들이 채워져 있는 Object</param>
/// <param name="page">컨트롤이 있는 Page 객체</param>
public static void BindObjectToControls(object obj, Page page)
{
if (obj == null) return;
Type objType = obj.GetType();
PropertyInfo[] objPropertiesArray = objType.GetProperties();
MasterPage master = page.Master;
Control control = null;
foreach (PropertyInfo objProperty in objPropertiesArray)
{
if (master != null)
{
ContentPlaceHolder holder = (ContentPlaceHolder)page.Master.FindControl("ContentPlaceHolder1");
if (holder != null)
{
control = holder.FindControl(objProperty.Name);
}
}
else
{
control = page.FindControl(objProperty.Name);
}
if (control != null)
{
if (control is ListControl)
{
if (objProperty.GetValue(obj, null) != null)
{
ListControl listControl = (ListControl)control;
if (listControl.Items.Count > 0)
{
if (listControl is CheckBoxList)
{
CheckBoxList ch = (CheckBoxList)listControl;
string[] val = objProperty.GetValue(obj, null).ToString().Split(',');
for (int i = 0; i < ch.Items.Count; i++)
{
for (int j = 0; j < val.Length; j++)
{
if (ch.Items[i].Value.ToLower().Equals(val[j].ToString().ToLower()))
{
ch.Items[i].Selected = true;
}
}
}
}
else if (listControl is ListBox)
{
ListBox box = (ListBox)listControl;
string[] val = objProperty.GetValue(obj, null).ToString().Split(',');
for (int i = 0; i < box.Items.Count; i++)
{
for (int j = 0; j < val.Length; j++)
{
if (box.Items[i].Value.ToLower().Equals(val[j].ToString().ToLower()))
{
box.Items[i].Selected = true;
}
}
}
}
else if (listControl is RadioButtonList)
{
RadioButtonList rbl = (RadioButtonList)listControl;
if (objProperty.GetValue(obj, null) != null)
{
foreach (ListItem item in rbl.Items)
{
if (objProperty.GetValue(obj, null).ToString().ToLower().Equals(item.Value.ToLower()))
{
item.Selected = true;
break;
}
}
}
}
else if (listControl is DropDownList)
{
DropDownList ddl = (DropDownList)listControl;
if (objProperty.GetValue(obj, null) != null)
{
foreach (ListItem item in ddl.Items)
{
if (objProperty.GetValue(obj, null).ToString().ToLower().Equals(item.Value.ToLower()))
{
item.Selected = true;
break;
}
}
}
}
}
else
{
string propertyValue = objProperty.GetValue(obj, null).ToString();
ListItem listItem = listControl.Items.FindByValue(propertyValue);
if (listItem != null) listItem.Selected = true;
}
}
}
else if (control is CheckBox)
{
if (objProperty.PropertyType == typeof(bool))
{
((CheckBox)control).Checked = (bool)objProperty.GetValue(obj, null);
}
else if (objProperty.PropertyType == typeof(string))
{
if (objProperty.GetValue(obj, null) != null && objProperty.GetValue(obj, null).ToString().ToLower().Equals("y"))
{
((CheckBox)control).Checked = true;
}
}
}
else if (control is TextBox)
{
if (objProperty.GetValue(obj, null) != null)
((TextBox)control).Text = "" + objProperty.GetValue(obj, null).ToString();
}
else if (control is Label)
{
if (objProperty.GetValue(obj, null) != null)
((Label)control).Text = "" + objProperty.GetValue(obj, null).ToString();
}
else if (control is Calendar)
{
if (objProperty.GetValue(obj, null) != null)
{
if (objProperty.PropertyType == typeof(DateTime))
{
((Calendar)control).SelectedDate = (DateTime)objProperty.GetValue(obj, null);
}
}
}
}
}
}
#endregion
#region DataTable ==> Control 로 바인딩 합니다.
public static void BindDataTableToControls(DataTable dt, Page page)
{
MasterPage master = page.Master;
Control control = null;
ContentPlaceHolder holder = (ContentPlaceHolder)master.FindControl("ContentPlaceHolder1");
for (int ii = 0; ii < dt.Rows.Count; ii++)
{
for (int jj = 0; jj < dt.Columns.Count; jj++)
{
if (master != null)
{
if (holder != null)
{
control = holder.FindControl(dt.Columns[jj].ColumnName);
}
}
else
{
control = page.FindControl(dt.Columns[jj].ColumnName);
}
if (control != null)
{
if (control is ListControl)
{
if (dt.Rows[ii][jj] != null)
{
ListControl listControl = (ListControl)control;
if (listControl.Items.Count > 0)
{
if (listControl is CheckBoxList)
{
CheckBoxList ch = (CheckBoxList)listControl;
string[] val = dt.Rows[ii][jj].ToString().Split(',');
for (int i = 0; i < ch.Items.Count; i++)
{
for (int j = 0; j < val.Length; j++)
{
if (ch.Items[i].Value.ToLower().Equals(val[j].ToString().ToLower()))
{
ch.Items[i].Selected = true;
}
}
}
continue;
}
else if (listControl is ListBox)
{
ListBox box = (ListBox)listControl;
string[] val = dt.Rows[ii][jj].ToString().Split(',');
for (int i = 0; i < box.Items.Count; i++)
{
for (int j = 0; j < val.Length; j++)
{
if (box.Items[i].Value.ToLower().Equals(val[j].ToString().ToLower()))
{
box.Items[i].Selected = true;
}
}
}
continue;
}
else if (listControl is RadioButtonList)
{
RadioButtonList rbl = (RadioButtonList)listControl;
if (dt.Rows[ii][jj] != null)
{
foreach (ListItem listItem in rbl.Items)
{
if (dt.Rows[ii][jj].ToString().ToLower().Equals(listItem.Value.ToLower()))
{
listItem.Selected = true;
break;
}
}
}
continue;
}
else if (listControl is DropDownList)
{
DropDownList ddl = (DropDownList)listControl;
if (dt.Rows[ii][jj] != null)
{
foreach (ListItem listItem in ddl.Items)
{
if (dt.Rows[ii][jj].ToString().ToLower().Equals(listItem.Value.ToLower()))
{
listItem.Selected = true;
break;
}
}
}
continue;
}
}
else
{
string propertyValue = dt.Rows[ii][jj].ToString();
ListItem listItem = listControl.Items.FindByValue(propertyValue);
if (listItem != null) listItem.Selected = true;
}
}
}
else if (control is CheckBox)
{
if (dt.Columns[jj].DataType == typeof(bool))
{
((CheckBox)control).Checked = (bool)dt.Rows[ii][jj];
}
else if (dt.Columns[jj].DataType == typeof(string))
{
if (dt.Rows[ii][jj] != null && dt.Rows[ii][jj].ToString().ToLower().Equals("y"))
{
((CheckBox)control).Checked = true;
}
}
continue;
}
else if (control is TextBox)
{
if (dt.Rows[ii][jj] != null)
((TextBox)control).Text = "" + dt.Rows[ii][jj].ToString();
continue;
}
else if (control is Label)
{
if (dt.Rows[ii][jj] != null)
((Label)control).Text = "" + dt.Rows[ii][jj].ToString();
continue;
}
else if (control is Calendar)
{
if (dt.Rows[ii][jj] != null)
{
if (dt.Columns[jj].DataType == typeof(DateTime))
{
((Calendar)control).SelectedDate = (DateTime)dt.Rows[ii][jj];
}
}
continue;
}
}
}
}
}
#endregion
#region Request.Params 컬렉션을 Object 로 매핑합니다
public static void CopyProperty(NameValueCollection src, object dest)
{
if (src == null) throw new Exception("src is null..");
if (dest == null) throw new Exception("dest obj is null..");
Type t = dest.GetType();
FieldInfo[] f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
string key = "";
for (int i = 0; i < src.Count; i++)
{
key = src.GetKey(i);
for (int j = 0; j < f.Length; j++)
{
try
{
if (key.Equals(f[j].Name))
{
if ("System.Int32".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Int32.Parse(src.Get(key)));
}
else if ("System.Int64".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Int64.Parse(src.Get(key)));
}
else if ("System.Single".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Single.Parse(src.Get(key)));
}
else if ("System.Double".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Double.Parse(src.Get(key)));
}
else if ("System.Boolean".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Boolean.Parse(src.Get(key)));
}
else if ("System.Decimal".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Decimal.Parse(src.Get(key)));
}
else if ("System.String".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, src.Get(key));
}
else if ("System.Object".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, src.Get(key));
}
else if ("System.DateTime".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, src.Get(key));
}
}
}
catch
{
}
}
}
} // end
#endregion
public static void MasterPageCopyProperty(NameValueCollection src, object dest)
{
if (src == null) throw new Exception("src is null..");
if (dest == null) throw new Exception("dest obj is null..");
string tmp = "";
Type t = dest.GetType();
FieldInfo[] f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
string key = "";
for (int i = 0; i < src.Count; i++)
{
key = src.GetKey(i);
if (key.LastIndexOf("$") > -1)
{
tmp = key.Substring(key.LastIndexOf("$") + 1);
for (int j = 0; j < f.Length; j++)
{
if (tmp.Equals(f[j].Name))
{
if ("System.Int32".Equals(f[j].FieldType.ToString()))
{
if (CheckNullToInt(src.Get(key).ToString()))
{
f[j].SetValue(dest, Convert.ToInt32(src.Get(key)));
}
}
else if ("System.Int64".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Convert.ToInt64(src.Get(key)));
}
else if ("System.Single".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Single.Parse(src.Get(key)));
}
else if ("System.Double".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Double.Parse(src.Get(key)));
}
else if ("System.Boolean".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Boolean.Parse(src.Get(key)));
}
else if ("System.Decimal".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, Decimal.Parse(src.Get(key)));
}
else if ("System.String".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, src.Get(key));
}
else if ("System.Object".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, src.Get(key));
}
else if ("System.DateTime".Equals(f[j].FieldType.ToString()))
{
f[j].SetValue(dest, src.Get(key));
}
}
}
}
}
} // end
private static bool CheckNullToInt(string args)
{
if (args != null && args.Length > 0)
{
return true;
}
else
{
return false;
}
}
public static string HtmlToString(object obj)
{
if(obj == null) return "obj 가 널이야!! 조사하면 다나와~~!!";
Type t = obj.GetType();
FieldInfo[] f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
StringBuilder builder = new StringBuilder("<Font color=\"\">");
for(int i = 0; i < f.Length; i++)
{
if(f[i].GetValue(obj) != null)
{
builder.Append(f[i].Name.ToString() + " : " + f[i].GetValue(obj) + "<BR>");
}
else
{
builder.Append(f[i].Name.ToString() + " : null<BR>");
}
}
builder.Append("</FONT>");
return builder.ToString();
}
public static string AlertString(object obj)
{
if (obj == null) return "obj 가 널이야!! 조사하면 다나와~~!!";
Type t = obj.GetType();
FieldInfo[] f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
StringBuilder builder = new StringBuilder("");
for (int i = 0; i < f.Length; i++)
{
if (f[i].GetValue(obj) != null)
{
builder.Append(f[i].Name.ToString() + " : " + f[i].GetValue(obj) + "\r\n");
}
else
{
builder.Append(f[i].Name.ToString() + " : null\r\n");
}
}
builder.Append("");
return builder.ToString();
}
public static DataSet ConvertObjectToDataSet(ArrayList arr)
{
DataSet ds = new DataSet();
DataTable dt = null;
if (arr == null || arr.Count == 0)
{
return ds;
}
Object obj = arr[0];
if (obj != null)
{
Type t = obj.GetType();
dt = new DataTable(t.Name);
FieldInfo[] f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
// 데이터 컬럼을 먼저 채운다.
for (int i = 0; i < f.Length; i++)
{
if (f[i].Name != null)
{
dt.Columns.Add(f[i].Name.ToString(), f[i].FieldType);
}
}
foreach (object tmp in arr)
{
t = tmp.GetType();
f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
DataRow tmpRow = dt.NewRow();
for (int i = 0; i < f.Length; i++)
{
if ("System.Int32".Equals(f[i].FieldType.ToString()))
{
if (CheckNullToInt(f[i].GetValue(obj).ToString()))
{
tmpRow[dt.Columns[i].ToString()] = PublicFunction.Int(f[i].GetValue(obj).ToString());
}
}
else if ("System.Int64".Equals(f[i].FieldType.ToString()))
{
tmpRow[dt.Columns[i].ToString()] = PublicFunction.Int64(f[i].GetValue(obj).ToString());
}
else if ("System.Single".Equals(f[i].FieldType.ToString()))
{
tmpRow[dt.Columns[i].ToString()] = Single.Parse(f[i].GetValue(obj).ToString());
}
else if ("System.Double".Equals(f[i].FieldType.ToString()))
{
tmpRow[dt.Columns[i].ToString()] = Double.Parse(f[i].GetValue(obj).ToString());
}
else if ("System.Boolean".Equals(f[i].FieldType.ToString()))
{
tmpRow[dt.Columns[i].ToString()] = Boolean.Parse(f[i].GetValue(obj).ToString());
}
else if ("System.Decimal".Equals(f[i].FieldType.ToString()))
{
tmpRow[dt.Columns[i].ToString()] = Decimal.Parse(f[i].GetValue(obj).ToString());
}
else if ("System.String".Equals(f[i].FieldType.ToString()))
{
tmpRow[dt.Columns[i].ToString()] = f[i].GetValue(obj).ToString();
}
else if ("System.Object".Equals(f[i].FieldType.ToString()))
{
tmpRow[dt.Columns[i].ToString()] = f[i].GetValue(obj);
}
else if ("System.DateTime".Equals(f[i].FieldType.ToString()))
{
tmpRow[dt.Columns[i].ToString()] = Convert.ToDateTime(f[i].GetValue(obj));
}
}
dt.Rows.Add(tmpRow);
}
ds.Tables.Add(dt);
}
return ds;
}
}
}
'.NET > C#' 카테고리의 다른 글
How to create a mailbox-enabled recipient by using Visual C# (0) | 2009.03.16 |
---|---|
Listening to Calendar Events with Outlook 2007 and VSTO (0) | 2008.10.09 |
펌 : OUTLOOK ADDIN 만들때... vsto 설치해야함... (0) | 2008.03.31 |
Exchange 2007 ews 로 루트폴더부터 가져오기... (0) | 2008.03.27 |
Configuring Context-Sensitive Custom Tabs MOC2007 (0) | 2008.03.08 |