[英]How can I embed a string resource and retrieve it after compiling with CodeDom?
I feel like this question might have been answered already but I cannot find an answer that I understand and that is specific to my example, so I'll ask. 我觉得这个问题可能已经回答了,但是我找不到我能理解的答案,并且该答案是我的示例所特有的,所以我会提出。
I have a C# application that compiles some code and I want to be able to embed a String resource (called someString) that I have inside of my Resources.resx file. 我有一个编译一些代码的C#应用程序,并且我希望能够嵌入我在Resources.resx文件中拥有的String资源(称为someString)。 Then I want to be able to access that embedded resource in the compiled program.
然后,我希望能够访问已编译程序中的嵌入式资源。 However, I am struggling to access the resource file as when I run the compiled program it says that the Stream cannot be null.
但是,当我运行已编译的程序时,它说Stream不能为null时,我正在努力访问资源文件。
Here is my example code: 这是我的示例代码:
string codeString =
@"using System;
using System.IO;
using System.Reflection;
namespace SomeProgram
{
class MyClass
{
static void Main(string[] args)
{
Assembly resourceAssembly = Assembly.GetExecutingAssembly();
StreamReader stream = new StreamReader(resourceAssembly.GetManifestResourceStream(""Resources.someString""));
string someStringValue = stream.ReadToEnd();
Console.WriteLine(someStringValue);
Console.ReadLine();
}
}
}";
// Compiler Code
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
string outFile = "output.exe";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = outFile;
parameters.EmbeddedResources.Add("..\\..\\Properties\\Resources.resx");
parameters.EmbeddedResources.Add("..\\..\\Properties\\Resources.Designer.cs");
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, codeString);
Am I accessing the embedded resource in the wrong way in the compiled code? 我在编译后的代码中以错误的方式访问嵌入式资源吗?
回答我自己的问题:找到了一种方法(这可能不是最好的方法),但是它涉及从嵌入式资源文件中读取流,然后稍后将其解析为所需的字符串值。
StreamReader stream = new StreamReader(resourceAssembly.GetManifestResourceStream(""Resources.resx""))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.