Click here to Skip to main content
15,921,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used java script in windows application. but i got error at
this.result = provider.CompileAssemblyFromSource  
 i.e  Object reference not set to an instance of an object

how to do it?
C#
 CodeDomProvider provider;
       CompilerParameters para;
       CompilerResults result;
       Assembly assembly;
       Type jsType;
       object jsObject;


public  string Message;

        public void ShowMessage()
        {
            MessageBox.Show(this.Message);
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            this.para = new CompilerParameters();
            this.para.GenerateInMemory = true;

            string text =
                    @"class JSSample
                            {
                                var obj;
                                function Main()
                                {
                                    obj.Message = 'Hello world!';
                                    obj.ShowMessage();
                                }
                            }";

            this.result = provider.CompileAssemblyFromSource(this.para, text);  // I got error here

            this.assembly = result.CompiledAssembly;

            this.jsType = assembly.GetType("JSSample");

            this.jsObject = Activator.CreateInstance(this.jsType);

            this.jsType.InvokeMember(
                "obj",
                BindingFlags.SetField,
                null,
                this.jsObject,
                new object[] { this });

            this.jsType.InvokeMember(
                "Main",
                BindingFlags.InvokeMethod,
                null,
                this.jsObject,
                null);
        }
    }
Posted
Updated 26-Aug-12 23:22pm
v2

1 solution

The CodeDomProvider is meant to be used for compiled jscript files, this is not "javascript" as it is compiled to .net and uses the CLR.

If you want a javascript engine within your application try using one of the following:
http://jint.codeplex.com/[^]

http://javascript.codeplex.com/[^]

http://jurassic.codeplex.com/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900