本文整理汇总了C#中CodeNamespace类的典型用法代码示例。如果您正苦于以下问题:C# CodeNamespace类的具体用法?C# CodeNamespace怎么用?C# CodeNamespace使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeNamespace类属于命名空间,在下文中一共展示了CodeNamespace类的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) {
// 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);
}
示例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: 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
}
示例5: 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);
}
}
示例6: 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);
}
示例7: GenerateCode
public static StringBuilder GenerateCode(CodeNamespace ns)
{
var codeProvider = new CSharpCodeProvider();
var builder = new StringBuilder();
var writer = new StringWriter(builder);
codeProvider.GenerateCodeFromNamespace(ns, writer, new CodeGeneratorOptions());
writer.Flush();
return builder;
}
示例8: VisitNamespace
protected override void VisitNamespace(CodeNamespace codeNamespace)
{
this.ShowName(codeNamespace, "NameSpace");
_level++;
base.VisitNamespace(codeNamespace);
_level--;
}
示例9: 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;
}
示例10: NamespaceTraverser
public NamespaceTraverser(CodeNamespace ns, Action<CodeClass> withCodeClass)
{
if (ns == null)
throw new ArgumentNullException("ns");
if (withCodeClass == null)
throw new ArgumentNullException("withCodeClass");
WithCodeClass = withCodeClass;
if (ns.Members != null)
Traverse(ns.Members);
}
示例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: 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);
}
示例14: GenerateCode
private static string GenerateCode(string expression)
{
var source = new StringBuilder();
var sw = new StringWriter(source);
var options = new CodeGeneratorOptions();
var myNamespace = new CodeNamespace("ExpressionEvaluator");
myNamespace.Imports.Add(new CodeNamespaceImport("System"));
var classDeclaration = new CodeTypeDeclaration { IsClass = true, Name = "Calculator", Attributes = MemberAttributes.Public };
var myMethod = new CodeMemberMethod { Name = "Calculate", ReturnType = new CodeTypeReference(typeof(double)), Attributes = MemberAttributes.Public };
myMethod.Statements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(expression)));
classDeclaration.Members.Add(myMethod);
myNamespace.Types.Add(classDeclaration);
provider.GenerateCodeFromNamespace(myNamespace, sw, options);
sw.Flush();
sw.Close();
return source.ToString();
}
示例15: Compilation_NotSupported
public void Compilation_NotSupported()
{
CodeDomProvider provider = GetProvider();
var options = new CompilerParameters(new string[] { }, "test.exe");
var cu = new CodeCompileUnit();
var ns = new CodeNamespace("ns");
var cd = new CodeTypeDeclaration("Program");
var mm = new CodeEntryPointMethod();
cd.Members.Add(mm);
ns.Types.Add(cd);
cu.Namespaces.Add(ns);
string tempPath = Path.GetTempFileName();
try
{
File.WriteAllText(tempPath, GetEmptyProgramSource());
Assert.Throws<PlatformNotSupportedException>(() => provider.CompileAssemblyFromFile(options, tempPath));
Assert.Throws<PlatformNotSupportedException>(() => provider.CompileAssemblyFromDom(options, cu));
Assert.Throws<PlatformNotSupportedException>(() => provider.CompileAssemblyFromSource(options, GetEmptyProgramSource()));
#pragma warning disable 0618 // obsolete
ICodeCompiler cc = provider.CreateCompiler();
Assert.Throws<PlatformNotSupportedException>(() => cc.CompileAssemblyFromDom(options, cu));
Assert.Throws<PlatformNotSupportedException>(() => cc.CompileAssemblyFromDomBatch(options, new[] { cu }));
Assert.Throws<PlatformNotSupportedException>(() => cc.CompileAssemblyFromFile(options, tempPath));
Assert.Throws<PlatformNotSupportedException>(() => cc.CompileAssemblyFromFileBatch(options, new[] { tempPath }));
Assert.Throws<PlatformNotSupportedException>(() => cc.CompileAssemblyFromSource(options, GetEmptyProgramSource()));
Assert.Throws<PlatformNotSupportedException>(() => cc.CompileAssemblyFromSourceBatch(options, new[] { GetEmptyProgramSource() }));
#pragma warning restore 0618
}
finally
{
File.Delete(tempPath);
}
}