How C # uses ILGenerator to implement instances of dynamically generated functions

Source: Internet
Author: User
This article mainly introduces the simple code of C # using ILGenerator dynamic generation function, the friend who needs can refer to the following

There are always a lot of configuration files in the game server to read, and these configuration files read: * Otherwise the weak type, is a bunch of strings or numbers, can not see the error (need to re-detect) * Otherwise strong type, each type will need to parse a bit

I personally prefer the latter, because the former Loadconfig code is simple, but the code is not simple when writing logic.


Class Config1:public IConfig {public void Fill (entryline& line); int32_t param1; string param2; Std::vector<int3 2_t> param3;}; void Config1::fill (entryline& line) {this->param1 = line. ReadInt32 (); This->param2 = line. ReadString (); THIS->PARAM3 = line. ReadVectorInt32 ();}

The approximate fill function is written above, just ReadInt32 this function is fictitious and requires you to implement it yourself (HUM)

Then the code is annoying, and I don't really want to write it. This is the reason why this article is produced.

In C # There is XML deserialization, and I define a class:


Class Config1 {int32 param1; string param2; int[] param3;} XmlSerializer serializer = new XmlSerializer (typeof (CONFIG1)); var obj = (Config1) serializer. Deserialize (stream);

This deserialization is very simple, I actually want this thing, just XML deserialization with node, and I want to use attributes, the other is the value of the property I have some of my own personalized things inside.

Thanks to Microsoft for debugging the. NET Framework features so that I can debug. NET source code, see how Microsoft is implemented.

A study of the discovery, he actually was in the XmlSerializer structure, the CONFIG1 analysis of the time, and then generate some meta-data, and Read/write method, deserialize just called the Read method only.

Now that you know how he did it, you'll be able to get one out of it if you study for a while.

Basically you have to get out a prototype and then program the prototype.

PS: Not yet tested performance, should not be too bad, the worst is the server started a few seconds slower.

XmlSerializationReaderILGen.cs

Summarize

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.