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
· asp.net "Atlas" official website
· asp.net "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:
<configSections>
<sectiongroup name= "Ajaxnet" >
<section name= "ajaxsettings" type= "Ajaxpro.ajaxsettingssectionhandler,ajaxpro"/>
</sectionGroup>
</configSections>
<ajaxNet>
<ajaxSettings>
<!--
<encryption crypttype= "Ajaxprosample.samplecryptprovider, ajaxprosample" keytype= " Ajaxprosample.samplekeyprovider, Ajaxprosample "/>
<token enabled= "true"/>
-->
<urlNamespaceMappings>
<add namespace= "ajaxreflection.ajaxreflect,ajaxreflection" path= "MyPath"/>
</urlNamespaceMappings>
</ajaxSettings>
</ajaxNet>
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>
Summary description of the Reflectionmethod.
</summary>
public sealed class Reflectionmethod
{
private static CSharpCodeProvider comp = new CSharpCodeProvider ();
private static CompilerParameters CP = new CompilerParameters ();
private static MethodInfo mi;
/**////<summary>
Execute function Get Results
</summary>
<param name= "methodname" > Function name </param>
<param name= "Methodcode" > Code snippet </param>
<returns></returns>
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;
}
Else
{
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>
Summary description of the Ajaxmethod.
</summary>
public class Ajaxmethodref
{
[Ajaxpro.ajaxmethod]
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;
}
Else
{
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