Combining Ajax and reflection refrection to implement the function body of the page without brush

Source: Internet
Author: User
Tags add object implement net reflection return string tostring
ajax| Functions | page | execute

In fact, there is no specific purpose to do this thing, just see this post and this blog later interested, suddenly it would like to try to do an online compiler, incidentally also research Ajax and reflection;

Ajax has been watching for a long time, and is more concerned about the developments of Microsoft's development package Atlas, but also has a lot of resources:

· MSDN's Atlas Web site

· "Atlas" official website

· "Atlas" preview document

· Atlas Quick Start Tutorial

The above several connections from Shi's blog, it seems he is also very concerned about the technology;

In front for a long time, there was an AJAX development package, in the author's blog can often see the development package of the dynamic, has been determined to get it to the project to try, since the technology is still not clear, there is not very familiar JavaScript, and is said to be inefficient development, Dare not take the risk, this time in two projects idle period, just good to see;

Nonsense said a lot, or to see the realization of it:

First, configure the web.config
1. Add the following code in the <configuration></configuration> section:

<sectiongroup name= "Ajaxnet" >
<section name= "ajaxsettings" type= "Ajaxpro.ajaxsettingssectionhandler,ajaxpro"/>

<encryption crypttype= "Ajaxprosample.samplecryptprovider, ajaxprosample" keytype= " Ajaxprosample.samplekeyprovider, Ajaxprosample "/>
<token enabled= "true"/>
<add namespace= "ajaxreflection.ajaxreflect,ajaxreflection" path= "MyPath"/>
2. Add in <system.web></system.web> section: <add verb= "*" path= "ajaxpro/*.ashx" type= "Ajaxpro.ajaxhandlerfactory,ajaxpro"/>
Then, write reflection implementation code, there are a lot of questions not to consider, experts have a view to mention, the following on the reflection of the code basically stolen from the Ninputer, made a point to change, hope not to cause the original author's criticism

Using System;
Using System.Text;
Using Microsoft.csharp;
Using System.Reflection;
Using System.Reflection.Emit;
Using System.CodeDom.Compiler;
Using System.Threading;

Namespace Ajaxreflection
Summary description of the Reflectionmethod.
public sealed class Reflectionmethod
private static CSharpCodeProvider comp = new CSharpCodeProvider ();

private static CompilerParameters CP = new CompilerParameters ();

private static MethodInfo mi;

Execute function Get Results
<param name= "methodname" > Function name </param>
<param name= "Methodcode" > Code snippet </param>
public static Object Executemethod (String methodname,string methodcode)
StringBuilder sb = new StringBuilder ();
Sb. Append ("Using system;\t\n");
Sb. Append ("namespace nsreflection{\t\n");
Sb. Append ("Public sealed class class" +methodname+ "{\t\n");
Sb. Append ("public static Object" +methodname+ "() {\t\n");
Sb. Append (Methodcode);
Sb. Append ("}\t\n}\t\n}\t\n");

Cp. Referencedassemblies.add ("System.dll");
Cp. GenerateExecutable = false;
Cp. GenerateInMemory = true;

CompilerResults CR = Comp. CreateCompiler (). CompileAssemblyFromSource (CP,SB. ToString ());

CompilerErrorCollection cec = cr. Errors;

if (CEC). HasErrors)
throw new ArgumentOutOfRangeException ("Methodname:methodcode", "The Code given [" +SB. ToString () + "] cannot be compiled, please check and modify and try again!" ");
String strerror = String. Empty;

foreach (CompilerError CE in CEC)
Strerror + = "Code has the following error during compilation:" +ce. Errortext+ce. ToString () + "error in row" +ce. Line.tostring ();

return strerror;
Assembly a = cr.compiledassembly;

Type t = a.gettype ("Nsreflection.class" +methodname,true,true);
Mi = T.getmethod (methodname, BindingFlags.Static | BindingFlags.Public);
Return MI. Invoke (NULL, new object[0]);

And then implement the middle tier, very simple call
Using System;

Namespace Ajaxreflection
Summary description of the Ajaxmethod.
public class Ajaxmethodref
public string Execute (string code)
Code = code. Replace ("#", "+");

Object Oresult = Reflectionmethod.executemethod ("MyTest", code);

Return String.Format ("returns the result type; { 0};\t\n value is; {1} ", Oresult.gettype (). ToString (), oresult.tostring ());
The last is the page end of the two small sentence code, and a JS file
1. Add this sentence on the Page_Load of the page.
AjaxPro.Utility.RegisterTypeForAjax (typeof (Ajaxmethodref)); 2. Save the following JavaScript script as Ajaxjscript.js
var Code;

function Getexecuteresult ()
Code = document.getElementById ("Tbcode"). Value;

Replace ("+", "#");

Alert (AjaxReflection.AjaxMethodRef.Execute (Code). value);

function Replace (oldsub,newsub)
var index = code.indexof (oldsub);

if (index==-1)
return Code;
Code = code.substring (0,index) +newsub+code.substring (index+1,code.length);
Replace (oldsub,newsub);

Here are a few depressing questions, I do not know is the problem of the program, or Ajaxpro, edit the code string incredibly uploaded to the server will be the + number removed, I had to in the JS in advance for the #, do not fish and fowl, which day must take a good look at the implementation of Ajaxpro code, Find out the reason to see!

Here is a small run effect chart:

above code Download
Ajaxpro Download

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: 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.