2

Let's say Project is the directory containing all my classes and related files for my C# solution.

Now I want to get a list of all the properties, fields and methods names used in these classes. How can I do that? My first guess was to use regular expressions but then I though it may be very error prone. Then I saw this but I don't know if it suits my situation and I didn't know how to use it.

So is there a program that does this?

Community
  • 1
  • 1
Vahid
  • 5,144
  • 13
  • 70
  • 146

4 Answers4

2

Create a solution with all the files if you don't have one already, and iterate through types and members using reflection.

foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
    foreach (var prop in type.GetProperties())
    {
        //Iterating properties
    }

    foreach (var prop in type.GetFields())
    {
        //Iterating fields
    }

    foreach (var prop in type.GetMethods())
    {
        //Iterating methods
    }

    //Or iterate through all members

    foreach (var member in type.GetMembers())
    {
        //Iterating properties
    }
}
Philippe Paré
  • 4,279
  • 5
  • 36
  • 56
  • Do I need to create a separate solution for the code above? I don't know where to use this code. – Vahid Aug 13 '15 at 19:32
1

Reflection:

        foreach (Type thisType in Assembly.GetExecutingAssembly().GetTypes())
        {
            foreach(PropertyInfo thePropertyInfo in thisType.GetProperties())
            {
                //Do something with it
            }
            foreach(MethodInfo theMethodInfo in thisType.GetMethods())
            {
                //Do something with it
            }
        }

Make sure you add Using System.Reflection

Mr. B
  • 2,845
  • 1
  • 21
  • 31
  • I don't know where and how i should use this? how do i link this with my solution? – Vahid Aug 13 '15 at 19:36
  • This code would have to execute within your solution. What kind of solution is it Console, WinForms, or Web App? – Mr. B Aug 13 '15 at 19:41
  • It is written in WPF. – Vahid Aug 13 '15 at 19:42
  • Okay, so just slap this code down somewhere in a method that is guaranteed to be run. Then use a StreamWriter to write everything you need out of the WPF app, and then remove the code. – Mr. B Aug 13 '15 at 19:43
  • You mean I should put this on program startup, so that when the program runs it will create a list of all the assemblies. Did I get it right? – Vahid Aug 13 '15 at 19:46
  • Yeah that should work. Basically the bottom line is that it has to run in the Assembly it scans. It is possible to load an .exe or .dll from disk and do the same thing, but why bother. Just put it somewhere in your current code, if you just need it to run one time. Why are you trying to do this (like what is the requirement you are trying to solve)? – Mr. B Aug 13 '15 at 19:48
  • I'm trying to get a list of property names to write a simple obfuscator. – Vahid Aug 13 '15 at 19:50
1

You could as the others said Iterate the ExecutingAssembly. If you only want the name just use this call

static void Main()
{
    CProjectReflector reflector = new CProjectReflector();
    Console.WriteLine(reflector.ToString());
    Console.ReadLine();
}

You have to create two classes though. In the CProjectReflector class you have to add this to the top of your file

using System.Reflection;

If you want to know more what reflection is you could visit the MSDN

With reflection you can do lots more and its really worth reading. In short its about this

Reflection provides objects (of type Type) that describe assemblies, modules and types.

CProjectReflector.cs:

  class CProjectReflector
    {
        private List<CProjectObject> _projectObjects;

        public List<CProjectObject> ProjectObjects 
        {
            get { return _projectObjects; }
            set { _projectObjects = value; }
        }

        public CProjectReflector()
        {
            _projectObjects = new List<CProjectObject>();

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                CProjectObject obj = CProjectObject.CreateProjectObjectWithName(type.Name);
                FillProperties(type, obj);
                FillFields(type, obj);
                FillMethods(type, obj);
                FillMembers(type, obj);
                FillEvents(type, obj);
                _projectObjects.Add(obj);
            }
        }

        private static void FillEvents(Type type, CProjectObject obj)
        {
            foreach (EventInfo eventInfo in type.GetEvents())
            {
                obj.Events.Add(eventInfo.Name);
            }
        }

        private static void FillMembers(Type type, CProjectObject obj)
        {
            foreach (MemberInfo memberInfo in type.GetMembers())
            {
                obj.Members.Add(memberInfo.Name);
            }
        }

        private static void FillMethods(Type type, CProjectObject obj)
        {
            foreach (MethodInfo methodInfo in type.GetMethods())
            {
                obj.Methods.Add(methodInfo.Name);
            }
        }

        private static void FillFields(Type type, CProjectObject obj)
        {

            foreach (FieldInfo fieldInfo in type.GetFields())
            {
                obj.Fields.Add(fieldInfo.Name);
            }
        }

        private static void FillProperties(Type type, CProjectObject obj)
        {
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                obj.Properties.Add(propertyInfo.Name);
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            foreach (CProjectObject obj in _projectObjects) 
            {
                builder.AppendLine(obj.ToString());
            }
            return builder.ToString();
        }

    }

and CProjectObject.cs

  class CProjectObject
    {

        private String _name;

        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private List<String> _methods;

        public List<String> Methods
        {
            get { return _methods; }
            set { _methods = value; }
        }

        private List<String> _properties;

        public List<String> Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }

        private List<String> _fields;

        public List<String> Fields
        {
            get { return _fields; }
            set { _fields = value; }
        }

        private List<String> _members;

        public List<String> Members
        {
            get { return _members; }
            set { _members = value; }
        }

        private List<String> _events;

        public List<String> Events
        {
            get { return _events; }
            set { _events = value; }
        }

        public CProjectObject()
        {
            _name = "Anonym";
            _fields = new List<string>();
            _properties = new List<string>();
            _methods = new List<string>();
            _members = new List<string>();
            _events = new List<string>();
        }

        private CProjectObject(String name) : this()
        {
            _name = name;
        }

        public static CProjectObject CreateProjectObjectWithName(String name) 
        {
            return new CProjectObject(name);
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("ObjectName:" + _name);
            builder.AppendLine("-Contains the following fields");
            foreach (String str in Fields) 
            {
                builder.AppendLine("\tField:" + str);
            }
            builder.AppendLine("-Contains the following properties");
            foreach (String str in Properties)
            {
                builder.AppendLine("\tProperty:" + str);
            }
            builder.AppendLine("-Contains the following methods");
            foreach (String str in Methods)
            {
                builder.AppendLine("\tMethod:" + str);
            }
            builder.AppendLine("-Contains the following members");
            foreach (String str in Members)
            {
                builder.AppendLine("\tMember:" + str);
            }
            builder.AppendLine("-Contains the following events");
            foreach (String str in Events)
            {
                builder.AppendLine("\tEvent:" + str);
            }

            return builder.ToString();
        }
    }
Bongo
  • 2,933
  • 5
  • 36
  • 67
0

If it's just to have a graphical view you can use visual studio and expand your solution tree.

Otherwise if you want to do it programmaticaly is to use the Reflection, it's not very difficult to master.