- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始使用 CodeDom 进行一些试验,并制作了一个简单的应用程序,该应用程序从用户输入中收集源代码并尝试使用 C# 语法对其进行编译。
对于那些想尝试整个过程的人,输入end... 来完成源代码入口。
例子如下:
using System;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
namespace CodeDomTest
{
class Program
{
static void Main(string[] args)
{
getTestCode();
}
public static Assembly getTestCode()
{
CompilerParameters CompilerOptions = new CompilerParameters(
assemblyNames: new String[] { "mscorlib.dll", "System.dll", "System.Core.dll" },
outputName: "test.dll",
includeDebugInformation: false)
{ TreatWarningsAsErrors = true, WarningLevel = 0, GenerateExecutable = false, GenerateInMemory = true };
List<String> newList = new List<String>();
String a = null;
while(a != "end...")
{
a = Console.ReadLine();
if (!a.Equals( "end..."))
newList.Add(a);
}
String[] source = { "class Test {static void test() {System.Console.WriteLine(\"test\");}}" };
source = newList.ToArray();
CSharpCodeProvider zb = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v4.0" } });
CompilerResults Results = zb.CompileAssemblyFromSource(CompilerOptions, source);
Console.WriteLine(Results.Errors.HasErrors);
CompilerErrorCollection errs = Results.Errors;
foreach(CompilerError z in errs)
{
Console.WriteLine(z.ErrorText);
}
if (!(errs.Count > 0))
{
AssemblyName assemblyRef = Results.CompiledAssembly.GetName();
AppDomain.CurrentDomain.Load(assemblyRef);
//foreach (String a in )
Console.WriteLine(Results.CompiledAssembly.FullName.ToString());
Type tempType = Results.CompiledAssembly.GetType("Test");
MethodInfo tempMethodInfo = tempType.GetMethod("test", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
if (tempMethodInfo != null)
tempMethodInfo.Invoke(null,null);
}
Console.ReadLine();
return null;
}
}
}
现在你可以看到,基本上它编译了下面的代码:
class Test {static void test() {System.Console.WriteLine(\"test\");}}
如果你像这样输入它(不带“)作为程序的用户输入,效果很好。但是一旦你在一个完成的行后按回车键插入换行符,编译就会中断并出现几个错误。看起来像它会通过给出以下语句将每一行评估为自己的程序:
} expected
Expected class, delegate, enum, interface, or struct
A namespace cannot directly contain members such as fields or methods
A namespace cannot directly contain members such as fields or methods
Type or namespace definition, or end-of-file expected
Type or namespace definition, or end-of-file expected
对于以下输入:
class Test
{
static void test()
{
System.Console.WriteLine
("test");
}
}
然后我必须将用户(自定义)条目分成一行吗?
最佳答案
sources 中的每一行都应该包含完整的源代码,而不是单行代码。由于您是将代码逐行收集到源数组中,因此您必须将其折叠成一个字符串,然后将该字符串添加到一个数组中以传递给 CompileAssemblyFromSource
试试这个:
while (a != "end...")
{
a = Console.ReadLine();
if (!a.Equals("end..."))
newList.Add(a);
}
string code = string.Join("\r\n", newList);
string[] source = new string[] { code };
关于c# - 使用 CodeDom 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39018394/
我使用 visual studio 发布选项向我的主机发布了我的网站。 我的主机使用 asp.net 4.5.1 框架。 它在本地运行良好,但在我发布之后,却出现了这个错误。 我的本地 visua
这是一个使用VS2015的WebApi项目。 重现步骤: 创建一个空的 WebApi 项目 将构建输出路径从“bin\”更改为“bin\Debug\” 运行 在我将构建输出路径从“bin\”更改为“b
我正在调查 System.CodeDom namespace用于独立于语言(至少在一定范围内)的源代码生成,我发现一些信息不鼓励使用 CodeDom . 我认为 this early blogpost
有人知道如何使用 CodeDom 调用基类的泛型方法吗? 我调用标准方法没有问题,但我找不到调用泛型的解决方案。 我用来调用标准基类方法 GetInstance 的代码: CodeAssignStat
如何使用 CodeDom 生成调用其他类 Constructor 的代码我找不到任何 CodeConstructorInvokeExpression。 最佳答案 我相信您正在寻找CodeObjectC
我有一个在 C# 中使用 WPF 的程序,它使用 .NET 4 Framework(不是客户端配置文件),它编译了一个名为“Source.txt”的源文件。但是,无论何时编译它,我都会收到此错误“错误
我需要向 CodeNamespace 添加一个类 (Type)。我已经编写了一个 C# 类,但我不想将其手动转换为 CodeDOM。有没有办法将现有的加载到 CodeTypeDeclaration。
我正在使用 CodeDom 生成稍后要编译的代码,并且我注意到某些结构会创建额外的括号集。虽然我知道它们不会影响任何东西,但它们看起来确实很奇怪。 执行此操作的代码示例是这样的: new CodeCo
我想为我的应用程序构建一个构建器,这是构建器的来源 using System; using System.Collections.Generic; using System.ComponentMode
我正在尝试使用 CodeDom 生成一些方法,但在为这些方法生成自定义属性时遇到问题。 我可以管理简单的空属性,比如 [DataMember()] 或带有字符串值参数的属性, [DataContrac
有没有办法使用 C# CodeDom 生成字典初始值设定项?这些是否受支持? 我想要: private IDictionary map = new Dictionary { { "Name",
在 visual studio 中,我可以单击“引用”>“添加引用”并从我的计算机浏览到现有的 .dll 文件。然后我可以按如下方式使用引用的 dll: dllNameSpace.dllClassNa
如标题所示,我正在尝试使用 CodeDom 在 C# 的数组中“嵌套”或创建数组。 这是我要复制的行: T.Invoke(null, new object[] { new string[] {} }
我有一个使用 CodeDom 创建的属性。如何将其设置为自动属性,而不是针对私有(private)成员添加 CodeFieldReferenceExpressions? 最佳答案 IIRC,CodeD
有没有办法用 CodeDom 生成类约束。 因为当我使用类似的东西时 var method = new CodeMemberMethod(); var genericParam = new CodeT
好的,我觉得我的问题的答案在网上,但我找不到。我要做的就是将一个文本资源文件添加到我正在使用 CodeDom 编译的程序中,然后在已编译的程序中访问该文本文件。为了添加嵌入式资源,我使用了以下代码:
我正在寻找为使用 codeDOM 生成的 exe 文件设置文件版本的任何方法。我的始终显示为 0.0.0.0。以编程方式显然是首选,但在这一点上,有总比没有好。 最佳答案 编译程序集的版本由 Asse
我从 SE 找到了使用 CodeDOM 的类名验证方法,并使用了它: System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentI
是否可以仅使用常规 .NET 中的 System.CodeDom 创建 Silverlight 应用程序(.xap 文件、testpage.html、ClientBin 旁边的内容资源、浏览器外设置等
在 StackExchange 上有几个与此类似的问题,但它们并未涵盖完全相同的情况。请在将其标记为重复之前通读它! 在 C# 中使用 CodeDom,我试图为字典生成一个初始化表达式。例如 Dict
我是一名优秀的程序员,十分优秀!