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