Mgen object 214: Convert TXT to exe

Source: Internet
Author: User
Tags emit visual studio 2010

Directory

  • 1. Use
  • 2. Download the program and source code
  • 3. Technical Points

 

 

Returned directory

1. Use

Easy to use:

 

Enter the path of the TXT text file to be converted, and then save the EXE path. Configure the text, color, program title, and other options ......

Finally, save the file and an EXE file is generated.

 

Double-click it to open it:

 

A configuration-based exe will display the TXT content. Haha.

 

 

 

Returned directory

2. Download the program and source code

 

Download the current version of the program and source code

Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.

Running Environment:. NET Framework 4.0 client profile

Source code environment: Visual Studio 2010

 

 

Returned directory

3. Technical Points

The program is written in WPF. Of course, the EXE file generated by txt is also a WPF program. The program logic is very simple, that is, the Reflection emit is used to automatically generate a WPF program.

 

Reflection emit:

  • . Net (C #): Reflection emit to generate WPF Program
  • More of these articles: http://www.cnblogs.com/mgen/tag/Emit/

Interface:

  • WPF pure XAML supports system font and color selection for previewing ComboBox

 

However, there are still some things to note: When writing colors, because the colors are dynamic (depending on user input), a local variable is first stored in ilgenerator to store a brushconverter in WPF. Then call brushconverter. convertfromstring to obtain the dynamic color (the brush type in WPF) when reflecting the emit generation code ). Finally, use the castclass command in Il to convert the object returned by the typeconverter. convertfromstring Method to the desired brush object.

// + Using system. reflection;

// + Using system. reflection. emit;

// Return the solidcolorbrush with the specified name in the current execution stack

// Conv stores a brushconverter variable in ilgenerator.

Static void genbrush (ilgenerator ilgen, string name, localbuilder Conv)

{

Ilgen. emit (Opcodes. ldloc, Conv );

Ilgen. emit (Opcodes. ldstr, name );

// Call brushconverter. convertfromstring

Ilgen. emit (Opcodes. callvirt,

Typeof (brushconverter). getmethod ("convertfromstring", new type [] {typeof (string )}));

// Convert the object type to the brush type

Ilgen. emit (Opcodes. castclass, typeof (Brush ));

 

}

 

The second is that the main functions of UI programs such as WPF need the stathreadattribute feature. In this case, the setcustomattribute method of customattributebuilder and assemblybuilder must be used to add custom features when emit is generated.

Code:

// + Using system. reflection;

// + Using system. reflection. emit;

 

// Create assemblybuilder, modulebuilder, typebuilder, and methodbuilder.

VaR assbuilder = appdomain. currentdomain. definedynamicassembly (

New assemblyname (assname ),

Assemblybuilderaccess. runandsave );

VaR modbuilder = assbuilder. definedynamicmodule (assname, filename );

VaR typebuilder = modbuilder. definetype ("program ");

VaR methodbuilder = typebuilder. definemethod (

"Main ",

Methodattributes. Static | methodattributes. hidebysig,

Callingconventions. Standard,

Null,

New Type [0]);

 

// Set stathreadattribute

VaR customattrbuilder = new customattributebuilder (

Typeof (stathreadattritor). getconstructor (New Type [0]), new object [0]);

Methodbuilder. setcustomattribute (customattrbuilder );

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.