Reflection + XML serialization practical application

Source: Internet
Author: User

PreviousArticleIn, I recorded the first time I used reflection to implement a simple smallProgramIn this article, I plan to implement a prototype of effect that I will achieve at work three days later.

My task is the development of the mail template management module. The specific business is to integrate all the emails to be used in the system and provide client calls using the WCF Service, the client may send different mail content, but for the convenience of unified management and template modification, we use the template engine to parse the template content. For a simple example, for example, our winform program has the function of sending emails to customers, the previous practice was to directly store the mail template in a directory under the client and replace the placeholder in the program to get the mail content or simply directlyCodeYou can concatenate the content of the email by concatenating strings and then call WCF to send the email. This leads to a problem, after using each email for a period of time, you may need to modify the style of the email content, as if our website had changed its style. If you want to modify it, you can find it (N multiple sub-projects) and replace it one by one. It is also possible that the same mail template is used in N multiple projects, and you have to repeat the work, this is very inconvenient. Therefore, we later thought about how to call WCF to obtain the mail template. The same mail is retrieved from the WCF segment, and the mail data is filled and sent. This involves a question: whether to let the client parse the template or let the server parse it. Obviously, it is better to let the server parse it. The client only needs to pass the relevant data source to WCF, the actual email content can be returned after the WCF Resolution, and the template engine and the WCF mechanism are consistent in some aspects. It also needs the corresponding data format for resolution. We all know (I learned later) that WCF cannot pass the object type, and the object type required by the template engine also needs data format. There are many templates. It is a new problem to let the engine know which data format to parse the template.

After the boss mentioned this, we finally decided to use the reflection mechanism to complete this function, that is, the client sends XML data to the server (the data source is parsed), and then compiles the database into a DLL file and submits it to WCF, this gives WCF a path to search, so my strength today is to implement this function.

It's just to achieve this process, so I am useless to use WCF, just focus on the areas that need the most attention, first, let's take a look at the XML data that my simulated client sends to the server (this project is saved directly in the XML file ).

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Root  >    <  Student  ID  = "1"  Name  = "Eric. Z. Zhou"  Sex  = "Male"  Age = "26"   />    <  Student  ID  = "2"  Name  = "Tom"  Sex  = "Male"  Age  = "32"   />    <  Student  ID  = "3" Name  = "Jack"  Sex  = "Male"  Age  = "28"   />    <  Student  ID  = "4"  Name  = "Tank"  Sex  = "Male"  Age  = "29"  />  </  Root  > 
The related DLL file code is as follows:
 Using  System. Collections. Generic;  Using  System. xml. serialization;  Namespace  Mydll {[xmlroot (  "  Root  " , Isnullable = False  )] Public   Class  Root {[xmlelement (  "  Student  "  )]  Public List <student> Student { Get ; Set  ;}}  Public   Class  Student {[xmlattribute (  " ID  "  )]  Public   Int Id { Get ; Set  ;} [Xmlattribute (  "  Name  "  )]  Public   String Name { Get ; Set ;} [Xmlattribute (  "  Sex  "  )]  Public   String Sex { Get ; Set  ;} [Xmlattribute (  "  Age  "  )]  Public   Int Age {Get ; Set  ;}}} 

Compile this class to generate the DLL file and submit it to the server. Specify the dll path and data source type (namespace + classname), and save the above XML file to a directory on the server, I use the console application simulation here, so save it in the bin directory and check how the server parses it:

 Using  System;  Using  System. IO;  Using  System. reflection;  Using  System. xml. LINQ;  Using  System. xml. serialization; Namespace  Reflection application instance V2 {  Class  Program {  Static   Void Main ( String  [] ARGs ){  String Xmlstring = Loadfromfile (); console. writeline (xmlstring );  String Dllpath = @"  \ DLLs \  " ;  String Dllname = "  Mydll. dll  "  ; Console. writeline (  "  Dllpath: {0} \ n dllname: {1}  "  , Dllpath, dllname );  String Basedirectory = Appdomain. currentdomain. basedirectory; console. writeline (  "  Dll path to be reflected: {0} " , Basedirectory + dllpath + Dllname); Assembly = Assembly. loadfrom (basedirectory + dllpath + Dllname); Type type = Assembly. GetType ( "  Mydll. Root  " ); //  Obtain type              Object OBJ = System. activator. createinstance (type); xmlserializer Ser = New Xmlserializer (type );  Using (Textreader reader = New  Stringreader (xmlstring )){  Object O = Ser. deserialize (Reader);} console. readkey ();}  Private   Static   String  Loadfromfile () {xelement XML = Xelement. Load (appdomain. currentdomain. basedirectory + " /Resource/students. xml  "  );  Return  XML. tostring ();}}} 
Result

In this way, the WCF client obtains a data source with a format, and then submits it to the template for parsing and processing. I started to be curious. Does the reflected object type have the original structure? So I followed up and looked at the figure:

It has a format. In fact, here, I 'd better take the template engine for verification to reach the final conclusion, but the template engine I wrote in the companyNveloctiyThe help class does not have a home, and I am too lazy to write it, so this step will be verified by the company.

This test ends now.

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.