本文整理汇总了C#中CodeCompileUnit类的典型用法代码示例。如果您正苦于以下问题:C# CodeCompileUnit类的具体用法?C# CodeCompileUnit怎么用?C# CodeCompileUnit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CodeCompileUnit类属于命名空间,在下文中一共展示了CodeCompileUnit类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateWebServiceFromWsdl
object CreateWebServiceFromWsdl(byte[] wsdl) {
// generate CodeDom from WSDL
ServiceDescription sd = ServiceDescription.Read(new MemoryStream(wsdl));
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ServiceDescriptions.Add(sd);
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
CodeNamespace codeNamespace = new CodeNamespace("");
codeCompileUnit.Namespaces.Add(codeNamespace);
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateNewAsync | CodeGenerationOptions.GenerateOldAsync;
importer.Import(codeNamespace, codeCompileUnit);
// update web service proxy CodeDom tree to add dynamic support
string wsProxyTypeName = FindProxyTypeAndAugmentCodeDom(codeNamespace);
// compile CodeDom tree into an Assembly
CodeDomProvider provider = CodeDomProvider.CreateProvider("CS");
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.GenerateInMemory = true;
compilerParams.IncludeDebugInformation = false;
compilerParams.ReferencedAssemblies.Add(typeof(Ops).Assembly.Location); //DLR
CompilerResults results = provider.CompileAssemblyFromDom(compilerParams, codeCompileUnit);
Assembly generatedAssembly = results.CompiledAssembly;
// find the type derived from SoapHttpClientProtocol
Type wsProxyType = generatedAssembly.GetType(wsProxyTypeName);
if (wsProxyType == null) {
throw new InvalidOperationException("Web service proxy type not generated.");
}
// create an instance of the web proxy type
return Activator.CreateInstance(wsProxyType);
}
示例2: BuildTree
public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
if (!(provider is JScriptCodeProvider)) {
// GENERATES (C#):
//
// namespace Namespace1 {
//
// public class TEST {
//
// public static void Main() {
// // the following is repeated Char.MaxValue times
// System.Console.WriteLine(/* character value goes here */);
// }
// }
// }
CodeNamespace ns = new CodeNamespace ("Namespace1");
ns.Imports.Add (new CodeNamespaceImport ("System"));
cu.Namespaces.Add (ns);
CodeTypeDeclaration cd = new CodeTypeDeclaration ("TEST");
cd.IsClass = true;
ns.Types.Add (cd);
CodeEntryPointMethod methodMain = new CodeEntryPointMethod ();
for (int i = 0; i < Char.MaxValue; i+=50)
methodMain.Statements.Add (CDHelper.ConsoleWriteLineStatement (new CodePrimitiveExpression (System.Convert.ToChar (i))));
cd.Members.Add (methodMain);
}
}
示例3: BuildTree
public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu) {
//cu.UserData["AllowLateBound"] = true;
// GENERATES (C#):
// namespace Namespace1 {
// using System;
//
//
// public class Class1 {
//
// public virtual string Foo1(string format, [System.Runtime.InteropServices.OptionalAttribute()] params object[] array) {
// string str;
// str = format.Replace("{0}", array[0].ToString());
// str = str.Replace("{1}", array[1].ToString());
// str = str.Replace("{2}", array[2].ToString());
// return str;
// }
// }
// }
CodeNamespace ns = new CodeNamespace("Namespace1");
ns.Imports.Add(new CodeNamespaceImport("System"));
cu.Namespaces.Add(ns);
// Full Verification Objects
CodeTypeDeclaration class1 = new CodeTypeDeclaration();
class1.Name = "Class1";
ns.Types.Add(class1);
AddScenario ("CheckFoo1");
CodeMemberMethod fooMethod1 = new CodeMemberMethod();
fooMethod1.Name = "Foo1";
fooMethod1.Attributes = MemberAttributes.Public ;
fooMethod1.ReturnType = new CodeTypeReference(typeof(string));
CodeParameterDeclarationExpression parameter1 = new CodeParameterDeclarationExpression();
parameter1.Name = "format";
parameter1.Type = new CodeTypeReference(typeof(string));
fooMethod1.Parameters.Add(parameter1);
CodeParameterDeclarationExpression parameter2 = new CodeParameterDeclarationExpression();
parameter2.Name = "array";
parameter2.Type = new CodeTypeReference(typeof(object[]));
if (Supports (provider, GeneratorSupport.ParameterAttributes)) {
parameter2.CustomAttributes.Add( new CodeAttributeDeclaration("System.ParamArrayAttribute"));
parameter2.CustomAttributes.Add( new CodeAttributeDeclaration("System.Runtime.InteropServices.OptionalAttribute"));
}
fooMethod1.Parameters.Add(parameter2);
class1.Members.Add(fooMethod1);
fooMethod1.Statements.Add( new CodeVariableDeclarationStatement(typeof(string), "str"));
fooMethod1.Statements.Add(CreateStatement(new CodeArgumentReferenceExpression ("format"), 0));
fooMethod1.Statements.Add(CreateStatement(new CodeVariableReferenceExpression ("str"), 1));
fooMethod1.Statements.Add(CreateStatement(new CodeVariableReferenceExpression ("str"), 2));
fooMethod1.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("str")));
}
示例4: FromDomBatch
protected virtual CompilerResults FromDomBatch (CompilerParameters options, CodeCompileUnit[] ea)
{
string[] fileNames = new string[ea.Length];
int i = 0;
if (options == null)
options = new CompilerParameters ();
StringCollection assemblies = options.ReferencedAssemblies;
foreach (CodeCompileUnit e in ea) {
fileNames[i] = Path.ChangeExtension (Path.GetTempFileName(), FileExtension);
FileStream f = new FileStream (fileNames[i], FileMode.OpenOrCreate);
StreamWriter s = new StreamWriter (f);
if (e.ReferencedAssemblies != null) {
foreach (string str in e.ReferencedAssemblies) {
if (!assemblies.Contains (str))
assemblies.Add (str);
}
}
((ICodeGenerator)this).GenerateCodeFromCompileUnit (e, s, new CodeGeneratorOptions());
s.Close();
f.Close();
i++;
}
return Compile (options, fileNames, false);
}
示例5: FromDomBatch
// Compile a CodeDom compile unit.
protected virtual CompilerResults FromDom
(CompilerParameters options, CodeCompileUnit e)
{
CodeCompileUnit[] list = new CodeCompileUnit [1];
list[0] = e;
return FromDomBatch(options, list);
}
示例6: BuildTree
public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
#if WHIDBEY
if (!(provider is JScriptCodeProvider)) {
cu.ReferencedAssemblies.Add("System.dll");
cu.UserData["AllowLateBound"] = true;
CodeNamespace ns = new CodeNamespace("Namespace1");
ns.Imports.Add(new CodeNamespaceImport("System"));
cu.Namespaces.Add(ns);
AddScenario ("FindPartialClass", "Attempt to find 'PartialClass'");
BuildClassesIntoNamespace (provider, ns, "PartialClass", ClassTypes.Class, TypeAttributes.Public);
AddScenario ("FindSealedPartialClass", "Attempt to find 'SealedPartialClass'");
BuildClassesIntoNamespace (provider, ns, "SealedPartialClass", ClassTypes.Class, TypeAttributes.Sealed);
BuildClassesIntoNamespace (provider, ns, "AbstractPartialClass", ClassTypes.Class, TypeAttributes.Abstract);
BuildClassesIntoNamespace (provider, ns, "PrivatePartialClass", ClassTypes.Class, TypeAttributes.NotPublic);
if (Supports (provider, GeneratorSupport.DeclareValueTypes)) {
AddScenario ("FindSealedPartialStruct", "Attempt to find 'SealedPartialStruct'");
BuildClassesIntoNamespace (provider, ns, "SealedPartialStruct", ClassTypes.Struct, TypeAttributes.Sealed);
BuildClassesIntoNamespace (provider, ns, "AbstractPartialStruct", ClassTypes.Struct, TypeAttributes.Abstract);
}
}
#endif
}
示例7: BuildTree
public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
// GENERATES (C#):
//
// namespace NSPC {
//
// public class ClassWithMethod {
//
// public int MethodName() {
// This is a CODE SNIPPET #*$*@;
// return 3;
// }
// }
// }
AddScenario ("FindSnippet", "Find code snippet in the code.");
CodeNamespace nspace = new CodeNamespace ("NSPC");
cu.Namespaces.Add (nspace);
CodeTypeDeclaration class1 = new CodeTypeDeclaration ("ClassWithMethod");
class1.IsClass = true;
nspace.Types.Add (class1);
CodeMemberMethod cmm = new CodeMemberMethod ();
cmm.Name = "MethodName";
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Final;
cmm.ReturnType = new CodeTypeReference (typeof (int));
cmm.Statements.Add (new CodeExpressionStatement (new CodeSnippetExpression ("This is a CODE SNIPPET #*$*@")));
cmm.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (3)));
class1.Members.Add (cmm);
}
示例8: BuildTree
public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
CodeNamespace ns = new CodeNamespace ("Namespace1");
cu.Namespaces.Add (ns);
// GENERATES (C#):
//
// namespace Namespace1 {
// public class Class1 {
// public int Method1 {
// #line 300 "LinedStatement"
// return 0;
//
// #line default
// #line hidden
// }
// }
// }
CodeTypeDeclaration class1 = new CodeTypeDeclaration ("Class1");
class1.IsClass = true;
class1.Attributes = MemberAttributes.Public;
ns.Types.Add (class1);
CodeMemberMethod method1 = new CodeMemberMethod ();
method1.ReturnType = new CodeTypeReference (typeof (int));
method1.Name = "Method1";
class1.Members.Add (method1);
AddScenario ("FindLinedStatement");
CodeMethodReturnStatement ret = new CodeMethodReturnStatement (new CodePrimitiveExpression (0));
ret.LinePragma = new CodeLinePragma ("LinedStatement", 300);
method1.Statements.Add (ret);
}
示例9: GeneratorData
public GeneratorData(Smoke* smoke, string defaultNamespace, List<string> imports, List<Assembly> references,
CodeCompileUnit unit, string destination, string docs)
{
Destination = destination;
Docs = docs;
Smoke = smoke;
string argNamesFile = GetArgNamesFile(Smoke);
if (File.Exists(argNamesFile))
{
foreach (string[] strings in File.ReadAllLines(argNamesFile).Select(line => line.Split(';')))
{
ArgumentNames[strings[0]] = strings[1].Split(',');
}
}
CompileUnit = unit;
Imports = imports;
References = references;
DefaultNamespace = new CodeNamespace(defaultNamespace);
this.AddUsings(DefaultNamespace);
foreach (Assembly assembly in References)
{
smokeClassAttribute = assembly.GetType("QtCore.SmokeClass", false);
if (smokeClassAttribute != null)
{
smokeClassGetSignature = smokeClassAttribute.GetProperty("Signature").GetGetMethod();
break;
}
}
foreach (Assembly assembly in References)
{
foreach (Type type in assembly.GetTypes())
{
object[] attributes = type.GetCustomAttributes(smokeClassAttribute, false);
if (attributes.Length != 0)
{
string smokeClassName = (string) smokeClassGetSignature.Invoke(attributes[0], null);
Type t;
if (ReferencedTypeMap.TryGetValue(smokeClassName, out t) && t.IsInterface)
{
continue;
}
ReferencedTypeMap[smokeClassName] = type;
}
else
{
ReferencedTypeMap[type.Name] = type;
}
}
}
CompileUnit.Namespaces.Add(DefaultNamespace);
NamespaceMap[defaultNamespace] = DefaultNamespace;
}
示例10: GenerateCode
public static StringBuilder GenerateCode(CodeNamespace ns)
{
var compileUnit = new CodeCompileUnit();
var codeProvider = new CSharpCodeProvider();
var builder = new StringBuilder();
var writer = new StringWriter(builder);
compileUnit.Namespaces.Add(ns);
codeProvider.GenerateCodeFromCompileUnit(compileUnit, writer, new CodeGeneratorOptions());
writer.Flush();
return builder;
}
示例11: BuildTree
public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
// GENERATES (C#):
//
// public class MyConverter : System.ComponentModel.TypeConverter {
//
// private void Foo() {
// this.Foo(null);
// }
//
// private void Foo(string s) {
// }
//
// public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) {
// return base.CanConvertFrom(context, sourceType);
// }
// }
CodeNamespace ns = new CodeNamespace ();
cu.Namespaces.Add (ns);
CodeTypeDeclaration class1 = new CodeTypeDeclaration ();
class1.Name = "MyConverter";
class1.BaseTypes.Add (new CodeTypeReference (typeof (System.ComponentModel.TypeConverter)));
ns.Types.Add (class1);
CodeMemberMethod foo1 = new CodeMemberMethod ();
foo1.Name = "Foo";
foo1.Statements.Add (new CodeMethodInvokeExpression (new CodeThisReferenceExpression (), "Foo", new CodePrimitiveExpression (null)));
class1.Members.Add (foo1);
CodeMemberMethod foo2 = new CodeMemberMethod ();
foo2.Name = "Foo";
foo2.Parameters.Add (new CodeParameterDeclarationExpression (typeof (string), "s"));
class1.Members.Add (foo2);
CodeMemberMethod convert = new CodeMemberMethod ();
convert.Name = "CanConvertFrom";
convert.Attributes = MemberAttributes.Public | MemberAttributes.Override | MemberAttributes.Overloaded;
convert.ReturnType = new CodeTypeReference (typeof (bool));
convert.Parameters.Add (new CodeParameterDeclarationExpression (typeof (System.ComponentModel.ITypeDescriptorContext), "context"));
convert.Parameters.Add (new CodeParameterDeclarationExpression (typeof (System.Type), "sourceType"));
convert.Statements.Add (
new CodeMethodReturnStatement (
new CodeMethodInvokeExpression (
new CodeBaseReferenceExpression (),
"CanConvertFrom",
new CodeArgumentReferenceExpression ("context"),
new CodeArgumentReferenceExpression ("sourceType"))));
class1.Members.Add (convert);
}
示例12: Main
static void Main(string[] args)
{
int namespaces = 10;
int classesPerNamespace = 10;
int methodsPerClass = 10;
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
for (int i = 0; i < namespaces; ++i)
{
CodeNamespace codeNamespace = new CodeNamespace();
codeCompileUnit.Namespaces.Add(codeNamespace);
codeNamespace.Name = "Namespace" + i;
for (int j = 0; j < classesPerNamespace; ++j)
{
CodeTypeDeclaration codeTypeDeclaration = new CodeTypeDeclaration();
codeNamespace.Types.Add(codeTypeDeclaration);
codeTypeDeclaration.Name = "Class" + j;
codeTypeDeclaration.TypeAttributes = TypeAttributes.Public;
codeTypeDeclaration.Comments.Add(
new CodeCommentStatement(
"<summary>This is a summary.</summary>",
true
)
);
for (int k = 0; k < methodsPerClass; ++k)
{
CodeMemberMethod codeMemberMethod = new CodeMemberMethod();
codeTypeDeclaration.Members.Add(codeMemberMethod);
codeMemberMethod.Name = "Method" + k;
codeMemberMethod.Attributes = MemberAttributes.Public;
codeMemberMethod.Comments.Add(
new CodeCommentStatement(
"<summary>This is a summary.</summary>",
true
)
);
}
}
}
CodeDomProvider codeDomProvider = new CSharpCodeProvider();
ICodeGenerator codeGenerator = codeDomProvider.CreateGenerator();
CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions();
codeGenerator.GenerateCodeFromCompileUnit(codeCompileUnit, Console.Out, codeGeneratorOptions);
}
示例13: GetTargetStream
protected override StreamWriter GetTargetStream(string destDirectory, CodeCompileUnit cu)
{
string cun = cu.Namespaces[0].Name;
int li = cun.LastIndexOf('.');
string filNam = cun.Substring(li + 1);
cun = cun.Substring(0, li);
string packagedDirectory = destDirectory + "/" + cun.Replace('.', '/');
if (!Directory.Exists(packagedDirectory))
Directory.CreateDirectory(packagedDirectory);
return new StreamWriter(packagedDirectory + "/" + filNam + FileExtension, false);
}
示例14: BuildTree
public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
CodeNamespace ns = new CodeNamespace ();
ns.Name = "MyNamespace";
cu.Namespaces.Add (ns);
// GENERATES (C#):
//
// namespace MyNamespace {
// public class MyClass {
// private void PrimitiveTest() {
// char var1 = 'a';
// char var2 = '\0';
// string var3 = "foo\0bar\0baz\0";
// object var4 = null;
// int var5 = 42;
// double var6 = 3.14;
// System.Console.Write(var1);
// System.Console.Write(var2);
// System.Console.Write(var3);
// System.Console.Write(var4);
// System.Console.Write(var5);
// System.Console.Write(var6);
// }
// }
// }
CodeTypeDeclaration class1 = new CodeTypeDeclaration ();
class1.Name = "MyClass";
class1.IsClass = true;
class1.Attributes = MemberAttributes.Public;
ns.Types.Add (class1);
CodeMemberMethod method = new CodeMemberMethod ();
method.Name = "PrimitiveTest";
method.Statements.Add (new CodeVariableDeclarationStatement (typeof (char), "var1", new CodePrimitiveExpression ('a')));
method.Statements.Add (new CodeVariableDeclarationStatement (typeof (char), "var2", new CodePrimitiveExpression ('\0')));
method.Statements.Add (new CodeVariableDeclarationStatement (typeof (string), "var3", new CodePrimitiveExpression ("foo\0bar\0baz\0")));
method.Statements.Add (new CodeVariableDeclarationStatement (typeof (Object), "var4", new CodePrimitiveExpression (null)));
method.Statements.Add (new CodeVariableDeclarationStatement (typeof (int), "var5", new CodePrimitiveExpression (42)));
method.Statements.Add (new CodeVariableDeclarationStatement (typeof (double), "var6", new CodePrimitiveExpression (3.14)));
method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var1")));
method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var2")));
method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var3")));
method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var4")));
method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var5")));
method.Statements.Add (new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (Console)), "Write", new CodeVariableReferenceExpression ("var6")));
class1.Members.Add (method);
}
示例15: GetNamespace
public virtual CodeNamespace GetNamespace(string name)
{
CodeNamespace nm;
if (!Namespaces.TryGetValue(name, out nm))
{
CodeCompileUnit cu = new CodeCompileUnit();
CodeNamespace n = new CodeNamespace(name);
AddDefaultImports(n);
cu.Namespaces.Add(n);
CompileUnits.Add(cu);
Namespaces[name] = n;
nm = n;
}
return nm;
}