2013-05-02 10 views
6

の別の配列にT4テンプレートコードへのバイト配列を渡します。私は次のコードを使用してアセンブリを作成するためにT4TemplateとのCodeDOMを使用しています同じタイプ

CompilerParameters Params = new CompilerParameters(); 
      Params.GenerateExecutable = true; 
      Params.ReferencedAssemblies.Add("System.dll"); 
      Params.OutputAssembly = "myfile.exe"; 

      RuntimeTextTemplate1 RTT = new RuntimeTextTemplate1(); 
      string Source = RTT.TransformText(); 

      CompilerResults Create = new CSharpCodeProvider().CompileAssemblyFromSource(Params, Source); 

テンプレートは、次のようになります(今のところ) :私は、特定のアプリケーションのいくつかのバイトを含むバイト配列を持つメインアプリケーションで

<#@ template language="C#" #> 
namespace Application 
{ 
    class Program 
    { 
    static void Main() 
    { 
     byte[] buffer = new byte[1024]; 
     //And some code for creating a file with the bytes in the buffer. 
    } 
    } 
} 

、これらのバイトは、実行時に、アレイ内にロードされています。

私の質問は次のとおりです。

  • 私は(byte[] buffer = new byte[1024];)に T4Templateにデータが含まれているバイトの配列(バイト)を渡すことができますどのように アセンブリがで書かれたコードを使用して作成されたときテンプレート、配列 にはバイトが含まれている必要があります。個人的に

答えて

0

、私は、実行時に生成されたコードにデータを渡すことを好むだろうが、あなたが本当にあなたの生成されたアセンブリ内に埋め込む必要がある場合、あなたはこのような何かを行うことができます:

を追加バイト配列パラメータをメンバーとして持つクラス・フィーチャ・ブロックを作成すると、ランタイム・テンプレートからそれを設定し、テンプレートにアクセスできます。次

<#@ template language="C#" #> 
namespace Application 
{ 
    class Program 
    { 
    static void Main() 
    { 
     byte[] buffer = new byte[1024] { <#= BufferValue.Select(b => "0x" + b.ToString("X2")).Aggregate((f, s) => f + ", " + s) #> }; 
     //And some code for creating a file with the bytes in the buffer. 
    } 
    } 
} 
<#+ 

public byte[] BufferValue { get; set; } 

#> 

のようなものは、それからちょうどテンプレートを呼び出す前に、あなたの配列にRTT.BufferValueを設定します。

関連する問題