我正在尝试使用CSharpCodeProvider编译下面的代码.该文件已成功编译,但是当我单击生成的EXE文件时,我收到错误(Windows正在搜索此问题的解决方案)并且没有任何反应.
当我使用CSharpCodeProvider编译下面的代码时,我MySql.Data.dll使用以下代码行添加了作为嵌入式资源文件:
if (provider.Supports(GeneratorSupport.Resources))
cp.EmbeddedResources.Add("MySql.Data.dll");
Run Code Online (Sandbox Code Playgroud)
该文件已成功嵌入(因为我注意到文件大小增加).
在下面的代码中,我尝试提取嵌入的DLL文件并将其保存到System32,但下面的代码由于某种原因不起作用.
namespace ConsoleApplication1
{
class Program
{
public static void ExtractSaveResource(String filename, String location)
{
//Assembly assembly = Assembly.GetExecutingAssembly();
Assembly a = .Assembly.GetExecutingAssembly();
//Stream stream = assembly.GetManifestResourceStream("Installer.Properties.mydll.dll"); // or whatever
//string my_namespace = a.GetName().Name.ToString();
Stream resFilestream = a.GetManifestResourceStream(filename);
if (resFilestream != null)
{
BinaryReader br = new BinaryReader(resFilestream);
FileStream fs = new FileStream(location, FileMode.Create); // Say
BinaryWriter bw = new BinaryWriter(fs);
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
bw.Write(ba);
br.Close();
bw.Close(); …Run Code Online (Sandbox Code Playgroud) 我有一个包含按钮的WPF C#应用程序.
按钮单击的代码写在单独的文本文件中,该文件将放在应用程序运行时目录中.
我想在单击按钮时执行放置在文本文件中的代码.
知道怎么做吗?
我正在使用这些System.CodeDom功能在运行时编译代码,我想知道是否可以指定编译器参数或其他解决方法来显示英语语言中的编译器错误,而不是使用系统的默认语言.
但是,在MSDN文档中,我找不到与显示语言相关的任何内容:
当我想在构建项目时在Visual Studio IDE下以特定语言显示编译器错误我所做的是更改当前文化,然后我尝试在我的应用程序中设置文化,也在我正在编译的文件中设置文化appliation,但首先不会生效,其次我会避免这样的可能的伎俩,因为它可能需要自动代码生成(调用CodeDomProvider编译器时的导入,引用和代码):
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-Us")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-Us")
Run Code Online (Sandbox Code Playgroud) 我有一个简单的查询如下:
var employeeTeam = Session.Query<EmployeeTeam>()
.Where(x => x.StartEffective <= competency.FinalDate && // competency.FinalDate is a DateTime
employeesIds.Contains(x.EmployeeId)) // employeeIds is a List<long>
.OrderByDescending(x => x.StartEffective)
.Select(x => new
{
x.EmployeeId,
x.StartEffective,
x.Team
}).ToList();
Run Code Online (Sandbox Code Playgroud)
它成功运行一次,但是当第二次执行时(或者第三次,第四次等),它会抛出一个无效的强制转换异常,例如:
致命错误:System.InvalidCastException:无法将类型'System.Linq.EnumerableQuery`1 [<> f__AnonymousType0`3 [System.Int64,System.DateTime,Team]]'转换为'System.Collections.Generic.IEnumerable`1 [< > f__AnonymousType0`3 [System.Int64,System.DateTime的,小组]]".在NHibernate.Linq.DefaultQueryProvider.Execute [TResult](表达式表达式)
其余的筹码追踪表现得非常勇敢.
查询始终在错误之前在数据库中执行.它不返回任何记录,但它没问题.如果我重新构建解决方案并再次运行,则第一次执行查询,然后在我运行它时开始抛出异常.其他查询每次运行都没有任何问题.我不知道导致错误的原因.
重要的是说这段代码是在CSharpCodeProvider环境中运行的,但我不知道它是否会有所作为.
UPDATE
即使使用最简单的查询形式,它也会发生:
var employeeTeam = Session.Query<EmployeeTeam>()
.Select(x => new
{
x.Id
}).ToList();
Run Code Online (Sandbox Code Playgroud)
它只是第一次运行正常.但是,如果我将annon对象更改{ x.Id }为{ x.TeamId },例如,它在第一次运行正常,则异常再次发生.
更新2
我只是意识到,如果我将以下属性添加到annon对象,则查询每次都有效:
Rnd = (new Random().Next(1, 999))
Run Code Online (Sandbox Code Playgroud)
那么,缓存问题可能呢?
更新3
我从更新NHibernate的3.3 …
我有一个唯一的c#源文件source.cs,我使用CSharpCodeProvider从构建器编译来获取可执行文件.
我会在构建器上添加一个选项,是否在应用程序启动时显示"关于"表单.
如何创建一个标题为关于我们的表单然后添加控件(标签,RichTextEdit等..)
就像是
if (display_about_dialog) {
// code to display the form }
Run Code Online (Sandbox Code Playgroud)
任何帮助将受到高度赞赏
我正在尝试使用来自.net 3.5应用程序的CSharpCodeProvider编译.cs文件,我想要定位.net4编译器,但我收到此错误"无法找到编译器可执行文件csc.exe".我安装了.net4.下面是我正在使用的代码,为简洁省略了一些行.当我将CompilerVersion设置为"v3.5"时,我没有错误.
CompilerResults results = null;
using (CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string>() {{"CompilerVersion", "v4.0"},}))
{
CompilerParameters options = new CompilerParameters();
...
results = provider.CompileAssemblyFromFile(options, Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories));
}
Run Code Online (Sandbox Code Playgroud) 我最近安装了Visual Studio 11 Beta,我正在尝试更新现有的4.0项目以使用4.5.在程序中,它使用编译一些动态生成的代码CSharpCodeProvider.
/// <summary>
/// Compile and return a reference to the compiled assembly
/// </summary>
private Assembly Compile()
{
var referencedDlls = new List<string>
{
"mscorlib.dll",
"System.dll",
"System.Core.dll",
};
referencedDlls.AddRange(RequiredReferences);
var parameters = new CompilerParameters(
assemblyNames: referencedDlls.ToArray(),
outputName: GeneratedDllFileName,
// only include debug information if we are currently debugging
includeDebugInformation: Debugger.IsAttached);
parameters.TreatWarningsAsErrors = false;
parameters.WarningLevel = 0;
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = false;
parameters.CompilerOptions = "/optimize+ /platform:x64";
string[] files = Directory.GetFiles(GenerationDirectory, "*.cs");
var compiler = …Run Code Online (Sandbox Code Playgroud) 在本主题中连接: 如何将我的类中的字符串连接到表单
我试图做与他们的答案相关的解决方案(特别是杰里米先生的回答),但这个错误一直在出现
由于其保护级别,'KeyWord.KeyWord.keywords'无法访问
KeyWords.cs的代码:
namespace KeyWord
{
public class KeyWord
{
String[] keywords = { "abstract", "as", "etc." };
}
}
Run Code Online (Sandbox Code Playgroud)
main.cs的代码
// Check whether the token is a keyword.
var keyboardCls = new KeyWord.KeyWord();
String[] keywords = keyboardCls.keywords;
for (int i = 0; i < keywords.Length; i++)
{
if (keywords[i] == token)
{
// Apply alternative color and font to highlight keyword.
rtb.SelectionColor = Color.Blue;
rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
我正在尝试使用C#代码提供程序从我的代码编译程序集.
当我使用compilerResult.CompiledAssembly访问已编译的程序集时,一切正常.但是,当我改为执行Assembly.Load(路径)时,我得到以下异常:
System.IO.FileLoadException:无法加载文件或程序集"C:\ Users\Name\Desktop\output.dll"或其依赖项之一.给定的程序集名称或代码库无效.(HRESULT异常:0x80131047)
我究竟做错了什么?
这是代码:
[Test]
public static void CompileCodeIntoAssembly()
{
var code = "public class X { }";
var file = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.cs");
File.WriteAllText(file, code);
using (var provider = new CSharpCodeProvider())
{
var parameters = new CompilerParameters
{
GenerateInMemory = false, // we want the dll saved to disk
GenerateExecutable = false,
CompilerOptions = "/target:library /lib:\"" + typeof(Class2).Assembly.Location + "\"",
OutputAssembly = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.dll"),
};
parameters.ReferencedAssemblies.AddRange(new[]
{
"System.dll",
typeof(Class1).Assembly.Location,
});
var compilerResult = provider.CompileAssemblyFromFile(parameters, file);
if (compilerResult.Errors.Count …Run Code Online (Sandbox Code Playgroud) 在下面的示例中...而不是编译.Net"v4.0",应该提供什么来编译PCL库?
var compiler = new CSharpCodeProvider(new Dictionary<string, string>
{
{"CompilerVersion", "v4.0"}
});
Run Code Online (Sandbox Code Playgroud)