本文整理汇总了C#中Microsoft.CSharp.CSharpCodeProvider.CreateGenerator方法的典型用法代码示例。如果您正苦于以下问题:C# Microsoft.CSharp.CSharpCodeProvider.CreateGenerator方法的具体用法?C# Microsoft.CSharp.CSharpCodeProvider.CreateGenerator怎么用?C# Microsoft.CSharp.CSharpCodeProvider.CreateGenerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CSharp.CSharpCodeProvider
的用法示例。
在下文中一共展示了Microsoft.CSharp.CSharpCodeProvider.CreateGenerator方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetImplementation
public MDynamicSupport GetImplementation()
{
// TODO: Adds a cache for generated assemblies...
CodeNamespace ns = BuildMDynamicSupportCodeDom();
Microsoft.CSharp.CSharpCodeProvider cp = new Microsoft.CSharp.CSharpCodeProvider();
cp.CreateGenerator().GenerateCodeFromNamespace( ns, Console.Out, null );
CodeCompileUnit unit = new CodeCompileUnit();
unit.Namespaces.Add( ns );
String[] assembliesName = GetRequiredAssemblies();
CompilerParameters parameters = new CompilerParameters(assembliesName);
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
CompilerResults result =
cp.CreateCompiler().CompileAssemblyFromDom(parameters, unit);
if (result.Errors.HasErrors)
{
throw new CompilationException(result.Errors);
}
Type dynamicType =
result.CompiledAssembly.GetType(
"Castle.ManagementExtensions.Generated.DynamicImplementation", true, true);
Object inst = Activator.CreateInstance(
dynamicType,
new object[] { info, instance } );
return (MDynamicSupport) inst;
}
示例2: generateCSharpButton_Click
//.........这里部分代码省略.........
// And setup the border style
initializeComponent.Statements.Add ( new CodeAssignStatement (
new CodePropertyReferenceExpression ( new CodeVariableReferenceExpression ( "_label" ), "BorderStyle" ),
new CodeFieldReferenceExpression ( new CodeTypeReferenceExpression ( typeof ( System.Windows.Forms.BorderStyle ) ), "Fixed3D" ) ) ) ;
// Set the location of the control
initializeComponent.Statements.Add ( new CodeAssignStatement (
new CodePropertyReferenceExpression ( new CodeVariableReferenceExpression ( "_label" ), "Location" ),
new CodeObjectCreateExpression ( typeof ( System.Drawing.Point ) ,
new CodeExpression[] { new CodePrimitiveExpression ( 8 ) ,
new CodePrimitiveExpression ( 8 ) } ) ) ) ;
// Set the name of the control
initializeComponent.Statements.Add ( new CodeAssignStatement (
new CodePropertyReferenceExpression ( new CodeVariableReferenceExpression ( "_label" ), "Name" ),
new CodePrimitiveExpression ( "_label" ) ) ) ;
// Set the size of the control
initializeComponent.Statements.Add ( new CodeAssignStatement (
new CodePropertyReferenceExpression ( new CodeVariableReferenceExpression ( "_label" ), "Size" ),
new CodeObjectCreateExpression ( typeof ( System.Drawing.Size ) ,
new CodeExpression[] { new CodePrimitiveExpression ( 312 ) ,
new CodePrimitiveExpression ( 23 ) } ) ) ) ;
// Set the tab index
initializeComponent.Statements.Add ( new CodeAssignStatement (
new CodePropertyReferenceExpression ( new CodeVariableReferenceExpression ( "_label" ), "TabIndex" ),
new CodePrimitiveExpression ( 0 ) ) ) ;
// And then the text!
initializeComponent.Statements.Add ( new CodeAssignStatement (
new CodePropertyReferenceExpression ( new CodeVariableReferenceExpression ( "_label" ), "Text" ),
new CodePrimitiveExpression ( messageText.Text ) ) ) ;
// Now add the label control to the controls collection
initializeComponent.Statements.Add (
new CodeMethodInvokeExpression ( new CodePropertyReferenceExpression ( new CodeThisReferenceExpression ( ) , "Controls" ), "Add",
new CodeExpression[] { new CodeVariableReferenceExpression ( "_label" ) } ) ) ;
// And set the name of the control to MyControl
initializeComponent.Statements.Add ( new CodeAssignStatement ( new CodePropertyReferenceExpression ( new CodeThisReferenceExpression ( ) , "Name" ) ,
new CodePrimitiveExpression ( "MyControl" ) ) ) ;
// And the size of the control
initializeComponent.Statements.Add ( new CodeAssignStatement ( new CodePropertyReferenceExpression ( new CodeThisReferenceExpression ( ) , "Size" ) ,
new CodeObjectCreateExpression ( typeof ( System.Drawing.Size ) ,
new CodeExpression[] { new CodePrimitiveExpression ( 328 ) ,
new CodePrimitiveExpression ( 100 ) } ) ) ) ;
// Add the ResumeLayout ( false ) call
initializeComponent.Statements.Add ( new CodeMethodInvokeExpression ( new CodeThisReferenceExpression ( ) , "ResumeLayout", new CodeExpression [] { new CodePrimitiveExpression ( false ) } ) ) ;
// And finally add initializeComponent to the members for the class
ctd.Members.Add ( initializeComponent ) ;
// Finally create the C#
CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider ( ) ;
#if DEBUG
// Generate the source code on disk
ICodeGenerator generator = provider.CreateGenerator ( ) ;
using ( StreamWriter sw = new StreamWriter ( "code.cs" , false ) )
{
CodeGeneratorOptions options = new CodeGeneratorOptions ( ) ;
options.BracingStyle = "C" ;
generator.GenerateCodeFromCompileUnit ( ccu, sw, options ) ;
}
#endif
ICodeCompiler compiler = provider.CreateGenerator ( ) as ICodeCompiler ;
CompilerParameters cp = new CompilerParameters ( new string[] { "System.dll", "System.Windows.Forms.dll", "System.Drawing.dll" } ) ;
cp.GenerateInMemory = true ;
cp.OutputAssembly = "AutoGenerated" ;
CompilerResults results = compiler.CompileAssemblyFromDom ( cp, ccu ) ;
if ( results.Errors.Count == 0 )
{
Type t = results.CompiledAssembly.GetType ( "MyControls.MyControl" ) ;
Control c = Activator.CreateInstance ( t ) as Control ;
c.Dock = DockStyle.Fill ;
controlPanel.SuspendLayout ( ) ;
controlPanel.Controls.Clear ( ) ;
controlPanel.Controls.Add ( c ) ;
controlPanel.ResumeLayout ( ) ;
}
else
{
CompilerError error = results.Errors[0] ;
int i = 0 ;
i++;
}
}
示例3: GenerateSource
protected void GenerateSource()
{
String fileName = _assyInfo._name + ".cs";
try {
File.OpenRead(fileName);
// It worked, so the souce file is there, don't generate
// another one
return;
} catch (FileNotFoundException) {
// Not found, we create it
}
CreateAssyNames();
ReadTypeLibInfo();
FileStream outStr = File.Create(fileName);
Console.WriteLine("Generating source file: " + fileName);
TextWriter writer = new StreamWriter(outStr);
CodeCompileUnit compileUnit = new CodeCompileUnit();
// Add TypeLib attribute
CodeAttributeArgument typeLibArg = new CodeAttributeArgument();
typeLibArg.Value = new CodePrimitiveExpression(Name);
compileUnit.AssemblyCustomAttributes.Add
(new CodeAttributeDeclaration
("System.Runtime.InteropServices.ImportedFromTypeLib",
new CodeAttributeArgument[] { typeLibArg }));
// Add Guid attribute
CodeAttributeArgument guidArg = new CodeAttributeArgument();
guidArg.Value = new CodePrimitiveExpression(_typeLibKey._guid.ToString());
compileUnit.AssemblyCustomAttributes.Add
(new CodeAttributeDeclaration
("System.Runtime.InteropServices.Guid",
new CodeAttributeArgument[] { guidArg }));
// Key file
/***
// FIXME - this is only for testing
CodeAttributeArgument keyFileArg = new CodeAttributeArgument();
keyFileArg.Value = new CodePrimitiveExpression
("\\d\\src\\mstools\\key\\nogoop.snk");
compileUnit.AssemblyCustomAttributes.Add
(new CodeAttributeDeclaration
("System.Reflection.AssemblyKeyFile",
new CodeAttributeArgument[] { keyFileArg }));
***/
CodeNamespace ns = new CodeNamespace(_assyInfo._name);
compileUnit.Namespaces.Add(ns);
foreach (Object cls in _members)
{
if (cls is ICodeDom)
((ICodeDom)cls).AddDomTo(ns.Types);
}
CodeDomProvider provider =
new Microsoft.CSharp.CSharpCodeProvider();
ICodeGenerator gen = provider.CreateGenerator();
gen.GenerateCodeFromCompileUnit(compileUnit,
writer,
null);
writer.Flush();
writer.Close();
}