Tuesday, February 2, 2010

How to use java script in win application in C#.net

//posted by anil jain

using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;

CodeDomProvider p;
CompilerParameters cp;
CompilerResults cr;
Assembly asmbly;
Type Type;
object Obj;
string javascript = //suppose I have a API for label1.
@"class a
{
var label1='Hi dear';
}";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
p = (CodeDomProvider)Activator.CreateInstance("Microsoft.JScript", "Microsoft.JScript.").Unwrap();
}

private void button1_Click(object sender, EventArgs e)
{
this.para = new CompilerParameters();
this.cp.GenerateInMemory = true;
this.cr = provider.CompileAssemblyFromSource(this.cp, javascript);
this.asmbly = cr.CompiledAssembly;
this.Type = assembly.GetType("a");
this.Obj = Activator.CreateInstance(this.Type);
object retValue = this.Type.InvokeMember("label1", BindingFlags.GetField, null, this.Obj, null);
label1.Text = retValue.ToString();
}

No comments:

Post a Comment