none
help with reflection - get parameters for methods

    Question

  • Hello

    I am trying to use Reflection to get the parameters for all the methods in an assebly ( I can already get the method names).

    Here is the code that I am currently using.

    Code Snippet

    string TApath = "C:\\TA.dll";
                // Get the Type and MemberInfo.
                //Type MyType = Type.GetType("TAlib");
                //Type MyType = Assembly.LoadFrom(TApath).GetType();
                //Type MyType = Assembly.LoadFile(TApath).GetType();
                //Type MyType = Type.TicTacTec.TA.Library.Core;
                Type MyType = typeof(TicTacTec.TA.Library.Core);
                MemberInfo[] Mymemberinfoarray = MyType.GetMethods();

                // Get the MemberType method and display the elements.
                Console.Write("\nThere are {0} members in ", Mymemberinfoarray.GetLength(0));
                Console.Write(MyType.FullName);

                var qMethods = (from asm in MyType.GetMethods()
                                where asm.Name == "Adxr"
                               select asm).Take(200);

                foreach(var m in qMethods)
                    {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Name = " + m.Name);

                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("GetMethodImplementationFlags = " + m.GetParameters());
                   
                    //MethodInfo invoke = MyType.GetMethod(m.Name);
                    //ParameterInfo[] pars = invoke.GetParameters();
                    //foreach (ParameterInfo p in pars)
                    //    {
                    //    Console.WriteLine(p.ParameterType);
                    //    }
                    //Console.WriteLine("Attributes = " + m.Attributes);
                    //Console.WriteLine("ReflectedType = " + m.ReflectedType);
                    //Console.WriteLine("CallingConvention = " + m.CallingConvention);
                    //Console.WriteLine("ReflectedType.GetProperties = " + m.ReflectedType.GetProperties(Type.ReflectionOnlyGetType(m.Name,false,true)));
                    //Console.WriteLine("ReflectedType.GetProperties = " + m.ReflectedType.GetProperties(Type.GetType(m.Name, false)));
                    //foreach (var mm in m.ReflectedType.GetProperty(m.Name))
                    //    {
                    //    Console.WriteLine(mm);

                    //    }
                    //Console.WriteLine("DeclaringType = " + m.DeclaringType);
                   
                    }


    Thanks for any help you can provide
    Sunday, April 20, 2008 2:32 PM

Answers

  •  

    Hope this helps

     

    Code Snippet

    using System; 
    using System.Reflection; 
     
    class MyClass 
      public MyClass(int i) { 
        Console.WriteLine("Constructing MyClass(int). ")
      
     
      public MyClass(int i, int j) { 
        Console.WriteLine("Constructing MyClass(int, int). ")
      
     
      public int sum() { 
        return 0
      
     
      public bool isBetween(int i) { 
        return false
      
     
      public void set(int a, int b) { 
        Console.Write("Inside set(int, int). ")
      
     
      public void set(double a, double b) { 
        Console.Write("Inside set(double, double). ")
      
     
      public void show() { 
        Console.WriteLine("Values")
      

     
    class MainClass 
      public static void Main() { 
        Type t = typeof(MyClass)
     
        // Get constructor info. 
        ConstructorInfo[] ci = t.GetConstructors()
     
        Console.WriteLine("Available constructors: ")
        foreach(ConstructorInfo c in ci) { 
          // Display return type and name. 
          Console.Write("   " + t.Name + "(")
     
          // Display parameters. 
          ParameterInfo[] pi = c.GetParameters()
     
          for(int i=0; i < pi.Length; i++) { 
            Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name)
            if(i+< pi.Length
               Console.Write(", ")
          
     
          Console.WriteLine(")")
        
        Console.WriteLine()
      
    }

     

     

    Sunday, April 20, 2008 10:16 PM

All replies

  • What do you mean parameters? You can get property info and method info. Do you want the constructor info?

     

    You can also use generics (if it supports generics) for obtaining type parameters for a generic class or method as in this example:

     

     

    Code Snippet

    using System;

    using System.Collections.Generic;

     

    Stack s = new Stack();

    Type t = s.GetType();

     

    foreach (Type types in t.GetGenericArguments())

    {

         System.Console.WriteLine("Type parameter: " + types.FullName);

    }

     

     

    Another example:

     

    Code Snippet

    using System;

     

    public class Program

    {

        static void Main()

        {

            Type type;

            type = typeof(System.Nullable<>);

            Console.WriteLine(type.ContainsGenericParameters);

            Console.WriteLine(type.IsGenericType);

     

            type = typeof(System.Nullable<DataTime>);

            Console.WriteLine(!type.ContainsGenericParameters);

            Console.WriteLine(type.IsGenericType);

        }

    }

     

     

     

    Sunday, April 20, 2008 2:49 PM
  • Hi

    Sorry for not properly explaining myself. Yes I would like to get the Consructor info. And the does not support generics.

    Thanks for your help.
    Sunday, April 20, 2008 5:21 PM
  •  

    Hope this helps

     

    Code Snippet

    using System; 
    using System.Reflection; 
     
    class MyClass 
      public MyClass(int i) { 
        Console.WriteLine("Constructing MyClass(int). ")
      
     
      public MyClass(int i, int j) { 
        Console.WriteLine("Constructing MyClass(int, int). ")
      
     
      public int sum() { 
        return 0
      
     
      public bool isBetween(int i) { 
        return false
      
     
      public void set(int a, int b) { 
        Console.Write("Inside set(int, int). ")
      
     
      public void set(double a, double b) { 
        Console.Write("Inside set(double, double). ")
      
     
      public void show() { 
        Console.WriteLine("Values")
      

     
    class MainClass 
      public static void Main() { 
        Type t = typeof(MyClass)
     
        // Get constructor info. 
        ConstructorInfo[] ci = t.GetConstructors()
     
        Console.WriteLine("Available constructors: ")
        foreach(ConstructorInfo c in ci) { 
          // Display return type and name. 
          Console.Write("   " + t.Name + "(")
     
          // Display parameters. 
          ParameterInfo[] pi = c.GetParameters()
     
          for(int i=0; i < pi.Length; i++) { 
            Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name)
            if(i+< pi.Length
               Console.Write(", ")
          
     
          Console.WriteLine(")")
        
        Console.WriteLine()
      
    }

     

     

    Sunday, April 20, 2008 10:16 PM