Create or modify a model based on the same field

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.