C# 方法签名 - 限制类型 - 正确的术语是什么?
C# method signatures - restricting types - what's the correct terminology?
我在我正在处理的项目的 Common
class 中有一个方法,其定义如下:
public static void PopulateSoapBody<TEnum>(Object obj, string[] aMessage) where TEnum : struct, IComparable, IFormattable, IConvertible
它是这样调用的(来自几个不同的 classes 定义它们自己的枚举类型,并填充它们自己的 soap 体 classes):
DCSSCardUpdateType wsSoapBody = new DCSSCardUpdateType();
Common.PopulateSoapBody<CardPinRequest>(wsSoapBody, aMessage);
哪里
CardPINRequest is an Enum Type defined in the calling class
wsSoapBody is a class type defined in the web service
aMessage is a string array (used to populate the wsSoapBody)
当 enum 类型被传递给一个方法时,它被称为什么,限制了可能的类型(我想阅读它以更好地理解如何像这样使用功能 d)
我认为您要查找的字词是generic type constraints。
来自链接的 MSDN 文章:
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints.
我在我正在处理的项目的 Common
class 中有一个方法,其定义如下:
public static void PopulateSoapBody<TEnum>(Object obj, string[] aMessage) where TEnum : struct, IComparable, IFormattable, IConvertible
它是这样调用的(来自几个不同的 classes 定义它们自己的枚举类型,并填充它们自己的 soap 体 classes):
DCSSCardUpdateType wsSoapBody = new DCSSCardUpdateType();
Common.PopulateSoapBody<CardPinRequest>(wsSoapBody, aMessage);
哪里
CardPINRequest is an Enum Type defined in the calling class
wsSoapBody is a class type defined in the web service
aMessage is a string array (used to populate the wsSoapBody)
当 enum 类型被传递给一个方法时,它被称为什么,限制了可能的类型(我想阅读它以更好地理解如何像这样使用功能 d)
我认为您要查找的字词是generic type constraints。
来自链接的 MSDN 文章:
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints.