随笔 - 159  文章 - 1  评论 - 195 
学着TerryLee 的把生成代码输出到文件的方式(原贴地址).但总是在在属性框里不出现任何东西呢。结果如下图:


我用的是CodeSmith v2.6,
模板为:

%@ CodeTemplate Language="C#" TargetLanguage="C#" Inherits="OutputFileCodeTemplate" Description="Build custom access code." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Windows.Forms.Design"%>
<%@ Assembly Name="System.Design" %>

<script runat="template">
// Override the OutputFile property and assign our specific settings to it.
[FileDialogAttribute(FileDialogType.Save, Title="Select Output File", Filter="C# Files (*.cs)|*.cs", DefaultExtension=".cs")]
public override string OutputFile
{
    
get {return base.OutputFile;}
    
set {base.OutputFile = value;}
}

</script>
<%=OutputFile%>
先谢谢了,再多问一句那个OutputFileCodeTemplate在哪里可以找到呀?我在我的CodeSmith目录下怎么找不到呢?
posted on 2007-05-15 14:30 过江 阅读(429) 评论(9)  编辑 收藏 所属分类: CodeSmith使用实例

  回复  引用  查看    
2007-05-15 15:53 | 学习.NET      
<script runat="template">
private string _outputDirectory = String.Empty;
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
[Category("General")]
[Description("The directory to output the results to.")]
[DefaultValue("")]
public string OutputDirectory
{
 get
 {
  if (_outputDirectory.Length == 0) return this.CodeTemplateInfo.DirectoryName + "output";
  return _outputDirectory;
 }
 set
 {
  if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);
  _outputDirectory = value;
 }
}
</script>

<script runat="template">
#region Ready Template
private const string entityTemplatename = "..\\DataAccess\\Dao.cst";// EntityByTable
private System.Collections.Hashtable _CodeTemplates = new System.Collections.Hashtable();
private string[] _templatesFileNames = new string[] {
              entityTemplatename //,
              //"MyEntityByView.cst" ,
              //"CollectionBase.cst" ,
              //"..\\DataAccess\\DataServices.cst" ,
              //"..\\BizObject\\BizObject.cst"
              };
              
