本文整理汇总了C#中Microsoft.CSharp.CSharpCodeProvider.CreateCompiler方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpCodeProvider.CreateCompiler方法的具体用法?C# CSharpCodeProvider.CreateCompiler怎么用?C# CSharpCodeProvider.CreateCompiler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CSharp.CSharpCodeProvider
的用法示例。
在下文中一共展示了CSharpCodeProvider.CreateCompiler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: init
private bool init(string generateCode, params string[] referenceAssemblies)
{
bool flag = false;
result = null;
using (CSharpCodeProvider provider = new CSharpCodeProvider())
{
ICodeCompiler objICodeCompiler = provider.CreateCompiler();
CompilerParameters objCompilerParameters = new CompilerParameters();
if (referenceAssemblies != null)
objCompilerParameters.ReferencedAssemblies.AddRange(referenceAssemblies);
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;
result = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, generateCode);
}
if (result != null)
{
if (result.Errors.Count > 0 && ErrorHandler != null)
{
ErrorHandler(result.Errors);
}
else
{
flag = true;
}
}
return flag;
}
示例2: InvokeWebService
public static object InvokeWebService(string url, string classname, string methodname, object[] args)
{
string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
if ((classname == null) || (classname == ""))
{
classname = WebServiceProxy.GetWsClassName(url);
}
try
{
//获取WSDL
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(stream);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(@namespace);
//生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
ICodeCompiler icc = csc.CreateCompiler();
//设定编译参数
CompilerParameters cplist = new CompilerParameters();
cplist.GenerateExecutable = false;
cplist.GenerateInMemory = true;
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.XML.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll");
//编译代理类
CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
//生成代理实例,并调用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + classname, true, true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
return mi.Invoke(obj, args);
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
}
}
示例3: CreateCompiler_ReturnsSame
public void CreateCompiler_ReturnsSame()
{
CSharpCodeProvider provider = new CSharpCodeProvider();
#pragma warning disable 0618
Assert.Same(provider.CreateCompiler(), provider.CreateCompiler());
#pragma warning restore 0618
}
示例4: BuildAssembly
private static Assembly BuildAssembly(string code)
{
CSharpCodeProvider provider =
new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = true;
compilerparams.ReferencedAssemblies.Add("PageTypeBuilder.dll");
compilerparams.ReferencedAssemblies.Add("PageTypeBuilder.Migrations.dll");
CompilerResults results =
compiler.CompileAssemblyFromSource(compilerparams, code);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors)
{
errors.AppendFormat("Line {0},{1}\t: {2}\n",
error.Line, error.Column, error.ErrorText);
}
throw new Exception(errors.ToString());
}
else
{
return results.CompiledAssembly;
}
}
示例5: Compile
public static CompilerResults Compile(string[] source, CompilerParameters compilerParameters)
{
CSharpCodeProvider CScodeProvider = new CSharpCodeProvider();
ICodeCompiler icodeCompiler = CScodeProvider.CreateCompiler();
CompilerResults compilerResults = icodeCompiler.CompileAssemblyFromSourceBatch(compilerParameters, source);
return compilerResults;
}
示例6: Invoke
public object Invoke(string url, string methodName, string className = "", params object[] args)
{
if (className == null || className == "")
{
className = GetClassName(url);
}
string @namespace = "ServiceBase.WebService.DynamicWebLoad." + ProcessUrl(url);
string cacheName = string.Format("{0}_{1}", @namespace, className);
Assembly assembly = null;
if (HttpContext.Current.Cache[cacheName] != null)
{
assembly = HttpContext.Current.Cache[cacheName] as Assembly;
}
else
{
//获取服务描述语言(WSDL)
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(stream);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(@namespace);
//生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
ICodeCompiler icc = csc.CreateCompiler();
//设定编译器的参数
CompilerParameters cplist = new CompilerParameters();
cplist.GenerateExecutable = false;
cplist.GenerateInMemory = true;
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.XML.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll");
//编译代理类
CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new StringBuilder();
foreach (CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
//生成代理实例,并调用方法
assembly = cr.CompiledAssembly;
HttpContext.Current.Cache[cacheName] = assembly;
}
Type t = assembly.GetType(@namespace + "." + className, true, true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod(methodName);
return mi.Invoke(obj, args);
}
示例7: BuildAssembly
public static Assembly BuildAssembly(string code)
{
Microsoft.CSharp.CSharpCodeProvider provider =
new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = true;
//compilerparams.IncludeDebugInformation = true;
compilerparams.ReferencedAssemblies.Add("System.dll");
compilerparams.ReferencedAssemblies.Add("System.Core.dll");
compilerparams.ReferencedAssemblies.Add("convexcad.exe");
CompilerResults results =
compiler.CompileAssemblyFromFile(compilerparams, code);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors)
{
errors.AppendFormat("Line {0},{1}\t: {2}\n",
error.Line, error.Column, error.ErrorText);
}
throw new System.ApplicationException(errors.ToString());
}
else
{
return results.CompiledAssembly;
}
}
示例8: StandardClass
// super class
// implements list
// slots
public StandardClass(string className)
{
this.className = className;
StringBuilder source = new StringBuilder("using System;");
//source.Append("class T{");
source.Append(string.Format("public class {0} {{",className));
source.Append("string _name;");
//source.Append("public T(string name){");
source.Append(string.Format("public {0}(string name) {{", className));
source.Append("_name=name;}");
source.Append("public void print(){");
source.Append("Console.WriteLine(\"Hello \"+_name);}");
source.Append("}");
Console.WriteLine(source.ToString());
CSharpCodeProvider cscompiler = new CSharpCodeProvider();
ICodeCompiler compiler = cscompiler.CreateCompiler();
CompilerParameters compparams = new CompilerParameters();
compparams.GenerateInMemory = true;
CompilerResults compresult = compiler.CompileAssemblyFromSource(
compparams, source.ToString());
if ( compresult == null | compresult.Errors.Count > 0 )
throw new Exception("class error");
type = compresult.CompiledAssembly.GetType(className);
//systemreturn test;
// object o = compresult.CompiledAssembly.CreateInstance("T", false,
// BindingFlags.CreateInstance, null,new object[]{"Filip"}, null, null);
// Type test = compresult.CompiledAssembly.GetType("T");
// MethodInfo m = test.GetMethod("print");
// m.Invoke(o, null);
}
示例9: Generate
public void Generate()
{
string codes = WsdlHelper.GetCodes(this.Address);
Microsoft.CSharp.CSharpCodeProvider codePrivoder = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler codeCompiler = codePrivoder.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters compilerParameters = new System.CodeDom.Compiler.CompilerParameters();
compilerParameters.ReferencedAssemblies.Add("System.dll");
compilerParameters.ReferencedAssemblies.Add("System.XML.dll");
compilerParameters.ReferencedAssemblies.Add("System.Web.Services.dll");
compilerParameters.ReferencedAssemblies.Add("System.Data.dll");
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = true;
System.CodeDom.Compiler.CompilerResults cr = codeCompiler.CompileAssemblyFromSource(compilerParameters, codes);
if (cr.Errors.HasErrors)
{
Error = "Compile Error:";
foreach (System.CodeDom.Compiler.CompilerError err in cr.Errors)
{
Error += err.ErrorText;
}
}
else
{
this.ProxyAssemble = cr.CompiledAssembly;
}
}
示例10: Compile
/// <summary>
/// 编译源码
/// </summary>
/// <param name="sourceCode">源码</param>
/// <param name="references"></param>
/// <returns>返回错误集合</returns>
public static string Compile(string[] sourceCode, List<string> references)
{
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Core.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Data.dll");
objCompilerParameters.ReferencedAssemblies.Add("Iveely.Database.dll");
if (references != null)
{
foreach (var reference in references)
{
objCompilerParameters.ReferencedAssemblies.Add(reference);
}
}
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;
CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCode(sourceCode));
if (cr.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder();
foreach (CompilerError err in cr.Errors)
{
string errorInformation = string.Format("line:{0},Cloumn:{1},error:{2}", err.Line, err.Column,
err.ErrorText);
errors.AppendLine(errorInformation);
}
return errors.ToString();
}
return string.Empty;
}
示例11: Eval
public static object Eval(string sCSCode)
{
CSharpCodeProvider c = new CSharpCodeProvider();
ICodeCompiler icc = (ICodeCompiler)c.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
cp.ReferencedAssemblies.Add("system.xml.dll");
cp.ReferencedAssemblies.Add("system.data.dll");
cp.ReferencedAssemblies.Add("system.windows.forms.dll");
cp.ReferencedAssemblies.Add("system.drawing.dll");
cp.CompilerOptions = "/t:library";
cp.GenerateInMemory = true;
StringBuilder sb = new StringBuilder("");
sb.Append(sCSCode);
CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
if (cr.Errors.Count > 0)
{
Console.Write("ERROR: " + cr.Errors[0].ErrorText, "Error evaluating cs code");
return null;
}
System.Reflection.Assembly a = cr.CompiledAssembly;
object o = a.CreateInstance("Cgi.Cgi");
Type t = o.GetType();
MethodInfo mi = t.GetMethod("main");
object s = mi.Invoke(o, null);
return s;
}
示例12: button1_Click
private void button1_Click(object sender, EventArgs e)
{
//show savefiledialog
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Dynamic Link Libraries (*.dll)|*.dll";
if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = false;
compilerparams.OutputAssembly = sfd.FileName;
compilerparams.ReferencedAssemblies.Add("Language.dll");
CompilerResults results =
compiler.CompileAssemblyFromSource(compilerparams, textBox1.Text);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors)
{
errors.AppendFormat("Line {0},{1}\t: {2}\n",
error.Line, error.Column, error.ErrorText);
}
MessageBox.Show(errors.ToString());
}
else
{
//do i need to save?
MessageBox.Show("Success");
}
}
示例13: Compile
public static List<Error> Compile(string fileName)
{
var err = Translator.Translate(new Source(fileName));
if (err.Count != 0)
{
return err;
}
//Emitter.Emit(Translator.Root);
CSharpEmitter.Emit(Translator.Root);
var codeProvider = new CSharpCodeProvider();
var icc = codeProvider.CreateCompiler();
var parameters = new CompilerParameters
{
GenerateExecutable = true,
OutputAssembly = Translator.Root.Name.Text+".exe"
};
var results = icc.CompileAssemblyFromSource(parameters, CSharpEmitter.Source);
if (results.Errors.Count > 0)
{
}
return new List<Error>();
}
示例14: Ctor_IDictionaryStringString
public void Ctor_IDictionaryStringString(IDictionary<string, string> providerOptions)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
#pragma warning disable 0618
Assert.NotNull(provider.CreateGenerator());
Assert.Same(provider.CreateGenerator(), provider.CreateCompiler());
#pragma warning restore 0618
}
示例15: Ctor_Default
public void Ctor_Default()
{
CSharpCodeProvider provider = new CSharpCodeProvider();
#pragma warning disable 0618
Assert.NotNull(provider.CreateGenerator());
Assert.Same(provider.CreateGenerator(), provider.CreateCompiler());
#pragma warning restore 0618
}