本文整理汇总了C#中System.CodeDom.Compiler.CodeDomProvider.CreateCompiler方法的典型用法代码示例。如果您正苦于以下问题:C# CodeDomProvider.CreateCompiler方法的具体用法?C# CodeDomProvider.CreateCompiler怎么用?C# CodeDomProvider.CreateCompiler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.CodeDom.Compiler.CodeDomProvider
的用法示例。
在下文中一共展示了CodeDomProvider.CreateCompiler方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
public static Assembly Compile(CodeDomProvider cp, string scriptCode, IList assemblies)
{
ICodeCompiler ic = cp.CreateCompiler();
CompilerParameters cpar = GetCompilerParameters(assemblies);
CompilerResults cr = ic.CompileAssemblyFromSource(cpar, scriptCode);
bool errors = false;
if (cr.Errors.Count > 0)
{
StringBuilder sb = new StringBuilder("Error compiling the composition script:\n");
foreach (CompilerError ce in cr.Errors)
{
if (!ce.IsWarning)
{
errors = true;
sb.Append("\nError number:\t")
.Append(ce.ErrorNumber)
.Append("\nMessage:\t ")
.Append(ce.ErrorText)
.Append("\nLine number:\t")
.Append(ce.Line);
}
}
if (errors)
{
throw new PicoCompositionException(sb.ToString());
}
}
return cr.CompiledAssembly;
}
示例2: GetCompiledType
public override Type GetCompiledType ()
{
Type type = CachingCompiler.GetTypeFromCache (parser.PhysicalPath);
if (type != null)
return type;
if (parser.Program.Trim () == "") {
type = parser.GetTypeFromBin (parser.ClassName);
CachingCompiler.InsertType (type, parser.PhysicalPath);
return type;
}
string lang = parser.Language;
CompilationConfiguration config;
config = CompilationConfiguration.GetInstance (parser.Context);
provider = config.GetProvider (lang);
if (provider == null)
throw new HttpException ("Configuration error. Language not supported: " +
lang, 500);
compiler = provider.CreateCompiler ();
compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
compilerParameters.IncludeDebugInformation = parser.Debug;
compilerParameters.CompilerOptions = config.GetCompilerOptions (lang);
compilerParameters.WarningLevel = config.GetWarningLevel (lang);
bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
TempFileCollection tempcoll;
tempcoll = new TempFileCollection (config.TempDirectory, keepFiles);
compilerParameters.TempFiles = tempcoll;
if (!Directory.Exists (dynamicBase))
Directory.CreateDirectory (dynamicBase);
inputFile = tempcoll.AddExtension (provider.FileExtension);
Stream st = File.OpenWrite (inputFile);
StreamWriter sw = new StreamWriter (st);
sw.WriteLine (parser.Program);
sw.Close ();
string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
compilerParameters.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
CompilerResults results = CachingCompiler.Compile (this);
CheckCompilerErrors (results);
if (results.CompiledAssembly == null)
throw new CompilationException (inputFile, results.Errors,
"No assembly returned after compilation!?");
results.TempFiles.Delete ();
type = results.CompiledAssembly.GetType (parser.ClassName, true);
CachingCompiler.InsertType (type, parser.PhysicalPath);
return type;
}
示例3: Compile
public Assembly Compile(CodeDomProvider codeDomProvider, object scriptCode, IList assemblies )
{
ICodeCompiler codeCompiler = codeDomProvider.CreateCompiler();
CompilerParameters compilerParameters = GetCompilerParameters(assemblies);
CompilerResults compilerResults = null;
if(scriptCode is string)
{
compilerResults = codeCompiler.CompileAssemblyFromSource(compilerParameters, scriptCode as string);
}
else
{
compilerResults = codeCompiler.CompileAssemblyFromDom(compilerParameters, scriptCode as CodeCompileUnit);
}
handleErrors(compilerResults);
return compilerResults.CompiledAssembly;
}
示例4: CompileScript
public static CompilerResults CompileScript(string Source, string Reference, CodeDomProvider Provider)
{
ICodeCompiler compiler = Provider.CreateCompiler();
CompilerParameters parms = new CompilerParameters();
CompilerResults results;
// Configure parameters
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.IncludeDebugInformation = false;
if (Reference != null && Reference.Length != 0)
parms.ReferencedAssemblies.Add(Reference);
parms.ReferencedAssemblies.Add("System.Windows.Forms.dll");
parms.ReferencedAssemblies.Add("System.dll");
// Compile
results = compiler.CompileAssemblyFromSource(parms, Source);
return results;
}
示例5: CheckCode
/// <summary>Check code with given CodeDomProvider.</summary>
/// <example>
/// <code lang="CS" description="The following example gets some code and check it with given CodeDomProvider">
/// string code = "double Calc(double x)
/// {
/// double gain = 1.5;
/// return x*gain;
/// }
/// "
/// String resultString;
///
/// CodeExpressionChecker.CheckCode(new CSharpCodeProvider(), code, ref resultString);
/// </code>
/// </example>
public static void CheckCode(CodeDomProvider provider, String sourceCode, ref String errorString)
{
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters cp = new CompilerParameters(new string[]{"mscorlib.dll", typeof(Point2D).Assembly.Location});
cp.GenerateInMemory = true;
CompilerResults cr = compiler.CompileAssemblyFromSource(cp, sourceCode);
if (cr.NativeCompilerReturnValue != 0)
{
int i = resultSpace;
string outputString = "";
while(i < cr.Output.Count)
{
outputString += cr.Output[i] + Environment.NewLine;
i++;
}
// Return the results of compilation with error flag and error string
errorString = outputString;
}
else
errorString = "";
}
示例6: Run
/// <summary>
/// This overrides the CodeDomTest Run method that does verification
/// on the tree provided in the BuildTree method you provide.
/// </summary>
/// <param name="provider">Provider to test.</param>
/// <returns>True if the tree builds, compiles, searches and passes
/// assembly verification. False if any of these fails.</returns>
public override bool Run (CodeDomProvider provider) {
bool fail = false;
// build the tree
LogMessageIndent ();
LogMessage ("- Generating tree.");
CodeCompileUnit cu = new CodeCompileUnit ();
LogMessageIndent ();
BuildTree (provider, cu);
LogMessageUnindent ();
// validate tree using 'experimental' subset tester
// but only if the test believes its in the subset
if ((TestType & TestTypes.Subset) != 0) {
SubsetConformance subsConf = new SubsetConformance ();
LogMessage ("- Checking tree subset conformance.");
if (!subsConf.ValidateCodeCompileUnit (cu))
LogMessage ("Failed subset tester: {0}",
subsConf.ToString ());
}
// realize source
StringWriter sw = new StringWriter (CultureInfo.InvariantCulture);
#if WHIDBEY
provider.GenerateCodeFromCompileUnit (cu, sw, GetGeneratorOptions (provider));
#else
ICodeGenerator generator = provider.CreateGenerator ();
generator.GenerateCodeFromCompileUnit (cu, sw, GetGeneratorOptions (provider));
#endif
// only continue if the source could be realized into a string.
if (!fail) {
string source = sw.ToString ();
if (saveSourceFileName.Length > 0) {
LogMessage ("- Saving source into '" + saveSourceFileName + "'");
// save this source to a file
DumpStringToFile (source, saveSourceFileName);
}
// log the source code
//LogMessage (source);
// search the source if the test case asks us to
if (ShouldSearch) {
LogMessageIndent ();
Search (provider, source);
LogMessageUnindent ();
}
// continue only if the test case wants to compile or verify
if (ShouldCompile || ShouldVerify) {
// ask the test case which compiler parameters it would like to use
CompilerParameters parms = GetCompilerParameters (provider);
#if FSHARP
// If the generated code has entrypoint, then F# requires us to generate EXE
bool hasEntryPoint = false;
foreach(CodeNamespace ns in cu.Namespaces)
foreach (CodeTypeDeclaration ty in ns.Types)
foreach(CodeTypeMember mem in ty.Members)
if (mem is CodeEntryPointMethod) { hasEntryPoint = true; }
// If the output file name is specified then it should be EXE
if (hasEntryPoint && parms.GenerateExecutable == false)
{
parms.GenerateExecutable = true;
if (saveAssemblyFileName.ToLower().EndsWith(".dll"))
saveAssemblyFileName = saveAssemblyFileName.Substring(0, saveAssemblyFileName.Length - 4) + ".exe";
}
#endif
// add the appropriate compiler parameters if the user asked us
// to save assemblies to file
if (saveAssemblyFileName.Length > 0) {
parms.OutputAssembly = saveAssemblyFileName;
LogMessage ("- Compiling to '" + saveAssemblyFileName + "'.");
}
// always generate in memory for verification purposes
parms.GenerateInMemory = true;
// compile!
#if WHIDBEY
CompilerResults results = provider.CompileAssemblyFromDom (parms, cu);
#else
ICodeCompiler compiler = provider.CreateCompiler ();
CompilerResults results = compiler.CompileAssemblyFromDom (parms, cu);
#endif
if (results.NativeCompilerReturnValue != 0) {
//.........这里部分代码省略.........
示例7: CompilerInfo
public CompilerInfo(CodeDomProvider provider)
{
Compiler = provider.CreateCompiler();
CodeGen = provider.CreateGenerator();
}
示例8: LoadClass
///<remarks>Brendan Ingram has greatly improved this method.</remarks>
private static object LoadClass(CodeDomProvider codeProvider, string targetClassName, string source, bool sourceIsString)
{
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = true;
compilerParameters.IncludeDebugInformation = true;
compilerParameters.TreatWarningsAsErrors = false;
// Add the reference to the NxBRE.dll assembly.
if ((ReferenceLinkMode == ReferenceLinkModes.NxBRE) || (ReferenceLinkMode == ReferenceLinkModes.Full))
AddReferencedAssembly(compilerParameters, NxbreAssemblyLocation);
// Add all implicitly referenced assemblies
if ((ReferenceLinkMode == ReferenceLinkModes.CurrentDomain) || (ReferenceLinkMode == ReferenceLinkModes.Full))
foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
// do not add AssemblyBuilders (bug 1482753), thanks to Bob Brumfield
if (!(assembly is AssemblyBuilder))
AddReferencedAssembly(compilerParameters, assembly.Location);
ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerResults cr;
if (sourceIsString)
cr = compiler.CompileAssemblyFromSource(compilerParameters, source);
else
cr = compiler.CompileAssemblyFromFile(compilerParameters, source);
if (cr.Errors.Count == 0)
return cr.CompiledAssembly.CreateInstance(targetClassName);
else
throw new BREException(GetCompilerErrors(cr));
}
示例9: CompileAssembly
private void CompileAssembly(CodeDomProvider provider, Hashtable typeDecls, string nsName) {
nsName = "Microsoft.Xslt.CompiledScripts." + nsName;
CodeNamespace msXslt = new CodeNamespace(nsName);
// Add all the default namespaces
foreach(string ns in _defaultNamespaces) {
msXslt.Imports.Add(new CodeNamespaceImport(ns));
}
foreach(CodeTypeDeclaration scriptClass in typeDecls.Values) {
msXslt.Types.Add(scriptClass);
}
CodeCompileUnit unit = new CodeCompileUnit(); {
unit.Namespaces.Add(msXslt);
// This settings have sense for Visual Basic only.
// We can consider in future to allow user set them in <xsl:script option="???"> attribute.
unit.UserData["AllowLateBound"] = true; // Allow variables to be undeclared
unit.UserData["RequireVariableDeclaration"] = false; // Allow variables to be declared untyped
}
CompilerParameters compilParams = new CompilerParameters(); {
compilParams.GenerateInMemory = true;
compilParams.ReferencedAssemblies.Add(typeof(XPathNavigator).Module.FullyQualifiedName);
compilParams.ReferencedAssemblies.Add("system.dll");
// compilParams.ReferencedAssemblies.Add("microsoft.visualbasic.dll"); This is not requied somehow.
}
CompilerResults results = provider.CreateCompiler().CompileAssemblyFromDom(compilParams, unit);
if (results.Errors.HasErrors) {
StringWriter stringWriter = new StringWriter();
foreach (CompilerError e in results.Errors) {
stringWriter.WriteLine(e.ToString());
}
System.Diagnostics.Debug.WriteLine(stringWriter.ToString());
throw new XsltException(Res.Xslt_ScriptCompileErrors, stringWriter.ToString());
}
Assembly assembly = results.CompiledAssembly;
foreach(DictionaryEntry entry in typeDecls) {
string ns = (string)entry.Key;
CodeTypeDeclaration scriptClass = (CodeTypeDeclaration)entry.Value;
this.stylesheet.ScriptObjectTypes.Add(ns, assembly.GetType(nsName + "." + scriptClass.Name));
}
}
示例10: CompileScript
public static CompilerResults CompileScript(
string sSource, string sReference, string sAssembly,
CodeDomProvider pProvider)
{
ICodeCompiler pCompiler = pProvider.CreateCompiler();
CompilerParameters pParams = new CompilerParameters();
// configure parameters
pParams.GenerateExecutable = false;
pParams.GenerateInMemory = false;
pParams.OutputAssembly = sAssembly + ".dll";
pParams.IncludeDebugInformation = false;
if (sReference != null && sReference.Length != 0)
{
pParams.ReferencedAssemblies.Add(sReference);
}
pParams.ReferencedAssemblies.Add("System.dll");
pParams.ReferencedAssemblies.Add("System.Drawing.dll");
// compile
return pCompiler.CompileAssemblyFromSource(pParams, sSource);
}
示例11: LoadClass
///<remarks>Brendan Ingram has greatly improved this method.</remarks>
private static object LoadClass(CodeDomProvider codeProvider, string targetClassName, string source, bool sourceIsString) {
if (compilerParameters == null) {
compilerParameters = new CompilerParameters();
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = true;
compilerParameters.IncludeDebugInformation = true;
compilerParameters.TreatWarningsAsErrors = false;
// Add the referent to the NxBRE.dll assembly.
compilerParameters.ReferencedAssemblies.Add(NxbreAssemblyLocation);
// Add all implicitly referenced assemblies
foreach(Assembly assy in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
compilerParameters.ReferencedAssemblies.Add(assy.Location);
}
catch {}
}
}
ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerResults cr;
if (sourceIsString)
cr = compiler.CompileAssemblyFromSource(compilerParameters, source);
else
cr = compiler.CompileAssemblyFromFile(compilerParameters, source);
if (cr.Errors.Count == 0)
return cr.CompiledAssembly.CreateInstance(targetClassName);
else
throw new BREException(GetCompilerErrors(cr));
}
示例12: ToAssemblyFile
public void ToAssemblyFile(CodeCompileUnit compileunit, CodeDomProvider provider, string fileName)
{
#if NET2
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.GenerateInMemory = false;
cp.OutputAssembly = fileName;
CompilerResults cr = provider.CompileAssemblyFromDom(cp, compileunit);
#else
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add( "System.dll" );
cp.GenerateInMemory = false;
cp.OutputAssembly = fileName;
CompilerResults cr = compiler.CompileAssemblyFromDom(cp, compileunit);
#endif
}
示例13: ToCompilerResults
public CompilerResults ToCompilerResults(CodeCompileUnit compileunit, CodeDomProvider provider, IDomainMap domainMap)
{
#if NET2
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.GenerateInMemory = true;
string code = ToCode(compileunit, provider, domainMap);
return provider.CompileAssemblyFromSource(cp, code);
#else
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add( "System.dll" );
cp.GenerateInMemory = true;
string code = ToCode(compileunit, provider, domainMap);
return compiler.CompileAssemblyFromSource(cp, code);
#endif
}
示例14: GetCompiledType
public virtual Type GetCompiledType ()
{
Type type = CachingCompiler.GetTypeFromCache (parser.InputFile);
if (type != null)
return type;
Init ();
string lang = parser.Language;
CompilationConfiguration config;
config = CompilationConfiguration.GetInstance (parser.Context);
provider = config.GetProvider (lang);
if (provider == null)
throw new HttpException ("Configuration error. Language not supported: " +
lang, 500);
compiler = provider.CreateCompiler ();
CreateMethods ();
compilerParameters.IncludeDebugInformation = parser.Debug;
compilerParameters.CompilerOptions = config.GetCompilerOptions (lang) + " " +
parser.CompilerOptions;
compilerParameters.WarningLevel = config.GetWarningLevel (lang);
bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
if (!Directory.Exists (dynamicBase))
Directory.CreateDirectory (dynamicBase);
TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, keepFiles);
compilerParameters.TempFiles = tempcoll;
string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
compilerParameters.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
CompilerResults results = CachingCompiler.Compile (this);
CheckCompilerErrors (results);
Assembly assembly = results.CompiledAssembly;
if (assembly == null) {
if (!File.Exists (compilerParameters.OutputAssembly))
throw new CompilationException (parser.InputFile, results.Errors,
"No assembly returned after compilation!?");
assembly = Assembly.LoadFrom (compilerParameters.OutputAssembly);
}
results.TempFiles.Delete ();
return assembly.GetType (mainClassExpr.Type.BaseType, true);
}