如何识别C#方法中的每个参数类型?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请使用 ParameterInfo.ParameterType
如果需要, 要检查检索后的
System.Type
,您可以使用IsPrimitive
和IsByRef
,正如David提到的。此外,您还可以使用IsValueType
。 System.Type 类中有大量 Is* 属性。您最好的选择是检查每个 Is* 属性的 MSDN 文档,即...IsClass
状态...因此可以推断不需要调用
IsValueType
。请记住,给定类型可以在多个属性中返回 true,因为IsClass
可以返回 true,并且IsPassByRef
可以返回 true。也许为已知的 CLR 类型提供逻辑,因为这些类型不会改变,并且您提前知道这些类型,然后构建用户定义的复杂类型的逻辑。您也可以采用构建逻辑的方法来为 CLR 类型执行此操作;无论哪种方式都会起作用。Make use of ParameterInfo.ParameterType
If you need to check the
System.Type
once retrieved you can useIsPrimitive
andIsByRef
as mentioned by David. In addition you can also useIsValueType
. 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...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 thatIsClass
could return true ANDIsPassByRef
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.要获取参数的实际
Type
,请使用ParameterInfo
值上的ParameterType
。通过该值,您可以通过多种方式使用它来识别类型。最简单的方法是直接与已知类型进行比较,或者在类型不可用的情况下,可以使用名称匹配(尽管这有点不稳定,因为重构操作可能会错过字符串常量并默默地破坏应用程序)
To get the actual
Type
of the parameter use theParameterType
on theParameterInfo
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 typeOr 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)