Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate over a class for properties

I'm trying to write a T4 template to iterate over a project folder (specified) and generate a js file based on those properties.

I'm able to return my first class file as a ProjectItem (returns as a System.__ComObject)

i see name is returning correctly ("MyReadModel.cs")

Public Class MyReadModel{
  Public string MyName { get; set; }
  public int MyAge { get; set;}
}

now I'm struggling to return the properties out of it. the file has a FileCodeModel as System.__ComObject. i can't find any properties.

i tried doing the following:

projectItem.GetType().GetProperties()

but returns System.Reflection.PropertyInfo[0]

any tips on where I'm going wrong? it appears its being cast as a com object... maybe this is wrong?

EDIT:

references:

http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration/

How to get T4 in VS2010 to iterate over class' properties

Code:

<# 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;
}
like image 739
nologo Avatar asked Mar 15 '12 10:03

nologo


People also ask

Which loop is used to iterate the properties of an object?

Method 1: Using for…in loop: The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object.

How do you iterate through an object in Kotlin?

To iterate over elements of Set in Kotlin, we can use for loop, forEach() function, or while loop.


1 Answers

In order to get the list of properties in the classes/structs contained in a given source file you can do the following (you could easily get the class name etc. by using CodeClass):

private IList<CodeProperty> GetProperties(string csFile)
{
  ProjectItem projectItem = TransformationContext.FindProjectItem(csFile);
  FileCodeModel codeModel = projectItem.FileCodeModel;
  var propertyList = new List<CodeProperty>();
  FindProperties(codeModel.CodeElements, propertyList);
  return propertyList;
}

private void FindProperties(CodeElements elements, IList<CodeProperty> properties)
{
  foreach (CodeElement element in elements)
  {
    CodeProperty property = element as CodeProperty;
    if (property != null)
    {
      properties.Add(property);
    } 
    FindProperties(element.Children, properties);
  }
}
like image 117
Dave Maff Avatar answered Oct 04 '22 00:10

Dave Maff