本文整理汇总了C#中Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromNamespace方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpCodeProvider.GenerateCodeFromNamespace方法的具体用法?C# CSharpCodeProvider.GenerateCodeFromNamespace怎么用?C# CSharpCodeProvider.GenerateCodeFromNamespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CSharp.CSharpCodeProvider
的用法示例。
在下文中一共展示了CSharpCodeProvider.GenerateCodeFromNamespace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main (string [] args)
{
try {
if (args.Length < 1) {
Console.WriteLine ("mono dsgentest.exe filename");
return;
}
DataSet ds = new DataSet ();
ds.ReadXml (args [0]);
ICodeGenerator gen = new CSharpCodeProvider ().CreateGenerator ();
CodeNamespace cns = new CodeNamespace ("MyNamespace");
TextWriter tw = new StreamWriter (Path.ChangeExtension (args [0], ".ms.cs"), false, Encoding.Default);
TypedDataSetGenerator.Generate (ds, cns, gen);
gen.GenerateCodeFromNamespace (cns, tw, null);
tw.Close ();
cns = new CodeNamespace ("MyNamespace");
tw = new StreamWriter (Path.ChangeExtension (args [0], ".mono.cs"), false, Encoding.Default);
CustomDataClassGenerator.CreateDataSetClasses (ds, cns, gen, null);
gen.GenerateCodeFromNamespace (cns, tw, null);
tw.Close ();
} catch (Exception ex) {
Console.WriteLine (ex);
}
}
示例2: Build
public void Build(DataStructure dataStructure, string rendererFilename = "")
{
if (string.IsNullOrEmpty(rendererFilename)) { rendererFilename = this.GetFilename(dataStructure); }
var rendererType = new CodeTypeDeclaration(dataStructure.RendererName);
rendererType.IsClass = true;
rendererType.IsPartial = true;
rendererType.BaseTypes.Add(typeof(Renderer));
rendererType.Comments.Add(new CodeCommentStatement("<summary>", true));
rendererType.Comments.Add(new CodeCommentStatement(string.Format("Renderer of {0}", dataStructure.TargetName), true));
rendererType.Comments.Add(new CodeCommentStatement("</summary>", true));
BuildCreate(rendererType, dataStructure);
BuildConstructor(rendererType, dataStructure);
BuildDoInitialize(rendererType, dataStructure);
BuildDoRender(rendererType, dataStructure);
var parserNamespace = new CodeNamespace("CSharpGL");
parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace));
parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Collections.Generic.List<int>).Namespace));
parserNamespace.Types.Add(rendererType);
//生成代码
using (var stream = new StreamWriter(rendererFilename, false))
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions opentions = new CodeGeneratorOptions();//代码生成选项
opentions.BlankLinesBetweenMembers = true;
opentions.BracingStyle = "C";
opentions.ElseOnClosing = false;
opentions.IndentString = " ";
opentions.VerbatimOrder = true;
codeProvider.GenerateCodeFromNamespace(parserNamespace, stream, opentions);
}
}
示例3: CanCreateDataTableAssignment
public void CanCreateDataTableAssignment()
{
CodeNamespace nsdecl = new CodeNamespace("My.Data");
CodeTypeDeclaration cdecl = new CodeTypeDeclaration("ResultSet");
CodeMemberMethod method = new CodeMemberMethod();
method.Name = "GetData";
method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
method.ReturnType = new CodeTypeReference("System.Data.DataTable");
method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "name"));
method.Statements.Add(new CodeVariableDeclarationStatement(
typeof(DataTable),
"result",
new CodeObjectCreateExpression(typeof(DataTable))));
cdecl.Members.Add(method);
method.Statements.Add(
new CodeVariableDeclarationStatement(
typeof(DataColumnCollection),
"columns",
new CodePropertyReferenceExpression(
new CodeVariableReferenceExpression("result"),
"Columns")));
method.Statements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("columns"), "Add", new CodeTypeOfExpression(typeof(string)), new CodeSnippetExpression("name")));
nsdecl.Types.Add(cdecl);
CSharpCodeProvider provider = new CSharpCodeProvider();
provider.GenerateCodeFromNamespace(nsdecl, Console.Out, new System.CodeDom.Compiler.CodeGeneratorOptions());
}
示例4: CreateCode
public static FileInfo CreateCode(DataTable dataTable, string filename = null)
{
// CodeGeneratorOptions so the output is clean and easy to read
CodeGeneratorOptions codeOptions = CreateGeneratorOptions();
string result = string.Empty;
CodeNamespace codeNamespace = Code.CreateCodeNamespace(dataTable);
using (MemoryStream memoryStream = new MemoryStream())
using (TextWriter textWriter = new StreamWriter(memoryStream, new UTF8Encoding(false, true)))
using (CSharpCodeProvider codeProvider = new CSharpCodeProvider())
{
codeProvider.GenerateCodeFromNamespace(codeNamespace, textWriter, codeOptions);
result = Encoding.UTF8.GetString(memoryStream.ToArray());
}
// Correct our little auto-property 'hack'
result = result.Replace("//;", "");
if (string.IsNullOrWhiteSpace(filename))
{
filename = Path.GetRandomFileName();
}
// Save file (if appropriate)
File.WriteAllText(filename, result);
return new FileInfo(filename);
}
示例5: Build
public void Build(DataStructure dataStructure, string modelFilename = "")
{
if (string.IsNullOrEmpty(modelFilename)) { modelFilename = this.GetFilename(dataStructure); }
// public class DemoModel : IBufferable { }
var modelType = new CodeTypeDeclaration(dataStructure.ModelName);
modelType.IsClass = true;
modelType.IsPartial = true;
modelType.BaseTypes.Add(typeof(IBufferable));
modelType.Comments.Add(new CodeCommentStatement("<summary>", true));
modelType.Comments.Add(new CodeCommentStatement(string.Format("Model of {0}", dataStructure.TargetName), true));
modelType.Comments.Add(new CodeCommentStatement("</summary>", true));
BuildFields(modelType, dataStructure);
BuildGetVertexAttributeBuffer(modelType, dataStructure);
BuildGetIndex(modelType, dataStructure);
var parserNamespace = new CodeNamespace("CSharpGL");
parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace));
parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Collections.Generic.List<int>).Namespace));
parserNamespace.Types.Add(modelType);
//生成代码
using (var stream = new StreamWriter(modelFilename, false))
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions opentions = new CodeGeneratorOptions();//代码生成选项
opentions.BlankLinesBetweenMembers = true;
opentions.BracingStyle = "C";
opentions.ElseOnClosing = false;
opentions.IndentString = " ";
opentions.VerbatimOrder = true;
codeProvider.GenerateCodeFromNamespace(parserNamespace, stream, opentions);
}
}
示例6: WriteCSharpCodeFile
private static void WriteCSharpCodeFile(CodeNamespace code, string outputFileName)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions();
using (StreamWriter writer = new StreamWriter(outputFileName, false))
provider.GenerateCodeFromNamespace(code, writer, options);
}
示例7: GenerateCodeFromFeature
private void GenerateCodeFromFeature(Feature feature, TextWriter writer)
{
SpecFlowUnitTestConverter converter = FactoryMethods.CreateUnitTestConverter(new NUnitTestConverter());
var codeNamespace = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions();
codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, options);
}
示例8: GenerateCodeFromFeature
private void GenerateCodeFromFeature(Feature feature, TextWriter writer)
{
var codeDomHelper = new CodeDomHelper(CodeDomProviderLanguage.CSharp);
var mbUnitTestGeneratorProvider = new MbUnitTestGeneratorProvider();
var converter = new SpecFlowUnitTestConverter(mbUnitTestGeneratorProvider, codeDomHelper, true);
CodeNamespace codeNamespace = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");
var codeProvider = new CSharpCodeProvider();
var options = new CodeGeneratorOptions();
codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, options);
}
示例9: GenerateCodeFromFeature
private void GenerateCodeFromFeature(Feature feature, TextWriter writer)
{
CodeDomHelper codeDomHelper = new CodeDomHelper(CodeDomProviderLanguage.CSharp);
XUnitTestGeneratorProvider xUnitTestGeneratorProvider = new XUnitTestGeneratorProvider();
SpecFlowUnitTestConverter converter = FactoryMethods.CreateUnitTestConverter(xUnitTestGeneratorProvider);
var codeNamespace = converter.GenerateUnitTestFixture(feature, "TestClassName", "Target.Namespace");
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions();
codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, options);
}
示例10: CreateAssemblyFromDataTable
public static System.Reflection.Assembly CreateAssemblyFromDataTable(DataTable DataValues)
{
Random rnd = new Random();
if (DataValues.TableName == null || DataValues.TableName == string.Empty)
DataValues.TableName = rnd.Next().ToString();
CodeTypeDeclaration classDec = new CodeTypeDeclaration(DataValues.TableName);
classDec.IsClass = true;
CodeConstructor constructor = new CodeConstructor();
constructor.Attributes = MemberAttributes.Public;
classDec.Members.Add(classDec);
foreach (DataColumn col in DataValues.Columns)
{
AddPropertyAndField(classDec, col.DataType, string.Empty, col.ColumnName);
}
AddPropertyAndField(classDec, null, "System.Collections.Generic.List<" + DataValues.TableName + ">", "ListOf" + DataValues.TableName);
using (CSharpCodeProvider provider = new CSharpCodeProvider())
{
//ICodeGenerator generator = provider.CreateGenerator();
CodeNamespace ns = new CodeNamespace("Terminals.Generated");
ns.Types.Add((CodeTypeDeclaration)classDec);
CodeGeneratorOptions options = new CodeGeneratorOptions();
//options.BlankLinesBetweenMembers = true;
string filename = System.IO.Path.GetTempFileName();
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, false))
{
//generator.GenerateCodeFromNamespace(ns, sw, options);
provider.GenerateCodeFromNamespace(ns, sw, options);
//ICodeCompiler icc = provider.CreateCompiler();
CompilerParameters compileParams = new CompilerParameters();
compileParams.GenerateExecutable = false;
compileParams.GenerateInMemory = true;
//return icc.CompileAssemblyFromSource(compileParams, System.IO.File.ReadAllText(filename)).CompiledAssembly;
CompilerResults icc = provider.CompileAssemblyFromSource(compileParams, System.IO.File.ReadAllText(filename));
return icc.CompiledAssembly;
}
}
}
示例11: GetSourceFromCCU
private string GetSourceFromCCU(CodeCompileUnit CCU)
{
CSharpCodeProvider cscp = new CSharpCodeProvider();
StringBuilder sb = new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BracingStyle = "C";
options.BlankLinesBetweenMembers = false;
if (Generated)
{
cscp.GenerateCodeFromCompileUnit(CCU, sw, options);
}
else
{
cscp.GenerateCodeFromNamespace(CCU.Namespaces[0], sw, options);
}
return sb.ToString();
}
示例12: GenerateCode
public static string GenerateCode(CodeObject codeObj)
{
var provider = new CSharpCodeProvider();
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
if (codeObj is CodeCompileUnit)
{
provider.GenerateCodeFromCompileUnit(codeObj as CodeCompileUnit, writer, null);
}
else if (codeObj is CodeExpression)
{
provider.GenerateCodeFromExpression(codeObj as CodeExpression, writer, null);
}
else if (codeObj is CodeStatement)
{
provider.GenerateCodeFromStatement(codeObj as CodeStatement, writer, null);
}
else if (codeObj is CodeTypeMember)
{
provider.GenerateCodeFromMember(codeObj as CodeTypeMember, writer, null);
}
else if (codeObj is CodeNamespace)
{
provider.GenerateCodeFromNamespace(codeObj as CodeNamespace, writer, null);
}
else
{
throw new InvalidOperationException();
}
writer.Flush();
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
示例13: VerifyCtors
public void VerifyCtors()
{
CodeNamespace nsdecl = new CodeNamespace("My.Data");
CodeTypeDeclaration cdecl = new CodeTypeDeclaration("ResultSet");
CodeConstructor ctor = new CodeConstructor();
ctor.ReturnType = new CodeTypeReference("ResultSet");
ctor.Attributes = MemberAttributes.Public;
cdecl.Members.Add(ctor);
nsdecl.Types.Add(cdecl);
CSharpCodeProvider provider = new CSharpCodeProvider();
provider.GenerateCodeFromNamespace(nsdecl, Console.Out, new System.CodeDom.Compiler.CodeGeneratorOptions());
}
示例14: DynamicInvocation
/// <summary>
/// Preia informatiile din documentul WSDL (contruieste si compileaza un proxy local pentru serviciul web).
/// Pune la dispozitie toate metodele ce pot fi invocate prin acest proxy.
/// </summary>
private void DynamicInvocation()
{
try
{
txbMessage.Text = "";
Uri uri = new Uri(txbWSDLAddress.Text);
txbMessage.Text += "Requesting WSDL\r\n";
WebRequest webRequest = WebRequest.Create(uri);
System.IO.Stream requestStream = webRequest.GetResponse().GetResponseStream();
ServiceDescription serviceDescription = ServiceDescription.Read(requestStream);
string serviceName = serviceDescription.Services[0].Name;
trvMethods.Nodes.Add(serviceName);
txbMessage.Text += "Generating Proxy\r\n";
ServiceDescriptionImporter serviceDescriptionImporter = new ServiceDescriptionImporter();
serviceDescriptionImporter.AddServiceDescription(serviceDescription, String.Empty, String.Empty);
serviceDescriptionImporter.ProtocolName = "Soap";
serviceDescriptionImporter.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
txbMessage.Text += "Generating assembly\r\n";
CodeNamespace codeNameSpace = new CodeNamespace();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
codeCompileUnit.Namespaces.Add(codeNameSpace);
ServiceDescriptionImportWarnings serviceDescriptionImportWarnings = serviceDescriptionImporter.Import(codeNameSpace, codeCompileUnit);
if (serviceDescriptionImportWarnings == 0)
{
StringWriter stringWriter = new StringWriter();
CSharpCodeProvider cSharpCodeProvider = new CSharpCodeProvider();
cSharpCodeProvider.GenerateCodeFromNamespace(codeNameSpace, stringWriter, new CodeGeneratorOptions());
txbMessage.Text += "Compiling assembly\r\n";
string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
CompilerParameters compileParameters = new CompilerParameters(assemblyReferences);
compileParameters.GenerateExecutable = false;
compileParameters.GenerateInMemory = true;
compileParameters.TreatWarningsAsErrors = false;
compileParameters.WarningLevel = 4;
CompilerResults compilerResults = new CompilerResults(new TempFileCollection());
compilerResults = cSharpCodeProvider.CompileAssemblyFromDom(compileParameters, codeCompileUnit);
Assembly assembly = compilerResults.CompiledAssembly;
service = assembly.GetType(serviceName);
txbMessage.Text += "Retrieving web service methods\r\n";
methodInfos = service.GetMethods();
foreach (MethodInfo methodInfo in methodInfos)
{
if (methodInfo.Name == "Discover")
{
break;
}
trvMethods.Nodes[0].Nodes.Add(methodInfo.Name);
}
trvMethods.Nodes[0].Expand();
txbMessage.Text += "Ready to invoke\r\n ";
}
else
{
txbMessage.Text += serviceDescriptionImportWarnings;
}
}
catch (Exception e)
{
txbMessage.Text += "\r\n" + e.Message + "\r\n\r\n" + e.ToString();
}
tabControl.SelectedTab = tabPageMessage;
//.........这里部分代码省略.........
示例15: InvokeMethod
private string InvokeMethod(string wsdl_text,string MethodName,Object[] param1)
{
try
{
Uri uri = new Uri(wsdl_text);
WebRequest webRequest = WebRequest.Create(uri);
System.IO.Stream requestStream = webRequest.GetResponse().GetResponseStream();
// Get a WSDL file describing a service
ServiceDescription sd = ServiceDescription.Read(requestStream);
string sdName = sd.Services[0].Name;
// Initialize a service description servImport
ServiceDescriptionImporter servImport = new ServiceDescriptionImporter();
servImport.AddServiceDescription(sd, String.Empty, String.Empty);
servImport.ProtocolName = "Soap";
servImport.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
CodeNamespace nameSpace = new CodeNamespace();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
codeCompileUnit.Namespaces.Add(nameSpace);
// Set Warnings
ServiceDescriptionImportWarnings warnings = servImport.Import(nameSpace, codeCompileUnit);
if (warnings == 0)
{
StringWriter stringWriter = new StringWriter(System.Globalization.CultureInfo.CurrentCulture);
Microsoft.CSharp.CSharpCodeProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
prov.GenerateCodeFromNamespace(nameSpace, stringWriter, new CodeGeneratorOptions());
// Compile the assembly with the appropriate references
string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
CompilerParameters param = new CompilerParameters(assemblyReferences);
param.GenerateExecutable = false;
param.GenerateInMemory = true;
param.TreatWarningsAsErrors = false;
param.WarningLevel = 4;
CompilerResults results = new CompilerResults(new TempFileCollection());
results = prov.CompileAssemblyFromDom(param, codeCompileUnit);
Assembly assembly = results.CompiledAssembly;
service = assembly.GetType(sdName);
MethodInfo[] methodinfo = service.GetMethods();
string result = null;
foreach (MethodInfo t in methodinfo)
if (t.Name == MethodName)
{
//Invoke Method
Object obj = Activator.CreateInstance(service);
Object response = t.Invoke(obj, param1);
Array myArrayList = response as Array;
if (myArrayList != null)
{
List<Object> result_obj = new List<Object>();
foreach (var item in myArrayList)
{
foreach (var currentPropertyInformation in item.GetType().GetProperties())
{
//currentPropertyInformation.GetValue(item, null);
//Result.Text = Result.Text + currentPropertyInformation.Name + ":" + currentPropertyInformation.GetValue(item, null);
result = currentPropertyInformation.GetValue(item, null).ToString();
}
}
}
else if(response.GetType().ToString() != "System.String")
{
foreach (var currentPropertyInformation in response.GetType().GetProperties())
{
//currentPropertyInformation.GetValue(item, null);
//Result.Text = Result.Text + currentPropertyInformation.Name + ":" + currentPropertyInformation.GetValue(item, null);
if (currentPropertyInformation.GetValue(response, null) != null)
{
result = result + currentPropertyInformation.Name + ":" + currentPropertyInformation.GetValue(response, null) + "|";
}
else
{
result = result + currentPropertyInformation.Name + ":NULL,";
}
}
}
if(response!=null && result==null)
{
result = response.ToString();
}
break;
}
return result;
}
else
{
return null;
}
}
//.........这里部分代码省略.........