<summary>
Class Name: Entityhelper
Class Description: Entity action Method Class
:
Creation Time: 2013/8/12
</summary>
public static Class Entityhelper
{
<summary>
Method Name: Datarowtoentity
Method Description: Convert data rows to entity objects
:
Creation Time: 2013/8/12
</summary>
<typeparam name= "T" > Entity type </typeparam>
<param name= "Dr" > Data rows </param>
<param name= "Excludefields" > Fields not assigned to the value </param>
<returns> Entity Objects </returns>
public static T datarowtoentity<t> (DataRow dr, String excludefields) where T:new ()
{
Type T = typeof (T);
propertyinfo[] Pinfos = T.getproperties ();
T obj = new T ();
foreach (PropertyInfo pi in Pinfos)
{
if (!DR. Table.Columns.Contains (pi. Name))
Continue
if (!string. Isnullorwhitespace (excludefields) && excludefields. Contains (pi. Name))
Continue
if (Dr[pi. Name]! = DBNull.Value)
Pi. SetValue (obj, Dr[pi. Name], NULL);
Else
Pi. SetValue (obj, null, NULL);
}
return obj;
}
<summary>
Method Name: Datarowtoentity
Method Description: Convert data rows to entity objects
:
Creation Time: 2013/8/12
</summary>
<typeparam name= "T" > Entity type </typeparam>
<param name= "Dr" >the dr.</param>
<returns></returns>
public static T datarowtoentity<t> (DataRow dr) where T:new ()
{
Return datarowtoentity<t> (DR, String. Empty);
}
<summary>
Method Name: Datatabletoentitylist
Method Description: Convert data rows to entity objects
:
Creation Time: 2013/8/12
</summary>
<typeparam name= "T" > Entity class </typeparam>
<param name= "DT" > Data Sheet </param>
<returns></returns>
public static list<t> datatabletoentitylist<t> (DataTable dt) where t:new ()
{
Return datatabletoentitylist<t> (Dt,null);
}
<summary>
Method Name: Datatabletoentitylist
Method Description: Convert data rows to entity objects
:
Creation Time: 2013/8/12
</summary>
<typeparam name= "T" > Entity class </typeparam>
<param name= "DT" > Data Sheet </param>
<param name= "Excludefields" > Fields not assigned to the value </param>
<returns></returns>
public static list<t> datatabletoentitylist<t> (DataTable dt,string excludefields) where T:new ()
{
list<t> list = new list<t> ();
if (dt! = NULL)
{
foreach (DataRow dr in Dt. Rows)
{
T t = datarowtoentity<t> (dr,excludefields);
if (t! = null)
List. ADD (t);
}
}
return list;
}
<summary>
Copying entities
</summary>
<typeparam name= "T" ></typeparam>
<param name= "source" ></param>
<returns></returns>
public static T copyentity<t> (t source) where t:new ()
{
Type T = typeof (T);
propertyinfo[] Pinfos = T.getproperties ();
T obj = new T ();
foreach (PropertyInfo pi in Pinfos)
{
Object v = getmembervalue (SOURCE,PI);
Pi. SetValue (obj, V, null);
}
return obj;
}
<summary>
Determine if two entities are the same
</summary>
<param name= "source" ></param>
<param name= "Dest" ></param>
<returns></returns>
public static bool Checkentityequals (object source, Object dest)
{
Type t = source. GetType ();
propertyinfo[] Pinfos = T.getproperties ();
foreach (PropertyInfo pi in Pinfos)
{
Object V1 = Getmembervalue (dest, pi);
Object v = getmembervalue (source, pi);
if (!object. Equals (v1, v))
return false;
}
return true;
}
<summary>
Method Description: Get the value of an entity member
Author
Creation Time: 2013/9/14
</summary>
<param name= "Entity" > Entities </param>
<param name= "Member" > Entity Member information </param>
<returns> value of entity members </returns>
public static object Getmembervalue (object entity, MemberInfo member)
{
if ((entity = = NULL) | | (member = = NULL))
{
return null;
}
Object value = NULL;
Switch (member. MemberType)
{
Case Membertypes.field:
FieldInfo field = member as FieldInfo;
if (field! = null)
{
Value = field. GetValue (entity);
}
Break
Case Membertypes.property:
PropertyInfo property = member as PropertyInfo;
if (property = null)
{
Value = property. GetValue (entity, NULL);
}
Break
Default
Break
}
return value;
}
<summary>
Method Description: Get the value of an entity member
Author
Creation Time: 2013/9/14
</summary>
<param name= "Entity" > Entities </param>
<param name= "MemberName" > Entity member name </param>
<param name= "ignoreCase" > Ignore case </param>
<returns> value of entity members </returns>
public static object Getmembervalue (object entity, String membername, bool ignoreCase)
{
if ((entity = = NULL) | | (String. IsNullOrEmpty (membername)))
{
return null;
}
Memberinfo[] members;
if (ignoreCase)
{
Members = entity. GetType (). GetMember (membername, BindingFlags.Public | Bindingflags.ignorecase | BindingFlags.Instance);
}
Else
{
Members = entity. GetType (). GetMember (membername);
}
if ((members = = null) | | (members. Length = = 0))
{
return null;
}
Return Getmembervalue (Entity, members[0]);
}
public static void Setmembervalue (object entity, String membername, Object Membervalue)
{
if ((entity = = NULL) | | (String. IsNullOrEmpty (membername)))
{
Return
}
Memberinfo[] members;
Members = entity. GetType (). GetMember (membername, BindingFlags.Public | Bindingflags.ignorecase | BindingFlags.Instance);
if ((members = = null) | | (members. Length = = 0))
{
Return
}
if (Members[0] is FieldInfo)
{
if (Membervalue = = DBNull.Value)
(Members[0] as FieldInfo). SetValue (entity, NULL);
Else
(Members[0] as FieldInfo). SetValue (entity, membervalue);
}
else if (Members[0] is PropertyInfo)
{
if (Membervalue = = DBNull.Value)
(Members[0] as PropertyInfo). SetValue (entity, NULL, NULL);
Else
(Members[0] as PropertyInfo). SetValue (entity, membervalue,null);
}
}
}
}
C # Type Copy