[Source code] Literacy fast reflection of read/write object attributes, Fields

Source: Internet
Author: User

Literacy description

Literacy uses the IL command to generate a method delegate. In terms of performance, it is much higher than reflection when the number of calls reaches a certain level.

Of course, it takes time to generate a method using the IL command. In this case, not only can the performance be improved, but the performance will be degraded. Therefore, you must be sure to use this method.

Below are some tests on my computer (there will be a slight error due to different machine configurations)

0 ms
100 0 ms
1 K 5 ms
1 W 16 ms
10 W 23 ms
100 million 87 ms

For more information about the IL command generation method, see

Dynamic compilation-advanced: 1. Access static attributes and fields by IL

Dynamic compilation-advanced: 2. Setting static attributes, fields, and type conversion in IL

Dynamic compilation-advanced: III. Reading and setting instance attributes

Common Literacy attributes

 

Literacy (Type type) Constructors load all public attributes
Literacy (Type type, bool ignoreCase)

Constructors load all public attributes

IgnoreCase indicates whether the attribute name is case sensitive.

NewObject ()

Call the no-argument constructor of an object and create an object.

CreateNewObject (Type type, Type [] argTypes = null) IL constructs a delegate to call the specified constructor of an object.
CreateGetter (PropertyInfo prop) IL constructs a delegate to obtain the object property value.
CreateGetter (FieldInfo field) IL constructs a delegate to obtain the value of an object field.
CreateSetter (PropertyInfo prop) IL constructs a delegate used to set object property values.
CreateSetter (FieldInfo field) IL constructs a delegate to set the object field value.
Load. PublicField () Load public instance fields
Load. NonPublicField () Load non-public instance fields
Load. StaticField (bool hasNonPublic) Load public static Fields
The hasNonPublic parameter indicates whether to load non-public static fields at the same time.
Load. NonPublicProperty () Load non-public instance attributes
Load. StaticProperty (bool hasNonPublic) Load public static attributes
The hasNonPublic parameter indicates whether to load non-public static properties at the same time.
Property [string name] Obtains the ObjectProperty object of a specified property.
Property [string name]. GetValue (obj instance) Obtains the value of a specified attribute of an instance object.
Property [string name]. TryGetValue (object instance, out object value) If you fail to get the value of the specified attribute of the Instance Object, false is returned.
Property [string name]. SetValue (object instance, object value) Set the value of the specified attribute of the Instance Object
Property [string name]. TrySetValue (object instance, object value) If you try to set the value of the specified attribute of the Instance Object, false is returned if the error is returned.
Field [string name] Obtains the ObjectProperty object of a specified field.
Field [string name]. GetValue (obj instance) Obtains the value of a specified field of an instance object.
Field [string name]. TryGetValue (object instance, out object value) If you fail to get the value of a specified field of the Instance Object, false is returned.
Field [string name]. SetValue (object instance, object value) Set the value of a specified field of the Instance Object
Field [string name]. TrySetValue (object instance, object value) If you try to set the value of a specified field of the Instance Object, false is returned if the error message is returned.

Literacy call instance

 

Download Literacy source code

Source code download

 

 

Related Article

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.