Two development solutions for. NET projects

Source: Internet
Author: User
Tags reflection tostring

The company's original project two development methods mainly using SQL, basically can also meet customer requirements, the advantage is simple to use, as long as familiar with SQL statements can be manipulated, the disadvantage is that too many restrictions, the need for the database at the bottom of the understanding, easy to use error, can not directly invoke business layer code, Research on the dynamic compilation of. NET, feel it to do two times the development effect should be good.

First we do a demo to explain the dynamic compilation, the following code means to organize a source string, and then compile execution.

Dynamically compiling simple code

 using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.csharp;
Namespace ConsoleApplication6
{
Class program
{
//c# code provider
private static Codedo Mprovider comp = new CSharpCodeProvider ();
//parameter to invoke compiler
private static compilerparameters CP = new CompilerParameters ();
private static MethodInfo mi;
static void Main (string[] args)
{
StringBuilder codebuilder = new StringBuilder (); codebuilder.appendline ("Using System;");
Codebuilder.appendline ("public class MyClass");
Codebuilder.appendline ("{");
Codebuilder.appendline ("public void Hello ()");
Codebuilder.appendline ("{");
Codebuilder.appendline ("Console.WriteLine" ("hello\"); ");
Codebuilder.appendline ("}");
Codebuilder.appendline ("}");
//Join the assembly that needs to be referenced
CP. Referencedassemblies.add ("System.dll");
CompilerResults cr = Comp. CompileAssemblyFromSource (CP, codebuilder.tostring ());
//If there is a compilation error
if (CR. Errors.haserrors)
{
foreach (CompilerError item in CR. Errors)
{
Console.WriteLine (item. ToString ());
}
}
Else
{
Assembly a = cr. compiledassembly; Gets the compiled assembly
Type t = a.gettype ("MyClass"); Using reflection to get the type
Object mode = A.createinstance ("MyClass");
Mi = t.getmethod ("Hello", BindingFlags.Instance | BindingFlags.Public);
mi. Invoke (mode, new object[0]); Execute method
}
}
}
}

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.