public void Run() { var compileUnit = new CodeCompileUnit(); var myNamespace = new CodeNamespace("MyNamespace"); myNamespace.Imports.Add(new CodeNamespaceImport("System")); var myClass = new CodeTypeDeclaration("MyClass"); var start = new CodeEntryPointMethod(); var cs1 = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World!")); compileUnit.Namespaces.Add(myNamespace); myNamespace.Types.Add(myClass); myClass.Members.Add(start); start.Statements.Add(cs1); var provider = new CSharpCodeProvider(); using (var sw = new StreamWriter("HelloWorld.cs", false)) { var tw = new IndentedTextWriter(sw, " "); provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions()); tw.Close(); } }
static void AddEntryPoint() { CodeEntryPointMethod start = new CodeEntryPointMethod(); CodeObjectCreateExpression objectCreate = new CodeObjectCreateExpression( new CodeTypeReference("CodeDOMCreatedClass"), new CodePrimitiveExpression(5.3), new CodePrimitiveExpression(6.9)); // Add the statement: // "CodeDOMCreatedClass testClass = // new CodeDOMCreatedClass(5.3, 6.9);" start.Statements.Add(new CodeVariableDeclarationStatement( new CodeTypeReference("CodeDOMCreatedClass"), "testClass", objectCreate)); // Creat the expression: // "testClass.ToString()" CodeMethodInvokeExpression toStringInvoke = new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("testClass"), "ToString"); // Add a System.Console.WriteLine statement with the previous // expression as a parameter. start.Statements.Add(new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("System.Console"), "WriteLine", toStringInvoke)); targetClass.Members.Add(start); }
public CodeNamespace CreateCodeHelloDemo() { var method = new CodeMemberMethod(); method.Name = "SayHello"; method.Attributes = MemberAttributes.Public | MemberAttributes.Final; method.ReturnType = new CodeTypeReference(typeof(string)); method.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression("Hello From Code!"))); CodeEntryPointMethod main = new CodeEntryPointMethod(); main.Statements.Add(new CodeVariableDeclarationStatement("HelloWord", "hw", new CodeObjectCreateExpression("HelloWord", new CodeExpression[] { }))); CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("hw"), "SayHello", new CodeExpression[] { }); main.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"),"WriteLine",methodInvoke)); main.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"), "Read")); CodeTypeDeclaration helloword=new CodeTypeDeclaration("HelloWord"); helloword.Attributes=MemberAttributes.Public; helloword.Members.AddRange(new CodeTypeMember[]{method,main}); CodeNamespace nspace=new CodeNamespace("HelloDemo1"); nspace.Imports.Add(new CodeNamespaceImport("System")); nspace.Types.Add(helloword); return nspace; }
/// <summary> /// This is a simple helper method to create a test valid /// CodeObject to be feed into the /// CreateScriptSource(CodeObject content, string id) /// /// However, CodeObject is simply a base class so we probably /// need a specific type of CodeObject but what? /// /// [Bill - has indicate that CodeObject parameter for CreateScriptSource /// does in fact need to be a CodeMemberMethod - Maybe a spec BUG] /// /// Probably need to put this somewhere else maybe put this in: /// ScriptEngineTestHelper.cs /// </summary> /// <returns>A valid CodeObject boxes some kind of CompileUnit</returns> private static CodeObject BuildCountCode() { // Create a new CodeCompileUnit to contain // the program graph. CodeCompileUnit compileUnit = new CodeCompileUnit(); // Declare a new namespace called Samples. CodeNamespace samples = new CodeNamespace("Samples"); // Add the new namespace to the compile unit. compileUnit.Namespaces.Add(samples); // Declare a new code entry point method. CodeEntryPointMethod start = new CodeEntryPointMethod(); CodeConstructor codecon = new CodeConstructor(); // CodeNamespace cns = new CodeNamespace("Test"); CodeTypeDeclaration ctd = new CodeTypeDeclaration("testclass"); ctd.IsClass = true; CodeMemberMethod method = new CodeMemberMethod(); method.Attributes = MemberAttributes.Public | MemberAttributes.Final; method.Name = "AddTenToNumber"; method.ReturnType = new CodeTypeReference("Int32"); method.Parameters.Add(new CodeParameterDeclarationExpression("int", "number")); method.Statements.Add(new CodeSnippetExpression("return number+10")); ctd.Members.Add(method); samples.Types.Add(ctd); // If we return just the method this will not throw exception // on CreateScriptSource // return method; return compileUnit; }
static void Main(string[] args) { CodeCompileUnit compileUnit = new CodeCompileUnit(); CodeNamespace myNamespace = new CodeNamespace("MyNamespace"); myNamespace.Imports.Add(new CodeNamespaceImport("System")); CodeTypeDeclaration myClass = new CodeTypeDeclaration("MyClass"); CodeEntryPointMethod start = new CodeEntryPointMethod(); CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World")); compileUnit.Namespaces.Add(myNamespace); myNamespace.Types.Add(myClass); myClass.Members.Add(start); start.Statements.Add(cs1); CSharpCodeProvider provider = new CSharpCodeProvider(); using(StreamWriter sw = new StreamWriter("HelloWorld.cs", false)) { IndentedTextWriter tw = new IndentedTextWriter(sw, " "); provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions()); tw.Close(); } Console.WriteLine("HelloWorld.cs generated in .../bin/Debug or .../bin/Release project folders."); Console.Write("Press a key to exit"); Console.ReadKey(); }
public void CompileGreeter() { Lexer lexer = new Lexer(@" PRINT ""Enter your name: "" name = INPUT() PRINT ""Hello, "" + name + ""!"" "); CodeCompileUnit ccu = new CodeCompileUnit(); CodeNamespace samples = new CodeNamespace("Samples"); samples.Imports.Add(new CodeNamespaceImport("System")); CodeTypeDeclaration targetClass = new CodeTypeDeclaration("CreatedClass"); targetClass.IsClass = true; targetClass.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed; CodeEntryPointMethod start = new CodeEntryPointMethod(); targetClass.Members.Add(start); var token = lexer.Next(); if (token is SubCallToken) { SubCallToken subCallToken = (SubCallToken) token; if (subCallToken.Name == "PRINT") { CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("Console"), "WriteLine", subCallToken.Arguments.Select(ConvertSubCallArgument).ToArray()); start.Statements.Add(cs1); } } samples.Types.Add(targetClass); ccu.Namespaces.Add(samples); CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); CodeGeneratorOptions options = new CodeGeneratorOptions(); options.BracingStyle = "C"; StringWriter writer = new StringWriter(); provider.GenerateCodeFromCompileUnit(ccu, writer, options); string result = writer.ToString(); Console.WriteLine(result); //StringAssert.Contains(@"Console.WriteLine(""Enter your name: "");", result); var compileResults = provider.CompileAssemblyFromDom(new CompilerParameters(), ccu); Assert.IsNotNull(compileResults); Assert.AreEqual(0, compileResults.Errors.Count); Console.WriteLine(compileResults.PathToAssembly); }
/// <summary> /// This function generates the default CodeCompileUnit template /// </summary> public CodeCompileUnit GetCodeCompileUnit(IDesignerHost host) { this.host = host; IDesignerHost idh = (IDesignerHost)this.host.GetService(typeof(IDesignerHost)); root = idh.RootComponent; Hashtable nametable = new Hashtable(idh.Container.Components.Count); ns = new CodeNamespace("DesignerHostSample"); myDesignerClass = new CodeTypeDeclaration(); initializeComponent = new CodeMemberMethod(); CodeCompileUnit code = new CodeCompileUnit(); // Imports ns.Imports.Add(new CodeNamespaceImport("System")); ns.Imports.Add(new CodeNamespaceImport("System.ComponentModel")); ns.Imports.Add(new CodeNamespaceImport("System.Windows.Forms")); code.Namespaces.Add(ns); myDesignerClass = new CodeTypeDeclaration(root.Site.Name); myDesignerClass.BaseTypes.Add(typeof(Form).FullName); IDesignerSerializationManager manager = host.GetService(typeof(IDesignerSerializationManager)) as IDesignerSerializationManager; ns.Types.Add(myDesignerClass); // Constructor CodeConstructor con = new CodeConstructor(); con.Attributes = MemberAttributes.Public; con.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "InitializeComponent"))); myDesignerClass.Members.Add(con); // Main CodeEntryPointMethod main = new CodeEntryPointMethod(); main.Name = "Main"; main.Attributes = MemberAttributes.Public | MemberAttributes.Static; main.CustomAttributes.Add(new CodeAttributeDeclaration("System.STAThreadAttribute")); main.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(System.Windows.Forms.Application)), "Run"), new CodeExpression[] { new CodeObjectCreateExpression(new CodeTypeReference(root.Site.Name)) })); myDesignerClass.Members.Add(main); // InitializeComponent initializeComponent.Name = "InitializeComponent"; initializeComponent.Attributes = MemberAttributes.Private; initializeComponent.ReturnType = new CodeTypeReference(typeof(void)); initializeComponent.Statements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "Text"), new CodePrimitiveExpression(root.Site.Name))); //roman// myDesignerClass.Members.Add(initializeComponent); codeCompileUnit = code; return codeCompileUnit; }
public Script(string namespaceToGen, string className) { this.compileUnit = new CodeCompileUnit(); this.codeNamespace = new CodeNamespace(namespaceToGen); this.compileUnit.Namespaces.Add(this.codeNamespace); this.codeClass = new CodeTypeDeclaration(className); this.codeNamespace.Types.Add(this.codeClass); this.proxySetting = ProxySettings.RequiredHeaders; this.mainMethod = new CodeEntryPointMethod(); this.mainMethod.Name = "Main"; this.mainMethod.Attributes = MemberAttributes.Public | MemberAttributes.Static; this.codeClass.Members.Add(this.mainMethod); this.dumpMethodRef = this.BuildDumper(); }
/// <summary> /// Compile the parse tree to CSharp. /// </summary> /// <returns></returns> private string Compile() { // this.buf = ['var interp']; _buf = new CodeEntryPointMethod(); if (_pp) // _buf.Push("/* var __indent = []; */"); _buf.Statements.Add(new CodeVariableDeclarationStatement("string[]", "__indent")); _lastBufferedIdx = 1; Visit(_node); var gen = new CSharpCodeProvider(); var writer = new StringWriter(); gen.GenerateCodeFromMember(_buf, writer, null); return writer.ToString(); }
static CodeCompileUnit HelloWorldCodeDOM() { CodeCompileUnit compileUnit = new CodeCompileUnit(); CodeNamespace myNamespace = new CodeNamespace("MyNamespace"); myNamespace.Imports.Add(new CodeNamespaceImport("System")); CodeTypeDeclaration myClass = new CodeTypeDeclaration("MyClass"); CodeEntryPointMethod start = new CodeEntryPointMethod(); CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World!")); compileUnit.Namespaces.Add(myNamespace); myNamespace.Types.Add(myClass); myClass.Members.Add(start); start.Statements.Add(cs1); return compileUnit; }
public void Run() { var compileUnit = new CodeCompileUnit(); var myNamespace = new CodeNamespace("MyNamespace"); myNamespace.Imports.Add(new CodeNamespaceImport("System")); var myClass = new CodeTypeDeclaration("MyClass"); var start = new CodeEntryPointMethod(); var cs1 = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World!")); compileUnit.Namespaces.Add(myNamespace); myNamespace.Types.Add(myClass); myClass.Members.Add(start); start.Statements.Add(cs1); }
public CodeCompileUnit GetCompierUnit() { CodeCompileUnit cu = new CodeCompileUnit(); CodeNamespace cn = new CodeNamespace("MyNameSpace"); cn.Imports.Add(new CodeNamespaceImport("System")); CodeTypeDeclaration myclass = new CodeTypeDeclaration("MyClass"); CodeEntryPointMethod method = new CodeEntryPointMethod(); CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World")); cu.Namespaces.Add(cn); cn.Types.Add(myclass); myclass.Members.Add(method); method.Statements.Add(cs1); return cu; }
static void Main(string[] args) { //Create a provider CSharpCodeProvider provider = new CSharpCodeProvider(); //Create a compile unit, root of object graph CodeCompileUnit compileUnit = new CodeCompileUnit(); //The namespace we are goin CodeNamespace nameSpace = new CodeNamespace("CodeDOMExample"); //Import system nameSpace.Imports.Add(new CodeNamespaceImport("System")); //add the namespace to the root graph compileUnit.Namespaces.Add(nameSpace); //program class CodeTypeDeclaration programClass = new CodeTypeDeclaration("Program"); nameSpace.Types.Add(programClass); //Create an entry point for the application CodeEntryPointMethod entryPoint = new CodeEntryPointMethod(); //Create expressions to first write a line and read a key CodeMethodInvokeExpression writeLnExpression = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World!!! Press any key to exit")); CodeMethodInvokeExpression readKeyExpression = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("Console"), "ReadKey",new CodePrimitiveExpression(false)); //add these expressions to the method entryPoint.Statements.Add(writeLnExpression); entryPoint.Statements.Add(readKeyExpression); //Add the entry point programClass.Members.Add(entryPoint); //Save source file SaveSourceFile(provider, compileUnit); //Compile source file GenerateAssembly(provider, compileUnit); Console.ReadKey(); }
//Create main method private void CreateEntryPoint(List<string> statements) { //Create an object and assign the name as "Main" CodeEntryPointMethod mymain = new CodeEntryPointMethod(); mymain.Name = "Main"; //Mark the access modifier for the main method as Public and //static mymain.Attributes = MemberAttributes.Public | MemberAttributes.Static; //Change string to statements if(statements != null){ foreach (string item in statements) { if (item != null) { CodeSnippetExpression exp1 = new CodeSnippetExpression(@item); CodeExpressionStatement ces1 = new CodeExpressionStatement(exp1); mymain.Statements.Add(ces1); } } } myclass.Members.Add(mymain); }
static void Main() { // // Demonstration of client calls to a JSON-RPC service. // JsonRpcClient client = new JsonRpcClient(); client.Url = "http://www.raboof.com/projects/jayrock/demo.ashx"; Console.WriteLine(client.Invoke("system.about")); Console.WriteLine(client.Invoke("system.version")); Console.WriteLine(string.Join(Environment.NewLine, (string[]) (new ArrayList((ICollection) client.Invoke("system.listMethods"))).ToArray(typeof(string)))); Console.WriteLine(client.Invoke("now")); Console.WriteLine(client.Invoke("sum", 123, 456)); Console.WriteLine(client.Invoke("total", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })); client.CookieContainer = new CookieContainer(); Console.WriteLine(client.Invoke("counter")); Console.WriteLine(client.Invoke("counter")); // // Demonstration of a JsonWriter implementation (JsonCodeWriter) // that writes code in terms of itself. So we take some JSON // text and covert it into the JsonWriter API. // CodeEntryPointMethod codeMainMethod = new CodeEntryPointMethod(); codeMainMethod.Statements.Add(new CodeVariableDeclarationStatement(typeof(JsonWriter), "writer", new CodeObjectCreateExpression(typeof(EmptyJsonWriter)))); JsonCodeWriter writer = new JsonCodeWriter(new CodeVariableReferenceExpression("writer"), codeMainMethod.Statements); writer.WriteFromReader(new JsonTextReader(new StringReader(@" { firstName : 'John', lastName : 'Doe' }"))); CodeTypeDeclaration codeType = new CodeTypeDeclaration("Program"); codeType.Members.Add(codeMainMethod); CodeGeneratorOptions options = new CodeGeneratorOptions(); GetLanguageProvider("c#").CreateGenerator().GenerateCodeFromType(codeType, Console.Out, options); GetLanguageProvider("vb").CreateGenerator().GenerateCodeFromType(codeType, Console.Out, options); }
static void Main(string[] args) { CodeCompileUnit compileUnit = new CodeCompileUnit(); CodeNamespace samples = new CodeNamespace("Samples"); samples.Imports.Add(new CodeNamespaceImport("System")); compileUnit.Namespaces.Add(samples); CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1"); samples.Types.Add(class1); CodeEntryPointMethod start = new CodeEntryPointMethod(); /* CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("System.Console"), "WriteLine", new CodePrimitiveExpression("Hello World!")); start.Statements.Add(cs1); */ class1.Members.Add(start); GenerateCSharpCode(compileUnit); }
public static System.CodeDom.CodeCompileUnit BuildGraph() { System.CodeDom.CodeCompileUnit CompileUnit = new System.CodeDom.CodeCompileUnit(); System.CodeDom.CodeNamespace nSpace = new System.CodeDom.CodeNamespace("HelloWorldViaCodeDOM"); CompileUnit.Namespaces.Add(nSpace); nSpace.Imports.Add(new System.CodeDom.CodeNamespaceImport("System")); System.CodeDom.CodeTypeDeclaration clsStartup = new System.CodeDom.CodeTypeDeclaration("Startup"); nSpace.Types.Add(clsStartup); System.CodeDom.CodeEntryPointMethod main = new System.CodeDom.CodeEntryPointMethod(); System.CodeDom.CodePrimitiveExpression exp = new System.CodeDom.CodePrimitiveExpression("Hello World!"); System.CodeDom.CodeTypeReferenceExpression refExp = new System.CodeDom.CodeTypeReferenceExpression("System.Console"); System.CodeDom.CodeMethodInvokeExpression invoke = new System.CodeDom.CodeMethodInvokeExpression(refExp, "WriteLine", exp); main.Statements.Add(new System.CodeDom.CodeExpressionStatement(invoke)); clsStartup.Members.Add(main); return(CompileUnit); }
public void CreateEntryPoint(ref CodeTypeDeclaration customClass) { //Create an object and assign the name as “Main” CodeEntryPointMethod main = new CodeEntryPointMethod(); main.Name = "Main"; //Mark the access modifier for the main method as Public and //static main.Attributes = MemberAttributes.Public | MemberAttributes.Static; //Provide defenition to the main method. //Create an object of the “Cmyclass” and invoke the method //by passing the required parameters. CodeSnippetExpression exp = new CodeSnippetExpression(CallingMethod(customClass)); //Create expression statements for the snippets CodeExpressionStatement ces = new CodeExpressionStatement(exp); //Add the expression statements to the main method. main.Statements.Add(ces); //Add the main method to the class customClass.Members.Add(main); }
static void Main(string[] args) { CodeCompileUnit compileUnit = new CodeCompileUnit(); CodeNamespace myNamespace = new CodeNamespace("TestNamespace"); myNamespace.Imports.Add(new CodeNamespaceImport("System")); CodeTypeDeclaration myClass = new CodeTypeDeclaration("MyClass"); CodeEntryPointMethod start = new CodeEntryPointMethod(); CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("Hello World!")); CodeMemberField memberField = new CodeMemberField(); memberField.Type = new CodeTypeReference(typeof(float)); CodeMemberProperty memberPropery = new CodeMemberProperty(); //memberPropery.ha CodeConditionStatement cond = new CodeConditionStatement(); compileUnit.Namespaces.Add(myNamespace); myNamespace.Types.Add(myClass); myClass.Members.Add(start); start.Statements.Add(cs1); CSharpCodeProvider provider = new CSharpCodeProvider(); using (StreamWriter sw = new StreamWriter("Helloworld.cs", false)) { IndentedTextWriter tw = new IndentedTextWriter(sw, " "); provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions()); tw.Close(); } }
private Assembly GenerateExecutableAssembly(PrologCompilerParameters compilerParameters, ArrayList instructions) { CodeCompileUnit compileUnit = new CodeCompileUnit(); // Declare namespace, default is Prolog.Assembly CodeNamespace plNamespace = new CodeNamespace("Prolog.Assembly"); plNamespace.Imports.Add(new CodeNamespaceImport("System")); plNamespace.Imports.Add(new CodeNamespaceImport("System.Collections")); plNamespace.Imports.Add(new CodeNamespaceImport("Axiom.Runtime")); compileUnit.Namespaces.Add(plNamespace); // Declare class type CodeTypeDeclaration classType = new CodeTypeDeclaration("PrologApp"); plNamespace.Types.Add(classType); classType.TypeAttributes = TypeAttributes.Public; // Declare private members CodeMemberField machineField = new CodeMemberField(new CodeTypeReference("AbstractMachineState"), "machine"); CodeMemberField moreField = new CodeMemberField(new CodeTypeReference("System.Boolean"), "_more"); classType.Members.Add(machineField); classType.Members.Add(moreField); // Generate constructor method CodeConstructor cons = new CodeConstructor(); cons.Attributes = MemberAttributes.Public; cons.Statements.Add(new CodeSnippetExpression("Init()")); classType.Members.Add(cons); // Generate Init() method GenerateInitMethod(classType, instructions); // Generate main method CodeEntryPointMethod mainMethod = new CodeEntryPointMethod(); mainMethod.Name = "Main"; mainMethod.Attributes = MemberAttributes.Static | MemberAttributes.Public; mainMethod.Statements.Add(new CodeSnippetStatement("PrologApp app = new PrologApp();")); mainMethod.Statements.Add(new CodeSnippetStatement("app.Run();")); classType.Members.Add(mainMethod); CodeMemberMethod runMethod = new CodeMemberMethod(); runMethod.Name = "Run"; runMethod.Attributes = MemberAttributes.Public; runMethod.Statements.Add(new CodeSnippetStatement("machine.Call(\"main\");")); classType.Members.Add(runMethod); // Compile the file into a DLL CompilerParameters compparams = new CompilerParameters(new string[] { "mscorlib.dll", "Axiom.Runtime.dll" }); compparams.GenerateInMemory = false; compparams.GenerateExecutable = true; compparams.OutputAssembly = compilerParameters.OutputAssembly; compparams.TempFiles.KeepFiles = true; Microsoft.CSharp.CSharpCodeProvider csharp = new Microsoft.CSharp.CSharpCodeProvider(); ICodeCompiler cscompiler = csharp.CreateCompiler(); CompilerResults compresult = cscompiler.CompileAssemblyFromDom(compparams, compileUnit); if (compresult.Errors.Count > 0) { foreach (CompilerError err in compresult.Errors) { Console.WriteLine(err); } return null; } return compresult.CompiledAssembly; }
public override void Flush() { if (this.dirty) { XmlDocument document = new XmlDocument(); document.AppendChild(document.CreateElement("DOCUMENT_ELEMENT")); IComponent root = this.host.RootComponent; Hashtable nametable = new Hashtable(this.host.Container.Components.Count); document.DocumentElement.AppendChild(this.WriteObject(document, nametable, root)); foreach (IComponent comp in this.host.Container.Components) { if (!((comp == root) || nametable.ContainsKey(comp))) { document.DocumentElement.AppendChild(this.WriteObject(document, nametable, comp)); } } CodeCompileUnit code = new CodeCompileUnit(); CodeNamespace ns = new CodeNamespace(root.Site.Name + "Namespace"); ns.Imports.Add(new CodeNamespaceImport("System")); SampleTypeResolutionService strs = this.host.GetService(typeof(ITypeResolutionService)) as SampleTypeResolutionService; foreach (Assembly assm in strs.RefencedAssemblies) { ns.Imports.Add(new CodeNamespaceImport(assm.GetName().Name)); } RootDesignerSerializerAttribute a = TypeDescriptor.GetAttributes(root)[typeof(RootDesignerSerializerAttribute)] as RootDesignerSerializerAttribute; CodeDomSerializer cds = Activator.CreateInstance(this.host.GetType(a.SerializerTypeName)) as CodeDomSerializer; IDesignerSerializationManager manager = this.host.GetService(typeof(IDesignerSerializationManager)) as IDesignerSerializationManager; CodeTypeDeclaration td = cds.Serialize(manager, root) as CodeTypeDeclaration; CodeConstructor con = new CodeConstructor(); con.Attributes = MemberAttributes.Public; con.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "InitializeComponent"), new CodeExpression[0])); td.Members.Add(con); CodeEntryPointMethod main = new CodeEntryPointMethod(); main.Name = "Main"; main.Attributes = MemberAttributes.Public | MemberAttributes.Static; main.CustomAttributes.Add(new CodeAttributeDeclaration("System.STAThreadAttribute")); main.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Application)), "Run"), new CodeExpression[] { new CodeObjectCreateExpression(new CodeTypeReference(root.Site.Name), new CodeExpression[0]) })); td.Members.Add(main); ns.Types.Add(td); code.Namespaces.Add(ns); this.dirty = false; this.xmlDocument = document; this.codeCompileUnit = code; this.UpdateCodeWindows(); } }
// Create a CodeDOM graph. static void CreateGraph(CodeDomProvider provider, CodeCompileUnit cu) { //<Snippet8> if (!provider.Supports(GeneratorSupport.GenericTypeReference | GeneratorSupport.GenericTypeDeclaration)) { // Return if the generator does not support generics. return; } //</Snippet8> CodeNamespace ns = new CodeNamespace("DemoNamespace"); ns.Imports.Add(new CodeNamespaceImport("System")); ns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic")); cu.Namespaces.Add(ns); // Declare a generic class. CodeTypeDeclaration class1 = new CodeTypeDeclaration(); class1.Name = "MyDictionary"; class1.BaseTypes.Add(new CodeTypeReference("Dictionary", new CodeTypeReference[] { new CodeTypeReference("TKey"), new CodeTypeReference("TValue"), })); //<Snippet2> //<Snippet10> CodeTypeParameter kType = new CodeTypeParameter("TKey"); //</Snippet2> //<Snippet3> kType.HasConstructorConstraint = true; //</Snippet3> //<Snippet4> kType.Constraints.Add(new CodeTypeReference(typeof(IComparable))); //</Snippet4> //<Snippet5> kType.CustomAttributes.Add(new CodeAttributeDeclaration( "System.ComponentModel.DescriptionAttribute", new CodeAttributeArgument(new CodePrimitiveExpression("KeyType")))); //</Snippet5> CodeTypeReference iComparableT = new CodeTypeReference("IComparable"); iComparableT.TypeArguments.Add(new CodeTypeReference(kType)); kType.Constraints.Add(iComparableT); CodeTypeParameter vType = new CodeTypeParameter("TValue"); vType.Constraints.Add(new CodeTypeReference(typeof(IList <System.String>))); vType.CustomAttributes.Add(new CodeAttributeDeclaration( "System.ComponentModel.DescriptionAttribute", new CodeAttributeArgument(new CodePrimitiveExpression("ValueType")))); class1.TypeParameters.Add(kType); class1.TypeParameters.Add(vType); //</Snippet10> ns.Types.Add(class1); //<Snippet6> // Declare a generic method. CodeMemberMethod printMethod = new CodeMemberMethod(); CodeTypeParameter sType = new CodeTypeParameter("S"); sType.HasConstructorConstraint = true; CodeTypeParameter tType = new CodeTypeParameter("T"); sType.HasConstructorConstraint = true; printMethod.Name = "Print"; printMethod.TypeParameters.Add(sType); printMethod.TypeParameters.Add(tType); //</Snippet6> //<Snippet7> printMethod.Statements.Add(ConsoleWriteLineStatement( new CodeDefaultValueExpression(new CodeTypeReference("T")))); printMethod.Statements.Add(ConsoleWriteLineStatement( new CodeDefaultValueExpression(new CodeTypeReference("S")))); //</Snippet7> printMethod.Attributes = MemberAttributes.Public; class1.Members.Add(printMethod); CodeTypeDeclaration class2 = new CodeTypeDeclaration(); class2.Name = "Demo"; CodeEntryPointMethod methodMain = new CodeEntryPointMethod(); CodeTypeReference myClass = new CodeTypeReference( "MyDictionary", new CodeTypeReference[] { new CodeTypeReference(typeof(int)), new CodeTypeReference("List", new CodeTypeReference[] { new CodeTypeReference("System.String") }) }); methodMain.Statements.Add( new CodeVariableDeclarationStatement(myClass, "dict", new CodeObjectCreateExpression(myClass))); methodMain.Statements.Add(ConsoleWriteLineStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("dict"), "Count"))); //<Snippet9> methodMain.Statements.Add(new CodeExpressionStatement( new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( new CodeVariableReferenceExpression("dict"), "Print", new CodeTypeReference[] { new CodeTypeReference("System.Decimal"), new CodeTypeReference("System.Int32"), }), new CodeExpression[0]))); //</Snippet9> string dictionaryTypeName = typeof(System.Collections.Generic.Dictionary <int, System.Collections.Generic.List <string> >[]).FullName; CodeTypeReference dictionaryType = new CodeTypeReference(dictionaryTypeName); methodMain.Statements.Add( new CodeVariableDeclarationStatement(dictionaryType, "dict2", new CodeArrayCreateExpression(dictionaryType, new CodeExpression[1] { new CodePrimitiveExpression(null) }))); methodMain.Statements.Add(ConsoleWriteLineStatement( new CodePropertyReferenceExpression( new CodeVariableReferenceExpression("dict2"), "Length"))); class2.Members.Add(methodMain); ns.Types.Add(class2); }
private CodeMemberMethod GenerateEntryPointMethod() { CodeMemberMethod cmmMain = null; CodeDomProvider codeProvider = EnsureCodeProvider(); if (codeProvider.Supports(GeneratorSupport.EntryPointMethod)) { // // [STAThread] // public static void Main () { // cmmMain = new CodeEntryPointMethod(); cmmMain.Attributes = MemberAttributes.Public | MemberAttributes.Static; cmmMain.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(STAThreadAttribute).FullName)); AddDebuggerNonUserCodeAttribute(cmmMain); AddGeneratedCodeAttribute(cmmMain); GenerateXmlComments(cmmMain, "Application Entry Point."); cmmMain.ReturnType = new CodeTypeReference(typeof(void)); } return cmmMain; }
protected override void GenerateEntryPointMethod(CodeEntryPointMethod e, CodeTypeDeclaration c) { Output.WriteLine("[CodeEntryPointMethod: {0}]", e.ToString()); }
protected override void GenerateEntryPointMethod(CodeEntryPointMethod e, CodeTypeDeclaration c) { if (e.CustomAttributes.Count > 0) { this.OutputAttributes(e.CustomAttributes, false); } base.Output.WriteLine("Public Shared Sub Main()"); base.Indent++; this.GenerateVBStatements(e.Statements); base.Indent--; base.Output.WriteLine("End Sub"); }
private static CodeMemberMethod CreateEntryPointMethod(string className) { CodeMemberMethod entryPointMethod = new CodeEntryPointMethod(); entryPointMethod.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(STAThreadAttribute)))); entryPointMethod.Statements.Add(new CodeSnippetStatement(" System.Windows.ApplicationHost.Current.Run(() =>")); entryPointMethod.Statements.Add(new CodeSnippetStatement(" {")); entryPointMethod.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(className), "application", new CodeObjectCreateExpression(new CodeTypeReference(className)))); entryPointMethod.Statements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("application"), "InitializeComponent"))); entryPointMethod.Statements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("application"), "Run"))); entryPointMethod.Statements.Add(new CodeSnippetStatement(" });")); return entryPointMethod; }
protected override void GenerateEntryPointMethod(System.CodeDom.CodeEntryPointMethod e, System.CodeDom.CodeTypeDeclaration c) { //ничего не делаем }
protected override void GenerateEntryPointMethod (CodeEntryPointMethod e, CodeTypeDeclaration c) { }
protected override void GenerateEntryPointMethod (CodeEntryPointMethod method, CodeTypeDeclaration declaration) { OutputAttributes (method.CustomAttributes, null, LineHandling.ContinueLine); Output.WriteLine ("Public Shared Sub Main()"); Indent++; GenerateStatements (method.Statements); Indent--; Output.WriteLine ("End Sub"); }
protected abstract void GenerateEntryPointMethod (CodeEntryPointMethod m, CodeTypeDeclaration d);
public void Visit (CodeEntryPointMethod o) { g.GenerateEntryPointMethod (o, g.CurrentClass); }
protected override void GenerateEntryPointMethod(CodeEntryPointMethod e, CodeTypeDeclaration c) { Output.Write("public static void Main()"); OutputStartingBrace(); Indent++; GenerateStatements(e.Statements); Indent--; Output.WriteLine("}"); }
private void GenerateEntryPointMethod(CodeEntryPointMethod e, CodeTypeDeclaration c) { if (e.CustomAttributes.Count > 0) { GenerateAttributes(e.CustomAttributes); } Output.Write("public static "); OutputType(e.ReturnType); Output.Write(" Main()"); OutputStartingBrace(); Indent++; GenerateStatements(e.Statements); Indent--; Output.WriteLine("}"); }