Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:41:59+00:00 2026-05-28T14:41:59+00:00

is there a way I can read in a namespace and loop through all

  • 0

is there a way I can read in a namespace and loop through all the classes in a t4 template using reflection or something?

<#foreach (class poco in LoadNamespace("Web.Code.Entities.Poco").Classes ) { #>
public interface I<# poco.ClassName #>Repository 
{
    IQueryable< <# poco.ClassName #> > Get();
    <# poco.ClassName #> Save(<# poco.ClassName #> entity);
    bool Delete(<# poco.ClassName #> entity);
}
<#} #>

example meta data:

namespace Web.Code.Entities.Poco
{
    public class Product
    { 
          public int Id { get; set; }
          public string Name{ get; set; }
          public string Category{ get; set; }
    }

    public class Employee
    { 
          public int Id { get; set; }
          public string Name{ get; set; } 
    }
}

desired output:

    public interface IProductRepository 
    {
        IQueryable<Product> Get();
        Product Save(Product entity);
        bool Delete(Product entity);
    }   

    public interface IEmployeeRepository 
    {
        IQueryable<Employee> Get();
        Employee Save(Employee entity);
        bool Delete(Employee entity);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:42:00+00:00Added an answer on May 28, 2026 at 2:42 pm

    First install T4Toolbox from here

    Then In your Template ensure you have the following lines at top :

    <#@ assembly name="EnvDTE" #> 
    <#@ import namespace="EnvDTE" #>
    <#@ include file="T4Toolbox.tt" #>
    

    Then add these lines of codes:

    <#+  
    
    private List<CodeClass> FindClasses(string nameSpace, string className)
    {
        List<CodeClass> result=new List<CodeClass>();
        FindClasses(TransformationContext.Project.CodeModel.CodeElements,className,nameSpace,result,false);
        return result;
    
    }
    
    
    private void FindClasses(CodeElements elements, string className,string searchNamespace,List<CodeClass> result,bool isNamespaceOk)
    {
        if (elements==null)return;
        foreach (CodeElement element in elements)
        {       
            if(element is CodeNamespace)
            {
                CodeNamespace ns = element as CodeNamespace;
                if(ns != null)
                {
                    if (ns.FullName == searchNamespace)
                        FindClasses(ns.Members, className,searchNamespace,result,true);
                    else
                        FindClasses(ns.Members, className,searchNamespace,result,false);
                }
            }
            else if(element is CodeClass && isNamespaceOk)
            {
                CodeClass c = element as CodeClass;
                if (c != null)
                {
                    if(c.FullName.Contains(className))
                        result.Add(c);
    
                    FindClasses(c.Members, className,searchNamespace,result,true);
                }
    
            }
        }
    }
    #>
    

    Then you can find all classes in a namespace by calling like this (the second parameter filters all classes which their names contain the passed string) :

    FindClasses("NameSpace.SubNameSpace",""))//All classes in the specifed namespace 
    

    OR

    FindClasses("NameSpace.SubNameSpace","Repository")) //All classes in the specifed namespace which have "Repository" in their names
    

    —————————————— UPDATE FOR VS 2012 ————————–

    Use these functions and namespaces if your T4Toolbox is updated for VS 2012 :

    //visual studio 2012+
    <#@ assembly name="Microsoft.VisualStudio.Shell.11.0" #>
    <#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #>
    <#@ import namespace="Microsoft.VisualStudio.Shell" #>
    <#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
    
    private List<CodeClass> FindClasses(string nameSpace, string className,string baseClassName)
    {
        List<CodeClass> result=new List<CodeClass>();
        FindClasses(GetProject().CodeModel.CodeElements,className,baseClassName,nameSpace,result,false);
        return result;
    
    }
    
    
    private void FindClasses(CodeElements elements, string className,string baseClassName,string searchNamespace,List<CodeClass> result,bool isNamespaceOk)
    {
        if (elements==null)return;
        foreach (CodeElement element in elements)
        {       
            if(element is CodeNamespace)
            {
                CodeNamespace ns = element as CodeNamespace;
                if(ns != null)
                {
                    if (ns.FullName == searchNamespace)
                        FindClasses(ns.Members, className,baseClassName,searchNamespace,result,true);
                    else
                        FindClasses(ns.Members, className,baseClassName,searchNamespace,result,false);
                }
            }
            else if(element is CodeClass && isNamespaceOk)
            {
                CodeClass c = element as CodeClass;
                if (c != null)
                {
                    if(c.FullName.Contains(className) && (baseClassName==null || (HasIt(c.Bases ,baseClassName) && c.Name != baseClassName)))
                        result.Add(c);
    
                    FindClasses(c.Members, className,baseClassName,searchNamespace,result,true);
                }
    
            }
        }
    }
    
    private bool HasIt(CodeElements elements,string name)
    {
        foreach (CodeElement element in elements)
        {
            if (element.Name==name)
                return true;
        }
        return false;
    }
    
    private Project GetProject()
    {
        // Get DTE
        var dte = (DTE)TransformationContext.Current.GetService(typeof(DTE));
    
        // Get ProjectItem representing the template file
        ProjectItem projectItem = dte.Solution.FindProjectItem(TransformationContext.Current.Host.TemplateFile);
    
        // Get the Project of the template file
        Project project = projectItem.ContainingProject;
    
        return project;
    }
    
    private string GetDefaultNamespace()
    {
    
        // Get DTE
        var dte = (DTE)TransformationContext.Current.GetService(typeof(DTE));
    
        // Get ProjectItem representing the template file
        ProjectItem projectItem = dte.Solution.FindProjectItem(TransformationContext.Current.Host.TemplateFile);
    
        // Get the Project of the template file
        Project project = projectItem.ContainingProject;
    
        var vsSolution = (IVsSolution)TransformationContext.Current.GetService(typeof(SVsSolution));
        IVsHierarchy vsHierarchy;
        ErrorHandler.ThrowOnFailure(vsSolution.GetProjectOfUniqueName(project.FullName, out vsHierarchy));
        uint projectItemId;
        ErrorHandler.ThrowOnFailure(vsHierarchy.ParseCanonicalName(projectItem.FileNames[1], out projectItemId));
        object defaultNamespace;
        ErrorHandler.ThrowOnFailure(vsHierarchy.GetProperty(projectItemId, (int)VsHierarchyPropID.DefaultNamespace, out defaultNamespace));
        return ((string)defaultNamespace);
    }
    

    so your search will be something like this :

    FindClasses(GetDefaultNamespace(),"Repository","RepositoryBase")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some way I can define String[int] to avoid using String.CharAt(int) ?
Is there any way I can read the content of the framebuffer in Qt
Is there any way I can read the contents of PDF file in Visual
Is there any way can declare a bean in just like JSP UseBean in
is there way thats i can preselect an item when the page loads or
Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in
Is there a way I can control columns from code. I had a drop
Is there a way I can access (for printout) a list of sub +
Is there a way you can run your UnitTest project against a compiled DLL
Is there any way I can detect when my page has been set as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.