public CodeTemplate CompileTemplate(string templateName)
{
 CodeTemplateCompiler compiler = new CodeTemplateCompiler(templateName);
 compiler.Compile();
 
 if (compiler.Errors.Count == 0)
 {
  return compiler.CreateInstance();
 }
 else
 {
  for (int i = 0; i < compiler.Errors.Count; i++)
  {
   Response.WriteLine(compiler.Errors[i].ToString());
  }
  return null;
 }
}
public void LoadTemplates()

 foreach(string _templatesFileName in _templatesFileNames)
 {
  string key = System.IO.Path.GetFileName(_templatesFileName);
  
  _CodeTemplates.Add(key, this.CompileTemplate(this.CodeTemplateInfo.DirectoryName + _templatesFileName));
 
  // Set the properties that all the commonsqlcode inherited templates should set
  try
  {
   //((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("EntityFormat", EntityFormat);
   //((CodeSmith.Engine.CodeTemplate)_CodeTemplates[key]).SetProperty("CollectionFormat", CollectionFormat);
  }
  catch(Exception) {}
 }
}
public CodeTemplate GetTemplate(string templateType)
{
 return (CodeSmith.Engine.CodeTemplate)_CodeTemplates[templateType];
}
public void Go()
{
 #region Create all directory and copy common files
 SafeCreateDirectory(OutputDirectory + "\\Model");
 SafeCreateDirectory(OutputDirectory + "\\DataAccess");
 SafeCreateDirectory(OutputDirectory + "\\Biz");
 #endregion
 
 #region LoadTemplates
 LoadTemplates();
 #endregion
 
 #region Go
 for (int i=0; i < SourceDatabase.Tables.Count; i++)
 {
  //Response.WriteLine(SourceDatabase.Tables[i].Name);
 }
 
 for (int i=0; i < SourceDatabase.Tables.Count; i++)
 {
  if(SourceDatabase.Tables[i].Name.StartsWith("pbcat")) continue;
  CodeSmith.Engine.CodeTemplate entity = this.GetTemplate(entityTemplatename);
  if (entity != null)
  {
   //this.GetTemplate(entityTemplatename).SetProperty("SourceDatabase", SourceDatabase);
   this.GetTemplate(entityTemplatename).SetProperty("SourceTable", SourceDatabase.Tables[i]);
   this.GetTemplate(entityTemplatename).SetProperty("NameSpace", NameSpace);
   this.GetTemplate(entityTemplatename).SetProperty("DevelopersName", DevelopersName); 
   
   this.GetTemplate(entityTemplatename).RenderToFile(OutputDirectory + "\\Model\\" + GetClassName(SourceDatabase.Tables[i].Name) + "Info.cs", true);
  //
  }
  if(IncludeCollection)
  {
   CodeSmith.Engine.CodeTemplate entities = this.GetTemplate("CollectionBase.cst");
   if(entities != null)
   {
    this.GetTemplate("CollectionBase.cst").SetProperty("ClassName", GetClassName(SourceDatabase.Tables[i].Name) + "Info");
    this.GetTemplate("CollectionBase.cst").SetProperty("CollectionClassName", GetClassName(SourceDatabase.Tables[i].Name) + "InfoCollection");
    this.GetTemplate("CollectionBase.cst").SetProperty("NameSpace", NameSpace);
    this.GetTemplate("CollectionBase.cst").SetProperty("DevelopersName", DevelopersName); 
    this.GetTemplate("CollectionBase.cst").RenderToFile(OutputDirectory + "\\Model\\" + GetClassName(SourceDatabase.Tables[i].Name) + "InfoCollection.cs", true);
   }
  //
  }
  //
  if(IncludeDataAccess)
  {
   CodeSmith.Engine.CodeTemplate dao = this.GetTemplate("Dao.cst");
   if(dao != null)
   {
    //Response.WriteLine("dao is not null!");
    this.GetTemplate("Dao.cst").SetProperty("SourceDatabase", SourceDatabase);
    this.GetTemplate("Dao.cst").SetProperty("SourceTable", SourceDatabase.Tables[i]);
    this.GetTemplate("Dao.cst").SetProperty("NameSpace", NameSpace);
    this.GetTemplate("Dao.cst").SetProperty("DevelopersName", DevelopersName); 
    
    this.GetTemplate("Dao.cst").RenderToFile(OutputDirectory + "\\DataAccess\\" + GetClassName(SourceDatabase.Tables[i].Name) + "Dao.cs", true);
   }
   else
   {
    Response.WriteLine("dao is null!");
   }
  }
  //
  if(IncludeBizObject)
  {
   CodeSmith.Engine.CodeTemplate biz = this.GetTemplate("BizObject.cst");
   if(biz != null)
   {
    this.GetTemplate("BizObject.cst").SetProperty("SourceDatabase", SourceDatabase);
    this.GetTemplate("BizObject.cst").SetProperty("SourceTable", SourceDatabase.Tables[i]);
    this.GetTemplate("BizObject.cst").SetProperty("NameSpace", NameSpace);
    this.GetTemplate("BizObject.cst").SetProperty("DevelopersName", DevelopersName); 
    this.GetTemplate("BizObject.cst").RenderToFile(OutputDirectory + "\\Biz\\" + GetClassName(SourceDatabase.Tables[i].Name) + ".cs", true);
   }
  }
 }
 
 Response.WriteLine("Success!");
 #endregion
}
</script>

  回复  引用    
