Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. help

help

Scheduled Pinned Locked Moved C#
helpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nidheeshkayal
    wrote on last edited by
    #1

    hi my problem is there is one list box in my program and a dialog. when i am selecting one class name from the dialog then all methods inside the class should display in to the list box. how can i do this without reflection nidh

    J C 2 Replies Last reply
    0
    • N nidheeshkayal

      hi my problem is there is one list box in my program and a dialog. when i am selecting one class name from the dialog then all methods inside the class should display in to the list box. how can i do this without reflection nidh

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      You posted the exact same question 15 minutes ago, with an equally bad subject title. Please read How to get answers to your questions[^].

      1 Reply Last reply
      0
      • N nidheeshkayal

        hi my problem is there is one list box in my program and a dialog. when i am selecting one class name from the dialog then all methods inside the class should display in to the list box. how can i do this without reflection nidh

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        nidheeshkayal wrote:

        how can i do this without reflection

        Why do you want to do it without reflection? (I can't think of any solution that doesn't use reflection - So I would help otherwise) ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

        C 1 Reply Last reply
        0
        • C Colin Angus Mackay

          nidheeshkayal wrote:

          how can i do this without reflection

          Why do you want to do it without reflection? (I can't think of any solution that doesn't use reflection - So I would help otherwise) ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

          C Offline
          C Offline
          chakkara2003
          wrote on last edited by
          #4

          can u write some code chakkara

          C 1 Reply Last reply
          0
          • C chakkara2003

            can u write some code chakkara

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            chakkara2003 wrote:

            can u write some code

            Of course I can. An example of pulling out a list of methods from a specific type:

            using System.Reflection;

            System.Type myType = typeof(MyClass);
            MethodInfo[] theMethods = myType.GetMethods();
            foreach(MethodInfo method in theMethods)
            {
            Console.WriteLine("{0}", method.Name);
            }

            Links to MSDN: * System.Type class[^] * Type.GetMethods()[^] * System.Reflection.MethodInfo class[^] Does this help? ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

            C 1 Reply Last reply
            0
            • C Colin Angus Mackay

              chakkara2003 wrote:

              can u write some code

              Of course I can. An example of pulling out a list of methods from a specific type:

              using System.Reflection;

              System.Type myType = typeof(MyClass);
              MethodInfo[] theMethods = myType.GetMethods();
              foreach(MethodInfo method in theMethods)
              {
              Console.WriteLine("{0}", method.Name);
              }

              Links to MSDN: * System.Type class[^] * Type.GetMethods()[^] * System.Reflection.MethodInfo class[^] Does this help? ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

              C Offline
              C Offline
              chakkara2003
              wrote on last edited by
              #6

              sir, here am attaching a piece of code, and it shows an error, plz correct it private void Browse_Click(object sender, EventArgs e) { openFileDialog1.FileName = ""; openFileDialog1.CheckFileExists = true; openFileDialog1.CheckPathExists = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { LoadMethods(openFileDialog1.FileName); } } public void LoadMethods(string Path) { int len = Path.Split('\\').Length; string Name = Path.Split('\\')[len - 1].ToString(); string ClsName = Name.Split('.')[0].ToString(); //here ClsName is the class name, i need to get objects under that class // System.Type myType = typeof(MyClass); System.Type myType = typeof(ClsName); MethodInfo[] theMethods = myType.GetMethods(); foreach (MethodInfo method in theMethods) { //Console.WriteLine("{0}", method.Name); LstMethods.Items.Add(method.Name); } } chakkara

              J 1 Reply Last reply
              0
              • C chakkara2003

                sir, here am attaching a piece of code, and it shows an error, plz correct it private void Browse_Click(object sender, EventArgs e) { openFileDialog1.FileName = ""; openFileDialog1.CheckFileExists = true; openFileDialog1.CheckPathExists = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { LoadMethods(openFileDialog1.FileName); } } public void LoadMethods(string Path) { int len = Path.Split('\\').Length; string Name = Path.Split('\\')[len - 1].ToString(); string ClsName = Name.Split('.')[0].ToString(); //here ClsName is the class name, i need to get objects under that class // System.Type myType = typeof(MyClass); System.Type myType = typeof(ClsName); MethodInfo[] theMethods = myType.GetMethods(); foreach (MethodInfo method in theMethods) { //Console.WriteLine("{0}", method.Name); LstMethods.Items.Add(method.Name); } } chakkara

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                When you browse, I assume you are browsing for a .NET assembly (dll or exe). In which case your variable ClsName is in fact the assembly name, not the name of one particular class (BTW, Look at System.IO.Path for extracting just the file name, it saves alot of boilerplate code) In this case, what you need to use is Assembly.LoadFrom[^] to load the .dll/.exe as an object of type Assembly[^]. From there you can use Assembly.GetType[^] to get an instance of a particular type, then the code to itterate the methods is exactly as Colin suggested. Here, I rewrote your LoadMethods method to demonstrate. You will need to add using System.Reflection if its not in your code already

                private void LoadMethods (string fileName)
                {
                
                	Assembly asm = Assembly.LoadFrom(fileName);
                
                	System.Text.StringBuilder sb = new System.Text.StringBuilder();
                	foreach(Type t in asm.GetTypes())
                	{
                		sb.AppendFormat("Type:{0} Methods:",t.Name);
                
                		foreach(MethodInfo mi in t.GetMethods())
                		{
                			sb.AppendFormat("{0} ",mi.Name);
                		}
                		sb.Append("\r\n");
                	}
                	MessageBox.Show(sb.ToString());
                }
                

                -- modified at 8:05 Thursday 2nd March, 2006

                N 1 Reply Last reply
                0
                • J J4amieC

                  When you browse, I assume you are browsing for a .NET assembly (dll or exe). In which case your variable ClsName is in fact the assembly name, not the name of one particular class (BTW, Look at System.IO.Path for extracting just the file name, it saves alot of boilerplate code) In this case, what you need to use is Assembly.LoadFrom[^] to load the .dll/.exe as an object of type Assembly[^]. From there you can use Assembly.GetType[^] to get an instance of a particular type, then the code to itterate the methods is exactly as Colin suggested. Here, I rewrote your LoadMethods method to demonstrate. You will need to add using System.Reflection if its not in your code already

                  private void LoadMethods (string fileName)
                  {
                  
                  	Assembly asm = Assembly.LoadFrom(fileName);
                  
                  	System.Text.StringBuilder sb = new System.Text.StringBuilder();
                  	foreach(Type t in asm.GetTypes())
                  	{
                  		sb.AppendFormat("Type:{0} Methods:",t.Name);
                  
                  		foreach(MethodInfo mi in t.GetMethods())
                  		{
                  			sb.AppendFormat("{0} ",mi.Name);
                  		}
                  		sb.Append("\r\n");
                  	}
                  	MessageBox.Show(sb.ToString());
                  }
                  

                  -- modified at 8:05 Thursday 2nd March, 2006

                  N Offline
                  N Offline
                  nidheeshkayal
                  wrote on last edited by
                  #8

                  here we get the class class name and objects from the assembly. my new requirement is i need to get the methods directly from class that is -- i have class named Form1, by using a dialog contols i can get the class. assign that dialog.Filename in to a string now i got the class name- from this class i need to fetch objects once again thanx for u r help nidheesh

                  J 1 Reply Last reply
                  0
                  • N nidheeshkayal

                    here we get the class class name and objects from the assembly. my new requirement is i need to get the methods directly from class that is -- i have class named Form1, by using a dialog contols i can get the class. assign that dialog.Filename in to a string now i got the class name- from this class i need to fetch objects once again thanx for u r help nidheesh

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    Your post is not particularly clear, but check out Type.GetType[^], which allows you to retrieve a Type instance from a string representaion.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups