Search code examples
c#visual-studiovisual-studio-2017

Is there a way to view all members (or properties, at least) of a class including its base class in my editor?


I'm working on implementing a factory pattern on a bunch of (like 30) classes that inherit from the same abstract class (some have another abstract class in between).

I have about 50 different "Create" methods to write, and it's tiresome trying to keep track of which properties I've written a definition for in each of these Create methods.

Is there a tool or plugin for VS2017 that will output a simple list of all available properties (and their types would be a bonus, and something I can export to a spreadsheet is even better), including those declared in the derived class, and it's parent class, and any grandparent+ classes? That way I can "check off" the ones I've defined while writing each of these "Create" methods in my factory class?

Obviously this information is available, since intellisense and the compiler "knows" this same information.

Right clicking the class name and Inspect -> Hierarchies gets me somewhat close, but I can't seem to get a list of ALL members / properties in the tree, only the class / inheritance "tier" that is selected.

Example (except IRL these classes are in separate files so it's not easy to do manually):

public abstract class Foo
{
    public string Name { get; set; }
    public string Color { get; set; }
    public int Quantity { get; set; }
}

public class Bar : Foo
{
    public double Width { get; set; }
    public double Length { get; set; }
    public double Depth { get; set; }
}

I'd like to then be able to export a list that would look something like:

public string Name
public string Color
public int Quantity
public double Width
public double Length
public double Depth

That way I can check off each property as I implement them.

EDIT: I should mention that I do have a Resharper license, so an answer that utilizes it would be acceptable in my case.

EDIT2: Another thing I found which is VERY close is the Class View window. There is an option to show inherited members and gets me a flat list. If there were a way to export this, and to somehow show the data type for each property, I'd be all set.

Also I am working with code that I'm not ready to compile, and won't be for a little while (this is all brand new code). There have been some good suggestions for code that does compile but unfortunately I'm not ready to implement those.


Solution

  • You could achieve something like this easily with Reflection, columns separated by tab so that you can just paste into Excel.

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
    
            var assembly = typeof(Foo).Assembly;
    
            var types = assembly.ExportedTypes
                // Abrstract && Sealed = Static classes
                .Where(x => x.IsClass && !(x.IsAbstract && x.IsSealed));
    
            var builder = new StringBuilder("Class\tDeclared By\tMember type\tName\n");
    
            foreach (var type in types)
            {
                builder.AppendLine(GetTypeInformation(type));
            }
    
            // result is copied into the clipboard, just CTRL+V into Excel
            System.Windows.Forms.Clipboard.SetText(builder.ToString());
        }
    
        private static string GetTypeInformation(Type type)
        {
            var classInformationBuilder = new StringBuilder();
    
            foreach (var member in type.GetProperties())
            {
                classInformationBuilder.AppendLine($"{type.Name}\t{member.DeclaringType.Name}\t{member.PropertyType.Name}\t{member.Name}");
            }
    
            return classInformationBuilder.ToString();
        }
    }
    
    public abstract class Foo
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public int Quantity { get; set; }
    }
    
    public class Bar : Foo
    {
        public double Width { get; set; }
        public double Length { get; set; }
        public double Depth { get; set; }
    }
    

    Results in excel:

    enter image description here