Generic-based optimal reflection

Source: Internet
Author: User

public class Order
{
public string OrderId {get; set;}
}
public class Setterwrapper<ttarget, tvalue>
{
Private Action<ttarget, tvalue> _setter;
Public Setterwrapper (PropertyInfo propinfo)
{
if (Propinfo = = null)
throw new ArgumentNullException ("PropertyInfo");
if (!propinfo.canwrite)
throw new NotSupportedException ("property is read-only or private Setter");
MethodInfo Setmethod = Propinfo.getsetmethod (true);
_setter = (Action<ttarget, tvalue>) delegate.createdelegate (typeof (Action<ttarget, tvalue>), NULL, Setmethod);
}
public void SetValue (Ttarget target, TValue val)
{
if (_setter! = null)
{
_setter (Target, Val);
}
}
}
public class Getterwrapper<ttarget, tvalue>
{
Private Func<ttarget, tvalue> _getter;
Public Getterwrapper (PropertyInfo propinfo)
{
if (Propinfo = = null)
throw new ArgumentNullException ("PropertyInfo");
if (!propinfo.canread)
throw new NotSupportedException ("property is unreadable or private Getter");
MethodInfo GetMethod = Propinfo.getgetmethod ();
_getter = (Func<ttarget, tvalue>) delegate.createdelegate (typeof (Func<ttarget, tvalue>), NULL, GetMethod) ;
}
Public TValue GetValue (ttarget target)
{
if (_getter! = null)
{
return _getter (target);
}
return default (TValue);
}
}

static void Main (string[] args)
{
Order Order=new Order () {OrderId = "W1234566"};
PropertyInfo piinfo = typeof (Order). GetProperty ("OrderId");
int maxCount = 1000*1000;
String v = string. Empty;
Stopwatch SW = new Stopwatch ();
Sw. Reset ();
Sw. Start ();
for (int i = 0; i < MaxCount; i++)
{
Order. OrderId = i.ToString ();
v = order. OrderId;
}
Sw. Stop ();
Console.WriteLine ("Direct access: {0}", SW.) Elapsedticks);

Sw. Reset ();
Sw. Start ();

for (int i = 0; i < MaxCount; i++)
{
Piinfo.setvalue (Order,i.tostring ());
v = piinfo.getvalue (order). ToString ();
}
Sw. Stop ();
Console.WriteLine ("Direct reflection: {0}", SW. Elapsedticks);


Sw. Reset ();
Sw. Start ();
Getterwrapper<order, string> getter = new Getterwrapper<order, string> (Piinfo);

Setterwrapper<order, string> setter = new Setterwrapper<order, string> (Piinfo);
for (int i = 0; i < MaxCount; i++)
{

Setter. SetValue (Order, i.tostring ());
v = getter. GetValue (order);
}
Sw. Stop ();
Console.WriteLine ("Generic-due reflection: {0}", SW. Elapsedticks);
Console.read ();

}

Win7,vs 2013,8g Memory, I5CPU under test

Generic-based optimal reflection

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.