Just looking for the solution where I want to add the c# script to the cache after compilation so that it can be accessed at any time when required.
Class which holds the cache details.
Public Class cCache
{
public string sSessionID{get;set;}
public string sName{get;set;}
public string sValue{get;set;}
}
C# script code used:
public static Run(string sName)
{
"Console.WriteLine(sName);"
}
When the button is clicked the method is called in the repository where it calls the another method with the script as the parameter to be complied and returns back the result in "MethodInfo" reflection. The script in the method info is invoked with parameter for the script and executed.
Code:
public string Button_CLick(string sScript)
{
MethodInfo methodInfo = WriteMethod(sScript);
cCache cChe= new cCache();
cChe.sSessionID="XYZ";
cChe.sName="Script";
cChe.sValue=MethodInfo.ToString();
if (methodInfo != null)
{
oResult = methodInfo.Invoke(null, new object[] { sName });
}
}
public MethodInfo WriteMethod(string sScript)
{
string sCode= @" using System;
namespace Scripting
{
public class AddScript
{
" + sScript + @"
}
}
";
CompilerParameters loParameters = new CompilerParameters();
loParameters.ReferencedAssemblies.Add("System.dll");
loParameters.GenerateInMemory = false;
ICodeCompiler provider = new CSharpCodeProvider().CreateCompiler();
CompilerResults results = provider.CompileAssemblyFromSource(loParameters, sCode);
if (results.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
throw new InvalidOperationException(sb.ToString());
}
Type binaryFunction = results.CompiledAssembly.GetType("Scripting.AddScript");
return binaryFunction.GetMethod(Run);
}
The above code is working fine but before invoking the script method I want to add the complied script to the cache and then invoke or call it with parameter when ever required as the class property is string I am getting error when converting it to string, so please anyone can help me to sort this error.
Aucun commentaire:
Enregistrer un commentaire