Solved

C# Reflection MethodInfo problem

Posted on 2004-10-27
1,427 Views
Last Modified: 2008-03-03
I am having trouble of getting the desired method outputs.    I need to get the method infomation from the Euclid class as example:

int x 0
 ...
 string HowTo()
 int gcd(int, int)

but I am only getting the output as below:
System.String HowTo()
Int32 gcd(Int32, Int32)
            :
            :
System.Type GetType()

below is the code I have.  Your help will be very much appreciated!

     Thanks!


************************************

public static void Main(string[] args)
        {
Regex r = new Regex("(,)"); // Split on hyphens.
            string[] s = r.Split(args[0]);
            string unknownClass = s[0];
            string unknownDll = s[2];

            string dllPath = unknownDll + ".dll";
         
            Assembly UnknownAssembly = Assembly.LoadFrom(dllPath);
            Type type = UnknownAssembly.GetType(unknownClass);
            //Object obj = Activator.CreateInstance(type);
           

           
            Console.WriteLine("\nFollowing is the member info for class:{0} ",
                                   unknownClass);
           // BindingFlags flags = (BindingFlags.Public | BindingFlags.Instance);
            int count = 0;
            foreach (MethodInfo methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {  
                // Print the Method Signature
     
                Console.WriteLine("{0}\t{1}", count, methodInfo);
                count++;
            }
           
            foreach (MemberInfo memberInfo in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                Console.WriteLine("{0}\t{1}",count, memberInfo);
                count++;
            }
}

namespace Edu.Rit.CS.ATS {
  /// <summary>
  ///   a relatively unknown test class.
  /// </summary>
  public class Euclid {
    public string HowTo () {
      return "to compute the greatest common divisor\n"
        +"\tset x and y and call gcd() or call gcd(x, y)";
    }
    public int x, y;
    public int gcd (int x, int y) {
      while (x != y) if (x > y) x -= y; else y -= x;
      return x;
    }
    public int gcd () { return gcd(x, y); }
    public int this [string name] {
      get {
        if (name == "x") return x;
        else if (name == "y") return y;
        else throw new IndexOutOfRangeException("only x or y");
      }
      set {
        if (name == "x") x = value;
        else if (name == "y") y = value;
        else throw new IndexOutOfRangeException("only x or y");
      }
    }
    public int X { get { return x; } set { x = value; } }
    public int Y { get { return y; } set { y = value; } }
  }
}

                             
0
Question by:charlesbaldo
    2 Comments
     
    LVL 7

    Expert Comment

    by:God_Ares
                     textBox3.Text = "";

                      Type t = typeof(Edu.Rit.CS.ATS.Euclid);
                      foreach(MethodInfo i in  t.GetMethods())
                      {
                            
                            textBox3.Text += i.Name.ToString()+"(";
                            string s="";
                            foreach(ParameterInfo p in i.GetParameters())
                            {
                                  s+= p.ParameterType.ToString()+" "+p.Name+",";
                            }
                            if (s.Length>0)      s=s.Substring(0,s.Length-1);
                            textBox3.Text += s+")"+"\r\n";                        
                      }
    0
     
    LVL 7

    Accepted Solution

    by:
                  Type t = typeof(Edu.Rit.CS.ATS.Euclid);

    can be done your way,...

    Assembly UnknownAssembly = Assembly.LoadFrom(dllPath);
                Type type = UnknownAssembly.GetType(unknownClass);

    als long if unknownClass == "Edu.Rit.CS.ATS.Euclid"
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now