テンプレート エンジンは Visual Studio の再配布可能な部分ではないため、実行時には、前処理されたテンプレートのみを変換できます。パラメータ ディレクティブを使用して、オブジェクトを前処理済みのテンプレートに渡すことができます。テンプレートに渡すオブジェクト タイプは、SerializableAttribute
. メソッドを呼び出す前TransformText()
に、パラメーターの値をテンプレート セッションに入れます。
前処理されたテンプレートを使用する場合、出力拡張ディレクティブは無視されます。このTransformText()
メソッドは、生成されたコードを含む文字列を返します。任意のファイル形式で保存できます。
<#@ template debug="true" #>
<#@ parameter name="MyObject" type="MyNamespace.MyType" #>
<#
// now access the passed parameter using
this.MyObject
#>
preprocessedTemplate を呼び出します。
var templateInstance = new MyTemplate();
templateInstance.Session = new Dictionary<string, object>();
templateInstance.Session.Add("MyObject", new MyType());
templateInstance.Initialize();
var generatedCode = templateInstance.TransformText();
System.IO.File.WriteAllText("outputfile.java", generatedCode);
お役に立てれば。