我正在尝试编写一个T4模板,以遍历指定的项目文件夹并基于这些属性生成一个js文件。
我能够返回我的第一个类文件作为ProjectItem(返回为System.__ComObject)。
我看到名称正确返回(“MyReadModel.cs”)。
现在我正在努力从中获取属性。 该文件有一个System.__ComObject作为FileCodeModel。 我找不到任何属性。
我尝试了以下操作:
但是返回的是System.Reflection.PropertyInfo[0],我在哪里出错了?它似乎被强制转换为com对象...也许这是错误的吗?
编辑:
参考:
我能够返回我的第一个类文件作为ProjectItem(返回为System.__ComObject)。
我看到名称正确返回(“MyReadModel.cs”)。
Public Class MyReadModel{
Public string MyName { get; set; }
public int MyAge { get; set;}
}
现在我正在努力从中获取属性。 该文件有一个System.__ComObject作为FileCodeModel。 我找不到任何属性。
我尝试了以下操作:
projectItem.GetType().GetProperties()
但是返回的是System.Reflection.PropertyInfo[0],我在哪里出错了?它似乎被强制转换为com对象...也许这是错误的吗?
编辑:
参考:
http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration/
代码:
<# Prepare(this); #>
<# foreach(ProjectItem pi in FindProjectItemsIn(CurrentProject.ProjectItems.Item("Commands"))) { #>
<# WriteLine("found " + pi); #>
<# } #>
<#+
static DTE Dte;
static Dictionary<string, ResultTypeInfo> ResultTypes;
static TextTransformation TT;
static Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
static Project CurrentProject;
IList<ProjectItem> FindProjectItemsIn(ProjectItem start) {
var list = new List<ProjectItem>();
FindProjectItemsIn(start, list);
return list;
}
static bool IsFolder1(ProjectItem item) {
return (item.Kind == Constants.vsProjectItemKindPhysicalFolder);
}
void FindProjectItemsIn(ProjectItem start, IList<ProjectItem> list) {
foreach(ProjectItem item in start.ProjectItems) {
if(IsFolder1(item)) {
FindProjectItemsIn(item, list);
continue;
}
list.Add(item);
}
}
void Prepare(TextTransformation tt) {
TT = tt;
// Get the DTE service from the host
var serviceProvider = Host as IServiceProvider;
if (serviceProvider != null) {
Dte = serviceProvider.GetService(typeof(SDTE)) as DTE;
}
var projectItem = Dte.Solution.FindProjectItem(Host.TemplateFile);
CurrentProject = projectItem.ContainingProject;
}