A simple Reflection class library Nmsreflector

Source: Internet
Author: User

Turn from: http://blog.csdn.net/lanx_fly/article/details/53914338 Background Introduction

Previously seen some code, is simply read Sqlreader and then assigned to the model, I do not disagree with this approach, just see the large space assignment is a bit of a waste of time and energy, especially some old projects mostly. I see the good, many more than 60 fields and no other ORM, if the change is involved, then the maintenance personnel may not only the eyesight live or even .... Physical activity. The other is the operation of the table, because I have written a similar project, the column name corresponds to the model attribute name, one is not bad, vaguely feel that they should be linked, so want to be able to simplify its operation as much as possible? It may be that you do too few projects, only think of the reflection of this method, but the reflection of the performance of everyone also understand that a lot of reflection assignment time can be slow to blink a few eyes, but this program I think is a disaster. So the combination of reflection issued by the method to write this library, if you can bring some convenience in the project I will be satisfied.

Case 1: [CSharp]View PlainCopy print?
  1. Public class Student:inmsreflector {
  2. public string Name;
  3. public string Description { get;          set;}
  4. public static string Staticfield;
  5. public Static string Staticproperty { get;      set;}
  6. }

Reference steps: [HTML]View PlainCopyprint?
    1. STEP1: Reference class Library.
    2. Step2:using Nmsreflector.
    3. STEP3: The implementation of your Class Inmsreflector interface, (of course, if you are too troublesome, you can change the source code, in the ModelOperator.cs).
    4. STEP4: Creates a cache with the Create method. (All classes of the search portal assembly are scanned)

An object instance can call an extension method because it is extended in the class library to the object type.

1, Emitset (string propertyname,object value) assigns a value to the field or property of the object

2, Emitget (String propertyname) Gets the object of a field or property value

Usage: [CSharp]View PlainCopy print?
  1. Modeloperator.create ();
  2. Student t = new Student ();
  3. Normal field
  4. T.name = "Xiao Ming";
  5. T.emitset ("Name", "Xiao Ming's Chest of red scarf more vivid!")     ");
  6. Console.WriteLine (T.name);
  7. Console.WriteLine (T.emitget ("Name"));
  8. Normal properties
  9. T.emitset ("Description", "he loves Xiao Gang");
  10. Console.WriteLine (t.description);
  11. Console.WriteLine (T.emitget ("Description"));
  12. Static fields
  13. T.emitset ("staticfiled", "is he next to Xiao Gang");
  14. Console.WriteLine (Student.staticfield);
  15. Console.WriteLine (T.emitget ("Staticfield"));
  16. Static properties
  17. T.emitset ("Staticproperty", "just hit the wrong");
  18. Console.WriteLine (Student.staticproperty);
  19. Console.WriteLine (T.emitget ("Staticproperty"));

Results:

Case 2:

Support for column labels [CSharp]View PlainCopyprint?
  1. Public class Student:inmsreflector
  2. {
  3. public string Name;
  4. [Column ("Note")]
  5. public string Description { get;   set;}
  6. public static string Staticfield;
  7. public Static string Staticproperty { get;   set;}
  8. }

Attention: [HTML]View PlainCopy print?
    1. The label here is from System.ComponentModel.DataAnnotations.Schema;
    2. Therefore, using System.ComponentModel.DataAnnotations.Schema is required;

Usage: You can assign values or get values regardless of the name of the tag setting or the property name. [CSharp]View PlainCopyprint?
    1. Modeloperator.create ();
    2. Student t = new Student ();
    3. T.emitset ("Note", "set label");
    4. Console.WriteLine (t.description);
    5. Console.WriteLine (T.emitget ("Note"));

Results:

Other: The Modeloperator class provides more operator functions. [CSharp]View PlainCopyprint?
  1. Unlike the extension method of object, the first parameter needs to pass the instance in
  2. Gets the value of a field and property of an instance T
  3. Object Get<t> (T-T, string propertyname)
  4. Set the value of a field and property of an instance T
  5. void set<t> (T T, string propertyname, object value)
  6. Gets the type of a field and property of type T
  7. Type gettype<t> (string propertyname)
  8. Get the Set method cache for type T
  9. dictionary<String, action<object, object>> getsetcache<t> ()
  10. Gets the Get method cache for type T
  11. dictionary<String, func<object, object>> getgetcache<t> ()
  12. Get the property field type cache for type T
  13. dictionary<String, type> gettypecache<t> ()
  14. Gets the label of type T with the property field cache
  15. dictionary<string, string> getmapcache<t> ()

Performance test:

Project Address: Https://github.com/NMSLanX/NMSReflector

A simple Reflection class library Nmsreflector

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.