using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
{
class Program
{
static void Main(string[] args)
{
}
}
public static class CollectionsConvertor
{
public static List DataTableToList(this DataTable dt) where T : class, new()
{
Type t = typeof(T);
PropertyInfo[] propertys = t.GetProperties();
List lst = new List();
string typeName = string.Empty;
}
public static class CollectionsConvertor
{
public static List DataTableToList(this DataTable dt) where T : class, new()
{
Type t = typeof(T);
PropertyInfo[] propertys = t.GetProperties();
List lst = new List();
string typeName = string.Empty;
foreach (DataRow dr in dt.Rows)
{
T entity = new T();
foreach (PropertyInfo pi in propertys)
{
typeName = pi.Name;
if (dt.Columns.Contains(typeName))
{
if (!pi.CanWrite) continue;
object value = dr[typeName];
if (value == DBNull.Value) continue;
if (pi.PropertyType == typeof(string))
{
pi.SetValue(entity, value.ToString(), null);
}
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
{
pi.SetValue(entity, int.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
{
pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(float))
{
pi.SetValue(entity, float.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(double))
{
pi.SetValue(entity, double.Parse(value.ToString()), null);
}
else
{
pi.SetValue(entity, value, null);
}
}
}
lst.Add(entity);
}
{
T entity = new T();
foreach (PropertyInfo pi in propertys)
{
typeName = pi.Name;
if (dt.Columns.Contains(typeName))
{
if (!pi.CanWrite) continue;
object value = dr[typeName];
if (value == DBNull.Value) continue;
if (pi.PropertyType == typeof(string))
{
pi.SetValue(entity, value.ToString(), null);
}
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
{
pi.SetValue(entity, int.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
{
pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(float))
{
pi.SetValue(entity, float.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(double))
{
pi.SetValue(entity, double.Parse(value.ToString()), null);
}
else
{
pi.SetValue(entity, value, null);
}
}
}
lst.Add(entity);
}
return lst;
}
public static DataTable ListToDataTable(this IEnumerable value) where T : class,new()
{
List lstProperty = new List();
Type type = typeof(T);
DataTable dt = new DataTable();
type.GetProperties().ToList().ForEach(p => //ForEach扩展方法,这里使用Array.ForEach(type.GetProperties(),p=>{})也是一样
{
lstProperty.Add(p);
if (p.PropertyType.IsGenericType)//是否为泛型,泛型获取不到具体的类型
{
dt.Columns.Add(p.Name);
}
else
{
dt.Columns.Add(p.Name, p.PropertyType);
}
});
if (value != null)
{
foreach (var item in value)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
lstProperty.ForEach(p =>
{
row[p.Name] = p.GetValue(item, null);
});
dt.Rows.Add(row);
}
}
return dt;
}
public static DataTable LinqToDataTable(IEnumerable varlist)
{
DataTable dataTable = new DataTable();
}
public static DataTable ListToDataTable(this IEnumerable value) where T : class,new()
{
List lstProperty = new List();
Type type = typeof(T);
DataTable dt = new DataTable();
type.GetProperties().ToList().ForEach(p => //ForEach扩展方法,这里使用Array.ForEach(type.GetProperties(),p=>{})也是一样
{
lstProperty.Add(p);
if (p.PropertyType.IsGenericType)//是否为泛型,泛型获取不到具体的类型
{
dt.Columns.Add(p.Name);
}
else
{
dt.Columns.Add(p.Name, p.PropertyType);
}
});
if (value != null)
{
foreach (var item in value)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
lstProperty.ForEach(p =>
{
row[p.Name] = p.GetValue(item, null);
});
dt.Rows.Add(row);
}
}
return dt;
}
public static DataTable LinqToDataTable(IEnumerable varlist)
{
DataTable dataTable = new DataTable();
// column names
PropertyInfo[] propertyInfo = null;
if (varlist == null) return dataTable;
PropertyInfo[] propertyInfo = null;
if (varlist == null) return dataTable;
foreach (T t in varlist)
{
// Use reflection to get property names, to create table, Only first time, others
if (propertyInfo == null)
{
propertyInfo = ((Type)t.GetType()).GetProperties();
foreach (PropertyInfo pi in propertyInfo)
{
Type colType = pi.PropertyType;
{
// Use reflection to get property names, to create table, Only first time, others
if (propertyInfo == null)
{
propertyInfo = ((Type)t.GetType()).GetProperties();
foreach (PropertyInfo pi in propertyInfo)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dataTable.Columns.Add(new DataColumn(pi.Name, colType));
}
}
}
}
DataRow dr = dataTable.NewRow();
foreach (PropertyInfo pi in propertyInfo)
{
dr[pi.Name] = pi.GetValue(t, null) == null ? DBNull.Value : pi.GetValue
(t, null);
}
{
dr[pi.Name] = pi.GetValue(t, null) == null ? DBNull.Value : pi.GetValue
(t, null);
}
dataTable.Rows.Add(dr);
}
return dataTable;
}
protected void btnShowDialogOne_Click(object sender, EventArgs e)
{
string key = "javascript";
string param = string.Format("BoardCode={0}&BoardName={1}", "001", "zhuban");
//Script. 标签不可省略;Js位于之前,即位于最后位置
string script. = string.Format("", param);
Page.ClientScript.RegisterStartupScript(this.GetType(), key, script);
}
protected void btnShowDialogTwo_Click(object sender, EventArgs e)
{
string key = "javascript";
string param = string.Format("BoardCode={0}&BoardName={1}", "001", "zhuban");
string script. = string.Format("OpenPage('ShowPage.aspx?{0}',600,500);",param);
if (!Page.ClientScript.IsClientScriptBlockRegistered(key))
{
//Script直接写js,无需添加Script标签;Js位于Form标签之后,控件标签之前
//Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, script, true);//Method A
//ScriptManager.RegisterStartupScript(this, this.GetType(), "", script, true);//Method B
//当无UpdatePanel时,Method A 和Method B相同,当有UpdatePanel时Method A失效;MethodB 和以下方法相同
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "", script, true);
}
}
}
}
}
return dataTable;
}
protected void btnShowDialogOne_Click(object sender, EventArgs e)
{
string key = "javascript";
string param = string.Format("BoardCode={0}&BoardName={1}", "001", "zhuban");
//Script. 标签不可省略;Js位于之前,即位于最后位置
string script. = string.Format("", param);
Page.ClientScript.RegisterStartupScript(this.GetType(), key, script);
}
protected void btnShowDialogTwo_Click(object sender, EventArgs e)
{
string key = "javascript";
string param = string.Format("BoardCode={0}&BoardName={1}", "001", "zhuban");
string script. = string.Format("OpenPage('ShowPage.aspx?{0}',600,500);",param);
if (!Page.ClientScript.IsClientScriptBlockRegistered(key))
{
//Script直接写js,无需添加Script标签;Js位于Form标签之后,控件标签之前
//Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, script, true);//Method A
//ScriptManager.RegisterStartupScript(this, this.GetType(), "", script, true);//Method B
//当无UpdatePanel时,Method A 和Method B相同,当有UpdatePanel时Method A失效;MethodB 和以下方法相同
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "", script, true);
}
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28699126/viewspace-772964/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28699126/viewspace-772964/