通过全限定名来获取 Type tp = Type.GetType("TJCommon.Dao.Deriver");
通过类来获取 Type tp = typeof(Int)
// 获取类的初始化构造信息 ConstructorInfo ct = tp.GetConstructor(System.Type.EmptyTypes); // 调用不带参数的构造器 T newObj = (T)ct.Invoke(null);
//定义参数类型数组 Type[] tps = new Type[2]; tps[0] = typeof(int); tps[1] = typeof(string); //获取类的初始化参数信息 ConstructorInfo ct2 = tp.GetConstructor(tps); //定义参数数组 object[] obj = new object[2]; obj[0] = (object)100; obj[1] = (object)"Param Example"; //调用带参数的构造器 ExampleClass Ex2 = (ExampleClass)ct2.Invoke(obj);
// 获取到所有公共字段 FieldInfo[] arr = t.GetFields(); // 给指定的字段赋值 需要传递进来一个对象 newObj f.SetValue(newObj, r[name]);
public static T dataToObj(String str)
{
String strSql = str;
DataSet ds = SqlCompose.ExecuteSqlQuery(strSql);
Type t = typeof(T);
DataRow r = ds.Tables[0].Rows[0]; // 找到一行
FieldInfo[] arr = t.GetFields(); // 返回所有公共字段(public)
ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes);
T newObj = (T)ct.Invoke(null);
if (r != null)
{
foreach (FieldInfo f in arr)// 遍历所有字段
{
string name = f.Name;
Type type2 = f.FieldType;
if (r[name].GetType() != typeof(DBNull))
{
string typeName = f.FieldType.Name;
f.SetValue(newObj, r[name]);
}
}
}
else
{
newObj = default(T);
}
ds.Tables.Clear();
return newObj;
}
public static List<T> dataToList(String str)
{
List<T> list = new List<T>();
String strSql = str;
DataSet ds = SqlCompose.ExecuteSqlQuery(strSql);
Type t = typeof(T);
FieldInfo[] arr = t.GetFields(); // 返回所有公共字段(public)
ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes);
foreach (DataRow dr in ds.Tables[0].Rows)
{
T newObj = (T)ct.Invoke(null);
foreach (FieldInfo f in arr)// 遍历所有字段
{
string name = f.Name;
Type type2 = f.FieldType;
string typeName = f.FieldType.Name;
if (dr[name].GetType() != typeof(DBNull))
{
f.SetValue(newObj, dr[name]);
}
}
list.Add(newObj);
}
ds.Tables.Clear();
return list;
}
public static void inserByBean(string tableName, T target)
{
StringBuilder sql = new StringBuilder(); // 拼接的sql
sql.Append("insert into "+tableName+"(");
Type t = target.GetType();
PropertyInfo[] ps = t.GetProperties();
for (int i = 0; i < ps.Length; i++)
{
object obj = ps[i].GetValue(target, null);
if (obj != null)
{
string name = ps[i].Name;
if (i != ps.Length - 1)
{
sql.Append(" " + name + ",");
}
else
{
sql.Append(" " + name + "");
}
}
}
sql.Append(") values(");
for (int i = 0; i < ps.Length; i++)
{
object obj = ps[i].GetValue(target, null);
if (obj != null)
{
if (i != ps.Length - 1)
{
if (ps[i].PropertyType == typeof(string) || ps[i].PropertyType == typeof(DateTime))
{
sql.Append("'" + obj + "',");
}
else {
sql.Append("" + obj + ",");
}
}
else
{
if (ps[i].PropertyType == typeof(string) || ps[i].PropertyType == typeof(DateTime))
{
sql.Append("'" + obj + "')");
}
else
{
sql.Append("" + obj + ")");
}
}
}
}
string resultSql = sql.ToString();
SqlCompose.ExecuteSqlNonQuery(resultSql);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有