2007-05-15 16:52 | 过江 [未注册用户]
@学习.NET ,先谢谢you。你留下的有些模板都没有呀,
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Inherits="OutputFileCodeTemplate" Description="Build custom access code." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Windows.Forms.Design"%>
<%@ Assembly Name="System.Design" %>
<script runat="template">
private string _outputDirectory = String.Empty;
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
[Category("General")]
[Description("The directory to output the results to.")]
[DefaultValue("")]
public string OutputDirectory
{
get
{
if (_outputDirectory.Length == 0) return this.CodeTemplateInfo.DirectoryName + "output";
return _outputDirectory;
}
set
{
if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);
_outputDirectory = value;
}
}
</script>

还是不行的呀!
  回复  引用  查看    
2007-05-15 16:53 | 幸运草      
可以参考shoucao.cnblogs.com
  回复  引用    
2007-05-16 14:27 | 过江 [未注册用户]
终于在shoucao.cnblogs.com的生成petShop3里找到了。
这样就可以了。
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Debug="True" Description="Template description here." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="SchemaExplorer" %>

<% if(!Directory.Exists(OutputDirectory)) Directory.CreateDirectory(OutputDirectory); %>

<script runat="template">

#region Output
private string _outputDirectory = String.Empty;

[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
[Category("General")]
[Description("The directory to output the results to.")]
[DefaultValue("")]
public string OutputDirectory
{
get
{
if (_outputDirectory.Length == 0)
{
return @"F:\FurturesSms";
}
else
{
return _outputDirectory;
}
}
set
{
if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);
_outputDirectory = value;
}
}
#endregion
</script>
  回复  引用    
2007-05-16 14:28 | 过江 [未注册用户]
再次谢谢学习.NET , 幸运草 !
  回复  引用    
2007-06-03 18:47 | 豆豆 [未注册用户]
<% if(!Directory.Exists(OutputDirectory)) Directory.CreateDirectory(OutputDirectory); %>
<script runat="template">

#region Output
private string _outputDirectory = String.Empty;

[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
[Category("General")]
[Description("The directory to output the results to.")]
[DefaultValue("")]
public string OutputDirectory
{
get
{
if (_outputDirectory.Length == 0)
{
return @"F:\FurturesSms";
}
else
{
return _outputDirectory;
}
}
set
{
if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);
_outputDirectory = value;
}
}
#endregion


这段代码变成:
<%@ Property Name = "OutputDirectory" Type="System.String" Category="General" Description="The directory to output the results to." %>
<% if(!Directory.Exists(OutputDirectory)) Directory.CreateDirectory(OutputDirectory); %>

简单明了
  回复  引用    
2007-06-03 19:06 | 豆豆 [未注册用户]
话又说回来
过江 楼主的问题是少了这个
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
  回复  引用    
2008-03-07 17:32 | Alpha cn [未注册用户]
还是" 豆豆 "的方法好.
以下是完整模块

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="OutputFileCodeTemplate" Debug="False" Description="C#代码生成模板" %>
<%@ Property Name="NameSpace" Type="String" Category="Context" Description="The namespace to use for this class"%>
<%@ Property Name="ClassName" Type="String" Optional="False" Category="Context" Description="The name of the class to generate" %>
<%@ Property Name="DevelopersName" Type="String" Optional="False" Category="Context" Description="The name to include in the comment header"%>
<%@ Property Name = "OutputDirectory" Type="String" Category="General" Description="The directory to output the results to." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>

<script runat="template">
</script>

///////////////////////////////////////////////////////////////////////////////////////
// File: <%=ClassName%>.cs
// Description: Enter summary here after generation.
// ---------------------
// Copyright ? <%= DateTime.Now.Year %> Our Client
// ---------------------
// History
// <%= DateTime.Now.ToShortDateString() %> <%= DevelopersName%> Original Version
///////////////////////////////////////////////////////////////////////////////////////
using System;

namespace <%=NameSpace%>
{
/// <summary>
/// Summary description for <%=ClassName %>.
/// </summary>
public class <%=ClassName%>
{
//
// TODO: Add constructor logic here
//
}
}
  回复  引用    
2008-03-07 17:37 | Alpha cn [未注册用户]
<script runat="template">
</script>

这段代码删除掉

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: