mardi 8 novembre 2016

Detecting collection type or polymorphism parameter using Reflection

For example, there I have these class

public class Executor{

    public void Execute(string name, DateTime start)
    public void Execute(ICollection<string> name, DateTime start)

}

... And another class that call this FindMethodAndParameter

public void FindMethodAndParameter(Type type)
{
    MethodInfo mInfo = type.GetMethods();
    foreach(MethodInfo mInfo in mInfos) 
    {
        Console.WriteLine( "Method: " + mInfo.Name );           
        ParameterInfo[] pInfos = mInfo.GetParameters();
        foreach( ParameterInfo parameterInfo in pInfos )
        {
            Console.WriteLine( "Parameter:" + parameterInfo.Position +  " - " + parameterInfo.ParameterType );
        }
    }
}

it returns the same for both methods:

Method: Execute
Parameter:0 - System.String
Parameter:1 - System.Date.Time
Method: Execute
Parameter:0 - System.String
Parameter:1 - System.Date.Time

how do I know if the second one is a list of names ?

I have read this, but I cannot figure out what to do either reading this. Detecting a collection-type property on a .NET object using reflection





Aucun commentaire:

Enregistrer un commentaire