How to define each type of parameter in a C # method?

I have a C # method:

MyMethod(int num, string name, Color color, MyComplexType complex)

Using reflection, how can I uniquely identify each of the parameter types of any method? I want to perform some task by parameter type. If the type is simple int, string or boolean, then I do something, if it is Color, XMLDocument, etc. I am doing something else, and if it is a custom type like MyComplexType or MyCalci etc., then I want to perform a specific task.

I can get all the method parameters using the ParameterInfo parameter and loop through each parameter and get their types. But how can I identify each data type?

foreach (var parameter in parameters)
{
    //identify primitive types??
    //identify value types
    //identify reference types

}

: , , . - / , , .

+5
2

ParameterInfo.ParameterType

 using System;
 using System.Reflection;

 class parminfo
 {
    public static void mymethod (
       int int1m, out string str2m, ref string str3m)
    {
       str2m = "in mymethod";
    }

    public static int Main(string[] args)
    {
       Console.WriteLine("\nReflection.Parameterinfo");

       //Get the ParameterInfo parameter of a function.

       //Get the type.
       Type Mytype = Type.GetType("parminfo");

       //Get and display the method.
       MethodBase Mymethodbase = Mytype.GetMethod("mymethod");
       Console.Write("\nMymethodbase = " + Mymethodbase);

       //Get the ParameterInfo array.
       ParameterInfo[]Myarray = Mymethodbase.GetParameters();

       //Get and display the ParameterInfo of each parameter.
       foreach (ParameterInfo Myparam in Myarray)
       {
          Console.Write ("\nFor parameter # " + Myparam.Position 
             + ", the ParameterType is - " + Myparam.ParameterType);
       }
       return 0;
    }
 }

System.Type, IsPrimitive IsByRef, . , IsValueType. System.Type Is *. MSDN Is *, ... IsClass state...

, , - ; .

, IsValueType . , true , IsClass true IsPassByRef true. , CLR, , , , . , CLR; .

+10

Type , ParameterType ParameterInfo. . -

if (parameter.ParameterType == typeof(int)) { 
  ...
}

, , ( , )

if (parameter.ParameterType.Name == "TheTypeName") {
  ...
}
+2

All Articles