locked
Get Class Name from uploaded file RRS feed

  • Question

  • Good day to every one. Im creating an application which will upload a file using an openfiledialog box. Then, i would like to get the class name of the uploaded file.Is it possible??Ive tried on System.Reflection but i can't seem to find anything. After i get the class i would use it to get the methodinfo of the uploaded page. Thanks!!
    If not now,maybe tomorrow..or else...
    Wednesday, February 24, 2010 2:54 AM

Answers

  • Hi,

    You can retrieve user define class as well as class which is derived from another class with the help of reflection.

    See how to achive with the help of code.I am assuming the file is either dll or exe.

    1. How to find list of user defined classes in specific Assembly?

         Assembly assembly = Assembly.LoadFrom(“assemblyName.dll/exe");
         private void GetAllUserDefinedClass(Assembly assembly)
            {
                //How to Retrive all user defined classes
                Type[] oType = assembly.GetExportedTypes();
                foreach (Type type in oType)
                {
                    MessageBox.Show(type.Name + " -- " + type.BaseType);
              }
            }

    2. How to find list of Class that has derived from a particular Class?

    private void GetClasssDerivedFromSpcificClass(Assembly assembly, String className)
            {
                Type[] oType = assembly.GetExportedTypes();
                foreach (Type type in oType)
                {
                    if (type.BaseType.Name.Equals(className)) 
            {  MessageBox.Show(type.Name + " -- " + type.BaseType);    }
                }
            }

    Do let me know if you need more details.


    Cheers, Sanjiv Kumar
    Wednesday, February 24, 2010 7:24 AM
  • Hi,

    I think you will not be able to use the reflection to get the class name written in the uploaded .cs file until you compile it to dll or exe file.

    Consider using CodeDomProvider.CompileAssemblyFromFile Method to compile the .cs file and use the reflection to load the output dll or exe file to get the class name. See this sample:

    http://support.microsoft.com/kb/304655

    Thanks
    Binze

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, March 1, 2010 2:28 AM
    Friday, February 26, 2010 8:08 AM

All replies

  • Hi,

    You can retrieve user define class as well as class which is derived from another class with the help of reflection.

    See how to achive with the help of code.I am assuming the file is either dll or exe.

    1. How to find list of user defined classes in specific Assembly?

         Assembly assembly = Assembly.LoadFrom(“assemblyName.dll/exe");
         private void GetAllUserDefinedClass(Assembly assembly)
            {
                //How to Retrive all user defined classes
                Type[] oType = assembly.GetExportedTypes();
                foreach (Type type in oType)
                {
                    MessageBox.Show(type.Name + " -- " + type.BaseType);
              }
            }

    2. How to find list of Class that has derived from a particular Class?

    private void GetClasssDerivedFromSpcificClass(Assembly assembly, String className)
            {
                Type[] oType = assembly.GetExportedTypes();
                foreach (Type type in oType)
                {
                    if (type.BaseType.Name.Equals(className)) 
            {  MessageBox.Show(type.Name + " -- " + type.BaseType);    }
                }
            }

    Do let me know if you need more details.


    Cheers, Sanjiv Kumar
    Wednesday, February 24, 2010 7:24 AM
  • Thanks for the reply. But can i get the class name if i were to upload a .cs file???and not the dll or exe file?
    If not now,maybe tomorrow..or else...
    Friday, February 26, 2010 12:51 AM
  • Hi,

    I think you will not be able to use the reflection to get the class name written in the uploaded .cs file until you compile it to dll or exe file.

    Consider using CodeDomProvider.CompileAssemblyFromFile Method to compile the .cs file and use the reflection to load the output dll or exe file to get the class name. See this sample:

    http://support.microsoft.com/kb/304655

    Thanks
    Binze

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, March 1, 2010 2:28 AM
    Friday, February 26, 2010 8:08 AM
  • Thanks for the reply. Now i know that it is not possible without any .exe or DLL file.
    If not now,maybe tomorrow..or else...
    Monday, March 1, 2010 3:18 AM
  • Hi,
     Can you help to extend the program to find the all user define function/methods with their parameter type (and number) and return type please. I can findout all the class file from dll, I need each of the function from that dll.
    Muqim
    • Edited by muqim Wednesday, March 10, 2010 9:21 PM adding
    Wednesday, March 10, 2010 9:19 PM