开发者

C# CodeDom Add embedded resource without first dropping file to disk

In CodeDom, it is possible to add an embedded resource to your dynamically created file by using the CompilerParameters.EmbeddedResources property. In my project, I am adding some bytes of data as an embedded resource to my dynamically created file (as shown below).

Byte[] bytes = new byte[] {1,2,3,4,5};

// Write the data to disk (I would like to avoid this step!).
File.WriteAllBytes(@"C:\EmbeddedResource.exe", bytes); 

CompilerParameters cp;
cp.EmbeddedResources.Add(@"C:\Embedded开发者_JAVA百科Resource.exe");

Is there some way I can store the data of 'bytes' in memory, and add it as an embedded resource directly from memory.

Thank you for any advice,

Evan


I'm afraid you can't. Looking into the CSharpCodeGenerator (with Telerik's JustDecompile or Redgate's .NET Reflector), I found the usage of the CompilerParameters class:

private string CmdArgsFromParameters(CompilerParameters options)
{
    StringBuilder stringBuilder = new StringBuilder(128);
    // ...
    StringEnumerator stringEnumerator = options.EmbeddedResources.GetEnumerator();
    try
    {
        while (stringEnumerator.MoveNext())
        {
            string str = stringEnumerator.Current;
            stringBuilder.Append("/res:\"");
            stringBuilder.Append(str);
            stringBuilder.Append("\" ");
        }
    }
    finally
    {
        IDisposable disposable2 = stringEnumerator as IDisposable;
        if (disposable2 != null)
        {
            disposable2.Dispose();
        }
    }
    // ...
    return stringBuilder.ToString();
}

They use it to create a string with all command-line parameters that are passed to the C# compiler exe (http://msdn.microsoft.com/en-us/library/6ds95cz0%28v=VS.100%29.aspx). So, at the end, I guess your code is simply compiled with csc.exe.

[]'s

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