如何识别C#方法中的每个参数类型?

发布于 2024-10-10 05:26:02 字数 622 浏览 4 评论 0原文

我有一个 C# 方法说:

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

使用反射,如何清楚地识别任何方法的每个参数类型? 我想通过参数类型执行一些任务。如果类型是简单的 int、string 或 boolean,那么我会做一些事情,如果它是 Color、XMLDocument 等,我会做其他事情,如果它是用户定义的类型,如 MyComplexType 或 MyCalci 等,那么我想做某些任务。

我能够使用 ParameterInfo 检索方法的所有参数,并且可以循环每个参数并获取它们的类型。但如何识别每种数据类型呢?

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

}

编辑:这是我的代码的一部分,用于创建属性网格排序页面,我想在其中显示包含所选方法的数据类型的参数列表。如果参数具有任何用户定义的类型/引用类型,那么我想进一步扩展它以显示其下的所有元素的数据类型。

I have a C# method say:

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

Using reflection, how can I distinctly 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 do something else and if it is user defined type like MyComplexType or MyCalci etc then I want to do certain task.

I am able to retrieve all the parameters of a method using ParameterInfo and can 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

}

Edit: this is apart of my code to create a propert grid sort of page where I want to show the parameter list with data types for the selected method. If the parameter has any userdefined type/reference type then I want to expand it further to show all the elements under it with datatypes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御弟哥哥 2024-10-17 05:26:02

请使用 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,您可以使用IsPrimitiveIsByRef,正如David提到的。此外,您还可以使用IsValueTypeSystem.Type 类中有大量 Is* 属性。您最好的选择是检查每个 Is* 属性的 MSDN 文档,即...IsClass 状态...

获取一个值,指示是否
类型是一个类;也就是说,不是一个值
类型或接口。

因此可以推断不需要调用 IsValueType 。请记住,给定类型可以在多个属性中返回 true,因为 IsClass 可以返回 true,并且 IsPassByRef 可以返回 true。也许为已知的 CLR 类型提供逻辑,因为这些类型不会改变,并且您提前知道这些类型,然后构建用户定义的复杂类型的逻辑。您也可以采用构建逻辑的方法来为 CLR 类型执行此操作;无论哪种方式都会起作用。

Make use of 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;
    }
 }

If you need to check the System.Type once retrieved you can use IsPrimitive and IsByRef as mentioned by David. In addition you can also use IsValueType. There are a significant number of Is* properties within the System.Type class. Your best bet would be to check the MSDN documentation on each Is* property ie...IsClass states...

Gets a value indicating whether the
Type is a class; that is, not a value
type or interface.

Therefore one could deduce that IsValueType does not need to be called. Keep in mind that a given type can return true across multiple properties in that IsClass could return true AND IsPassByRef could return true. Perhaps provide logic for the known CLR types since those will not change and you know those ahead of time and then build in the logic for complex types as defined by the user. You could take the approach of building in the logic to do this for the CLR types as well; either way would work.

你如我软肋 2024-10-17 05:26:02

要获取参数的实际Type,请使用ParameterInfo 值上的ParameterType。通过该值,您可以通过多种方式使用它来识别类型。最简单的方法是直接与已知类型进行比较

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

,或者在类型不可用的情况下,可以使用名称匹配(尽管这有点不稳定,因为重构操作可能会错过字符串常量并默默地破坏应用程序)

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

To get the actual Type of the parameter use the ParameterType on the ParameterInfo value. With that value there are several ways you can use it to identify the type. The easiest is with a direct comparison to a known type

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

Or in cases where the type is not available a name match can be used (although this is a bit flakier as refactor operations may miss the string constant and silently break the application)

if (parameter.ParameterType.Name == "TheTypeName") {
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文