C # dynamically create a type during running,

Source: Internet
Author: User

C # dynamically create a type during running,

C # The dynamic creation type at runtime. Here, the C # source code is dynamically generated, and then the dynamic creation type is implemented by compiling the compiler into an assembly.

Public static Assembly NewAssembly () {// create a compiler instance. Provider = new CSharpCodeProvider (); // set the compilation parameters. Cp = new CompilerParameters (); cp. generateExecutable = false; cp. generateInMemory = true; // Generate an executable instead of // a class library. // cp. generateExecutable = true; // Set the assembly file name to generate. cp. outputAssembly = "c: \ 1.dll"; // Generate debug information. cp. includeDebugInformation = true; // Save the assembly as a physical file. cp. generateInMemory = false; // Set th E level at which the compiler // shocould start displaying warnings. cp. warningLevel = 3; // Set whether to treat all warnings as errors. cp. treatWarningsAsErrors = false; // Set compiler argument to optimize output. cp. compilerOptions = "/optimize"; cp. referencedAssemblies. add ("System. dll "); // cp. referencedAssemblies. add ("System. core. dll "); cp. referencedAssemblies. add ("System. data. dll "); // cp. refer EncedAssemblies. add ("System. data. dataSetExtensions. dll "); cp. referencedAssemblies. add ("System. deployment. dll "); cp. referencedAssemblies. add ("System. design. dll "); cp. referencedAssemblies. add ("System. drawing. dll "); cp. referencedAssemblies. add ("System. windows. forms. dll "); // create dynamic code. StringBuilder classSource = new StringBuilder (); classSource. append ("using System; using System. windows. forms; \ npublic class DynamicClass: UserControl \ n "); classSource. append ("{\ n"); classSource. append ("public DynamicClass () \ n {\ nInitializeComponent (); \ nConsole. writeLine (\ "hello \") ;}\ n "); classSource. append ("private System. componentModel. IContainer components = null; \ nprotected override void Dispo Se (bool disposing) \ n {\ n "); classSource. Append (" if (disposing & (components! = Null) {components. dispose ();} base. dispose (disposing); \ n} \ n "); classSource. append ("private void InitializeComponent () {\ nthis. suspendLayout (); this. autoScaleDimensions = new System. drawing. sizeF (6F, 12F); "); classSource. append ("this. autoScaleMode = System. windows. forms. autoScaleMode. font; this. name = \ "DynamicClass \"; this. size = new System. drawing. size (112, 74); this. resumeLayout (false); \ n} "); // create a genus. ******** * *********************/ClassSource. append (propertyString ("aaa"); classSource. append (propertyString ("bbb"); classSource. append (propertyString ("ccc"); classSource. append ("}"); System. diagnostics. debug. writeLine (classSource. toString (); // compile the code. CompilerResults result = provider. compileAssemblyFromSource (cp, classSource. toString (); if (result. errors. count> 0) {for (int I = 0; I <result. errors. count; I ++) Console. writeLine (result. errors [I]); Console. writeLine ("error"); return null;} // gets the compiled assembly. Assembly assembly = result. compiledAssembly; return assembly;} private static string propertyString (string propertyName) {StringBuilder sbProperty = new StringBuilder (); sbProperty. append ("private int _" + propertyName + "= 0; \ n"); sbProperty. append ("public int" + "+ propertyName +" \ n "); sbProperty. append ("{\ n"); sbProperty. append ("get {return _" + propertyName + ";}\ n"); sbProperty. append ("set {_" + propertyName + "= value ;}\ n"); sbProperty. append ("}"); return sbProperty. toString ();}

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

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.