public static Class Objectutils
{
<summary>
Creates a strongly typed object from source and assigns values based on the same property name.
</summary>
<typeparam name= "T" ></typeparam>
<param name= "source" ></param>
<returns></returns>
public static T createobject<t> (object source) where T:class, new ()
{
var obj = new T ();
var propertiesfromsource = source. GetType (). GetProperties (BindingFlags.Public | BindingFlags.Instance | BINDINGFLAGS.DECLAREDONLY);
var properties = typeof (T). GetProperties (BindingFlags.Public | BindingFlags.Instance | BINDINGFLAGS.DECLAREDONLY);
foreach (var property in properties)
{
var sourceproperty = Propertiesfromsource.firstordefault (x = = X.name = = property. Name);
if (sourceproperty! = null)
{
Property. SetValue (obj, sourceproperty.getvalue (source, NULL), NULL);
}
}
Return obj;
}
///<summary>
//Modifies a strongly typed target Object based on source and assigns values based on the same property name.
//</summary>
//< Typeparam name= "Ttarget" ></TYPEPARAM>
//<typeparam name= "TSource" ></TYPEPARAM>
// <param name= "target" ></PARAM>
//<param name= "source" ></PARAM>
//<param name= " Propertyexpressionsfromsource "></PARAM>
public static void Updateobject<ttarget, Tsource> ( Ttarget Target, TSource source, params Expression<func<tsource, object>>[] propertyexpressionsfromsource)
where Ttarget:class
where Tsource:class
{
if (target = = null)
{
throw new Argumentnullexcepti On ("target");
}
if (source = = null)
{
throw new ArgumentNullException ("source");
}
if (Propertyexpressionsfromsource = = null)
{
throw new ArgumentNullException (" Propertyexpressionsfromsource ");
}
var properties = target. GetType (). GetProperties ();
foreach (Var propertyexpression in Propertyexpressionsfromsource)
{
var propertyfromsource = Getproperty<tsource, object> (propertyexpression);
var propertyfromtarget = properties. Singleordefault (x = X.name = = propertyfromsource.name);
if (propertyfromtarget! = null)
{
Propertyfromtarget.setvalue (target, propertyfromsource.getvalue (source, NULL), NULL);
}
}
}
private static PropertyInfo Getproperty<tsource, tproperty> (Expression<func<tsource, tproperty>> Lambda
{
var type = typeof (TSource);
Memberexpression memberexpression = null;
Switch (lambda. Body.nodetype)
{
Case Expressiontype.convert:
Memberexpression = ((unaryexpression) lambda. Body). Operand as Memberexpression;
Break
Case Expressiontype.memberaccess:
Memberexpression = lambda. Body as Memberexpression;
Break
}
if (memberexpression = = null)
{
throw new ArgumentException (string. Format ("Invalid Lambda Expression ' {0} '.", Lambda.) ToString ()));
}
var propinfo = Memberexpression.member as PropertyInfo;
if (Propinfo = = null)
{
throw new ArgumentException (string. Format ("Expression ' {0} ' refers to a field, not a property.", Lambda. ToString ()));
}
if (type! = Propinfo.reflectedtype &&!type. IsSubclassOf (Propinfo.reflectedtype))
{
throw new ArgumentException (string. Format ("Expresion ' {0} ' refers to a property-is-not-from type {1}.", Lambda. ToString (), type));
}
return propinfo;
}
}
Create or modify a model based on the same field