-1

I am designing a .NET tool that is using reflection to report information on methods inside our assemblies.

Its completely object oriented and made up of Assembly Objects, Class Objects and Method objects all with my defined information I am retrieving from reflection. An Assembly contains many Classes and a class contains many Methods.

My problem is coming up with a structure and naming convention is is easy to read and use.

My current (rough example) object structure (in .NET) is just:

var assemblyList = new List<Assembly>();

public class Assembly
{
    public string Name;
    public List<Class> Classes;
}

public class Class {
    public string Name;
    public List<Method> Methods;
}

public class Method {
    public string Name;
}

This would work fine, except that I have additional properties that I want to have alongside each of these lists as im collecting information. That and I have inner classes I want to have for these class definitions for example inside the Method class I may want to have a class I created called Types.

Would the below be appropriate (bear with me as its a rough representation, not the actual objects) or am I making too much of a mess of things. I can't think of a correct solution, but would expect there to be a simpler way of doing it.

public class Class {
        public class Context {
            public string Name;
            public List<Method.Context> Methods;
        }

        public class List {
            public List<Context> List;
            public string propertyA;
            public int propertyB;
        }

        public class Tags {
            public const string MethodTypeA = "Ex1";
            public const string MethodTypeB = "Ex2";
        }
    }

    public class Method
        public class Context {
            public string Name;
        }

        public class List {
            public List<Context> List;
            public string propertyA;
            public int propertyB;
        }

        public class Tags {
            public const string ClassTypeA = "Ex1";
            public const string ClassTypeB = "Ex2";
        }
    }

Any help would be appreciated! My naming/structure design skills are quite poor.

  • 1
    Doesn't System.Reflection.Assembly already contain all this information? – Dan1701 May 23 '17 at 5:55
  • 4
    "theres a lot of filtering, logic and manipulation that is going to be built into my own objects and methods" It is unclear what you are trying to do. If it is indeed "to report information on methods inside our assemblies" you do not need anything over what System.Reflection offers. First define what you want to report and in what format, then design an interface. I do not see a need for state/data yet, you just want a command that collect what it needs and returns a report. Even if you want to play with filters, there is nothing to persist, everything is already persisted in assemblies. – Martin Maat May 23 '17 at 6:39
  • 2
    What are these classes intended to do? Are you trying to build some View Models for a GUI application? Have you considered the Layout, workflow and data which you want to include on your UI? Do you know what information you want to be displayed in your reports? Do you know how the Reflection data will be processed/filtered? Have you captured a clear set of requirements/user stories for the app? Do you have a target platform or UI framework for the app? – Ben Cottrell May 23 '17 at 7:10
  • 1
    (many reports, performance) So, you start by solving a problem that likely does not exist because 1.OS read caching and 2.You are asleep when it runs, sure to create a lot of noise and room for errors. Get your logic straight first, and then, if you feel you need to speed it up, put some buffer in front of it that STILL does not have to be a class hierarchy but can be just a simple proxy object. – Martin Maat May 24 '17 at 5:47
  • 2
    @Cyassin I am saying you do not know if this is an issue yet and that you do not need your own class tree to trim down a couple of collections to just the items you are interested in. If the filtering would appear to be expensive you could try to speed it up or buffer some results but it seems to me you are not there yet and you are waisting your time on stuff that is somehow more appealing. – Martin Maat May 24 '17 at 6:47
1

I assume You are going to have some meta information in your context. Right?

So potentially you might end up with a "God-Context" class, if You will, try to put different concepts under the same umbrella. For instance, in the following example there are two concepts mixed: usage and checksums.

someClassInstance.Context.ReferencedBy
someClassInstance.Context.MD5
someClassInstance.Context.SHA

So instead of having one context its better to split. Something like that:

someClassInstance.Usage.ReferencedBy
someClassInstance.Checksum.MD5
someClassInstance.Checksum.SHA

In some scenarios it migt be an overhead to introduce a class per concept, so you can easily combine names and expose the concepts via properties

someClassInstance.ReferencedBy
someClassInstance.MD5Checksum
someClassInstance.SHAChecksum

In your particular case it would be

 public class Class
 {
        public string Name { get; set; }
        public List<Method> Methods { get; set; } = new List<Method>();
        public List<Tag> Tags = new List<Tag>();

        ...
        public string SomeContextSpecificProperty1 { get; set; }
        ...
        public string SomeContextSpecificPropertyN { get; set; }
  }

  public class Method
  {
        public string Name;
        public List<Tag> Tags = new List<Tag>();
  }

  public class Tag
  {
      public string Value { get; set; }
  }

If You want to introduce class per context, consider this

public class Assembly
    {
        public string Name { get; set; }
        public List<Class> Classes { get; set; } = new List<Class>();
        public List<Tag> Tags = new List<Tag>();
        public AssemblyContext Context { get; set; }
        public class AssemblyContext
        {
            public string SomeContextSpecificProperty1 { get; set; }
            //,,,
            public string SomeContextSpecificPropertyN { get; set; }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.