none
Would like to access all methods and Class from assembly? RRS feed

  • Question

  • Hi ,

    Is there way to access all class and method from application.

    I have assmebly which is my exe, i want to use this exe and access all class inside and methods?

    Thanks

    A-


    Ashok

    Friday, September 28, 2012 1:59 PM

Answers

  • Does this work?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    
    namespace ListTypesInAssembly
    {
        interface FirstInterface { }
        interface SecondInterface { }
        class FirstClass { }
        class SecondClass { }
    
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("All types:");
    
                List<Type> allTypes = Assembly.GetExecutingAssembly().GetTypes().ToList();
                allTypes.ForEach(typeRef => Console.WriteLine(typeRef.Name));
    
                Console.WriteLine();
                Console.WriteLine("All classes:");
    
                IEnumerable<Type> allClassesEnumerable = from typeItem in allTypes
                                                   where typeItem.IsClass
                                                   select typeItem;
                List<Type> allClasses = allClassesEnumerable.ToList();
                allClasses.ForEach(classRef => Console.WriteLine(classRef.Name));
    
                allClasses.ForEach(classRef => 
                        {
                            Console.WriteLine();
                            Console.WriteLine("Methods in class {0}:", classRef.Name);
                            List<MethodInfo> methods = classRef.GetMethods().ToList();
                            methods.ForEach (methodRef => Console.WriteLine (methodRef.Name));
                        });
    
                Console.ReadKey();
            }
        }
    }

    • Marked as answer by Jumpingboy Saturday, September 29, 2012 1:25 PM
    Friday, September 28, 2012 9:33 PM

All replies

  • An exe file has a command line which are the arguements to main static void Main(string[] args).  So you can has a different dash option for each class.

    MyProgram.exe -class1 arg1 arg2 arg3

    Remmeber that a command line is a string so you have to parse the command line strings and convert them to the proper format for each class.  so you will need in your main() function a command line intepreter that reads the command line string.  Your code will have something like this

                string command = args[1];
                switch (command)
                {
                    case  "class1" :
                        class1(arg1,arg2,arg3);
                        break;
                    case  "class2" :
                        class2(arg1,arg2,arg3);
                        break;
                    case  "class3" :
                        class3(arg1,arg2,arg3);
                        break;
                }


    jdweng


    Friday, September 28, 2012 5:08 PM
  • Does this work?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    
    namespace ListTypesInAssembly
    {
        interface FirstInterface { }
        interface SecondInterface { }
        class FirstClass { }
        class SecondClass { }
    
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("All types:");
    
                List<Type> allTypes = Assembly.GetExecutingAssembly().GetTypes().ToList();
                allTypes.ForEach(typeRef => Console.WriteLine(typeRef.Name));
    
                Console.WriteLine();
                Console.WriteLine("All classes:");
    
                IEnumerable<Type> allClassesEnumerable = from typeItem in allTypes
                                                   where typeItem.IsClass
                                                   select typeItem;
                List<Type> allClasses = allClassesEnumerable.ToList();
                allClasses.ForEach(classRef => Console.WriteLine(classRef.Name));
    
                allClasses.ForEach(classRef => 
                        {
                            Console.WriteLine();
                            Console.WriteLine("Methods in class {0}:", classRef.Name);
                            List<MethodInfo> methods = classRef.GetMethods().ToList();
                            methods.ForEach (methodRef => Console.WriteLine (methodRef.Name));
                        });
    
                Console.ReadKey();
            }
        }
    }

    • Marked as answer by Jumpingboy Saturday, September 29, 2012 1:25 PM
    Friday, September 28, 2012 9:33 PM
  • I guess access can mean a few different things.  I code mean expose the name or it could mean execute the method.  Rajesh code reads the names of each class (not sure which order they will appear) and displays the name but does it make any sense to e execute the code like this?  Now what you could do in a .EXE file is to create a command line console which prompts the user to select a class which class the user want to execute using code like Rajest.

              


    jdweng

    Saturday, September 29, 2012 12:33 AM
  • Hi Joel,

    I respect your statement of "access can mean a few different things". But, equally, I don't want to respond back on any direct harsh comments like yours.

    > "does it make any sense to execute the code like this?".

    Part-1 - My opinion

    I respect the problem of the person who has originally posted this question. He/she may face any kind of problem and may have posted just the gist of that problem. We cannot judge on whether some code makes sense without knowing full context, for example the following information are not available in the original question: (1) it is a command line application or not? OR (2) the author wants to know just the name of the classes and methods or wants to execute them also?

    Even if wants to execute, the idea of Reflection is good, isn't it? First identifying the name of the classes and methods and then execute. At least, it is a better maintainable solution if not the best.

    Part-2 - Whether my code is really useful or not in real scenario?

    Once we were "automatically generating Serializers" for the classes inside an assembly. The launch sequence of the program picks all the classes decorated with "[Serializable]" attributes and all the properties that are decorated with "[SerializableMember]" in those classes to automatically generate the Serializer for them. Reflection is the easiest and best way for such requirements. Here, the order of the identification of classes does not matter but just the generation of serializer.

    Part-3 - Conclusion

    What if the original questioner also has some similar requirements but has posted only the gist of it here in the forum? We can't deny such real requirements. My real intention is to help the community. I really don't intend to divert other's attention from the problems of the original questioner with some code that does not make sense.

    Best Regards

    Rajesh Kannan

    Saturday, September 29, 2012 7:16 AM
  • Rajesh : I didn't mean so imply you code was not good.  My point was only to say that you can't just randomly execute code in the order that the serializer finds the modules.

    They say : "Art is in the eyes of the beholder".  Programming is definely an art.  What one person thinks is beautiful another person will think is ugly.  But art doesn't always have a functionality.  With programming you can write a beautiful piece of code, but if it doesn't have of way of being used is it any good?

    A seriaizlized code is great when you want to display the results so the uses can see everything in the object.  But you need a piece of AI (artificial inteligent) to be able to execute the results of the serialization.  An input command line is an alternative to using AI where the programmer has the intelligents.

    Saturday, September 29, 2012 9:59 AM
  • Thanks Joel and Rajesh,

    I think this code ll work for me, i just wanted to access Class and Methods, inside Assembly,

    whether for exe or .dll i have that part of code.

    i am doing some development task for that i need such kind of code and i have not gone order level thing, but for now above approach ll work for me ..

    Joel by ur example, do i need to give class name even i dont know abt class inside Assembly? how that ll work?


    Thanks

    a-


    Ashok


    • Edited by Jumpingboy Saturday, September 29, 2012 1:26 PM
    Saturday, September 29, 2012 1:25 PM