Example #1
0
 private CompileResult CompileCode(string[] references)
 {
     DynamicAssembly assembly = new DynamicAssembly("AnyAssemblyName", references);
     assembly.CustomClasses.AddNew(codeEditorControl1.Text);
     CompileResult result = CSharpCompiler.CompileDynamicAssembly(assembly);
     return result;
 }
Example #2
0
 private void codeEditorControl1_CompileRequest(CodeEditorControl sender, CompileRequestEventArgs args)
 {
     codeEditorControl1.ErrorPanelSettings.Header = "Compile...";
     DynamicAssembly assembly = new DynamicAssembly("TestAssembly", codeEditorControl1.References.ToStringPathArray());
     assembly.CustomClasses.AddNew(codeEditorControl1.Text);
     CompileResult result = CSharpCompiler.CompileDynamicAssembly(assembly);
     codeEditorControl1.ShowErrors(result.Errors, "Sucseed");
 }
Example #3
0
 /// <summary>s
 /// Creates an instance of the class
 /// </summary>
 internal DynamicClass(DynamicAssembly parent)
 {
     Parent     = parent;
     Usings     = new DynamicUsingsCollection();
     Interfaces = new DynamicInterfaceCollection();
     Properties = new DynamicPropertyCollection();
     Methods    = new DynamicMethodCollection();
 }
Example #4
0
 /// <summary>s
 /// Creates an instance of the class
 /// </summary>
 internal DynamicClass(DynamicAssembly parent)
 {
     Parent = parent;
     Usings = new DynamicUsingsCollection();
     Interfaces = new DynamicInterfaceCollection();
     Properties = new DynamicPropertyCollection();
     Methods = new DynamicMethodCollection();            
 }
Example #5
0
        /// <summary>
        /// Creates an Assembly from DynamicAssembly definition
        /// </summary>
        /// <param name="assemblyDefinition">assembly description</param>
        /// <returns>result with assembly or error info</returns>
        public static CompileResult CompileDynamicAssembly(DynamicAssembly assemblyDefinition)
        {
            CodeDomProvider    codeDomProvider    = CodeDomProvider.CreateProvider("CSharp");
            CompilerParameters compilerParameters = new CompilerParameters();

            compilerParameters.CompilerOptions = "/t:library /platform:anycpu /lib:" + "\"" +
                                                 (String.IsNullOrWhiteSpace(assemblyDefinition.ReferencesPath) ? GetCurrentPath() : assemblyDefinition.ReferencesPath) + "\"";
            compilerParameters.IncludeDebugInformation = false;
            //compilerParameters.OutputAssembly = assemblyDefinition.Name;
            compilerParameters.GenerateInMemory   = true;
            compilerParameters.GenerateExecutable = false;

            foreach (var item in assemblyDefinition.References)
            {
                compilerParameters.ReferencedAssemblies.Add(item);
            }

            List <string> codeModules = new List <string>();

            foreach (DynamicClass item in assemblyDefinition.Classes)
            {
                string code = DynamicClass.Template;
                code = DynamicClass.AddUsingsToTemplate(item, code);
                code = DynamicClass.AddInterfacesToTemplate(item, code);
                code = DynamicClass.AddNamespaceToTemplate(String.IsNullOrWhiteSpace(item.Namespace) ? assemblyDefinition.Name : item.Namespace, code);
                code = DynamicClass.AddNameToTemplate(item, code);
                code = DynamicClass.AddPropertiesToTemplate(item, code);
                code = DynamicClass.AddMethodsToTemplate(item, code);
                codeModules.Add(code);
            }

            foreach (DynamicCustomClass item in assemblyDefinition.CustomClasses)
            {
                codeModules.Add(item.Code);
            }

            // we dont allow empty class definitions(fun fact: its okay for the c# compiler)
            foreach (DynamicCustomClass item in assemblyDefinition.CustomClasses)
            {
                if (String.IsNullOrWhiteSpace(item.Code))
                {
                    CompilerErrorCollection collection  = new CompilerErrorCollection();
                    CompilerError           customError = new CompilerError("CustomClass", 0, 0, "Custom", "Unable to compile an empty code module.");
                    collection.Add(customError);
                    return(new CompileResult(codeModules.ToArray(), collection, null));
                }
            }

            CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromSource(compilerParameters, codeModules.ToArray());

            codeDomProvider.Dispose();

            return(new CompileResult(codeModules.ToArray(), compilerResults.Errors, compilerResults.Errors.Count > 0 ? null : compilerResults.CompiledAssembly));
        }
Example #6
0
        /// <summary>
        /// Creates an Assembly from DynamicAssembly definition
        /// </summary>
        /// <param name="assemblyDefinition">assembly description</param>
        /// <returns>result with assembly or error info</returns>
        public static CompileResult CompileDynamicAssembly(DynamicAssembly assemblyDefinition)
        {
            CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp");
            CompilerParameters compilerParameters = new CompilerParameters();
            compilerParameters.CompilerOptions = "/t:library /platform:anycpu /lib:" + "\"" + 
                (String.IsNullOrWhiteSpace(assemblyDefinition.ReferencesPath) ? GetCurrentPath() : assemblyDefinition.ReferencesPath) + "\"";
            compilerParameters.IncludeDebugInformation = false;
            //compilerParameters.OutputAssembly = assemblyDefinition.Name;
            compilerParameters.GenerateInMemory = true;
            compilerParameters.GenerateExecutable = false;   
            
            foreach (var item in assemblyDefinition.References)
                compilerParameters.ReferencedAssemblies.Add(item);

            List<string> codeModules = new List<string>();
            foreach (DynamicClass item in assemblyDefinition.Classes)
            {
                string code = DynamicClass.Template;
                code = DynamicClass.AddUsingsToTemplate(item, code);
                code = DynamicClass.AddInterfacesToTemplate(item, code);
                code = DynamicClass.AddNamespaceToTemplate(String.IsNullOrWhiteSpace(item.Namespace) ? assemblyDefinition.Name : item.Namespace, code);
                code = DynamicClass.AddNameToTemplate(item, code);
                code = DynamicClass.AddPropertiesToTemplate(item, code);
                code = DynamicClass.AddMethodsToTemplate(item, code);
                codeModules.Add(code);
            }

            foreach (DynamicCustomClass item in assemblyDefinition.CustomClasses)
                codeModules.Add(item.Code);

            // we dont allow empty class definitions(fun fact: its okay for the c# compiler)
            foreach (DynamicCustomClass item in assemblyDefinition.CustomClasses)
            {
                if (String.IsNullOrWhiteSpace(item.Code))
                {
                    CompilerErrorCollection collection = new CompilerErrorCollection();
                    CompilerError customError = new CompilerError("CustomClass", 0, 0, "Custom", "Unable to compile an empty code module.");
                    collection.Add(customError);
                    return new CompileResult(codeModules.ToArray(), collection, null);
                }
            }

            CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromSource(compilerParameters, codeModules.ToArray());
            codeDomProvider.Dispose();

            return new CompileResult(codeModules.ToArray(), compilerResults.Errors, compilerResults.Errors.Count > 0 ? null : compilerResults.CompiledAssembly);
        }
Example #7
0
 /// <summary>
 /// Creates an instance of the class
 /// </summary>
 /// <param name="parent">Parent assembly definition</param>
 internal DynamicClassCollection(DynamicAssembly parent)
 {
     Parent = parent;
 }