mercredi 20 mai 2015

How to Get Parameters with Reflection in C# [on hold]

I was going through some videos to get a better handle on reflection and was wondering way I can't get the parameters of a method.

class Program
{
    static void Main(string[] args)
    {
        Customer Sam = new Customer(1, "Sam");

        Customer Samuel = new Customer();
        Samuel.Name = "Samuel";
        Samuel.ID = 2;

        Type T = Type.GetType("MyNamespace.Customer");
        Console.WriteLine("The fully qualified name: {0}", T.FullName);
        Console.WriteLine("Just the name of the class: {0}", T.Name);
        Console.WriteLine("Just the namespace: {0}", T.Namespace);

        PropertyInfo[] props = T.GetProperties();
        Console.WriteLine("*********Properties*********");
        foreach(PropertyInfo p in props)
        {
            Console.WriteLine(p.PropertyType.Name + " " + p.Name + "\n");
        }

        Console.WriteLine("********Methods********");
        MethodInfo[] meths = T.GetMethods();
        foreach (MethodInfo m in meths)
        {
            Console.WriteLine("The Methods are: {0}", m.Name + " and the return types are: {1}", m.ReturnType.Name);
        }
        Console.WriteLine("The reason you see more than the two I created is because it shows all the ones that inherit from the System.BaseClass and the auto implemented props");

        Console.WriteLine("**********Constructors********");
        ConstructorInfo[] cons = T.GetConstructors();
        foreach(ConstructorInfo c in cons)
        {
            Console.WriteLine("The constructors are: {0}", c);
        }

        Console.WriteLine("****Fields*****");
        FieldInfo[] fields = T.GetFields();
        foreach(FieldInfo f in fields)
        {
            Console.WriteLine("The fields are {0}", f);
        }

        Console.WriteLine("*****Parameters*****");
        ParameterInfo[] param = T.();//T.GetParameters() doesnt exist??

    }
}
public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }

    public Customer(int ID, string Name)
    {
        this.ID = ID;
        this.Name = Name;
    }
    public Customer()
    {
        this.ID = -1;
        this.Name = string.Empty;
    }
    public void PrintID()
    {
        Console.WriteLine("The ID is {0}", this.ID);
    }
    public void PrintName()
    {
        Console.WriteLine("The name is {0}", this.Name);
    }
}





Aucun commentaire:

Enregistrer un commentaire