@@ -68,6 +68,44 @@ public void NotNullConstraint()
68
68
Assert . AreEqual ( null , method . Invoke ( instance , [ null ] ) ) ;
69
69
}
70
70
71
+ [ TestMethod ]
72
+ public void StructConstraint ( )
73
+ {
74
+ SourceTypeInfo type = SourceReflector . GetRequiredType < GenericMethodTestObject > ( ) ;
75
+
76
+ var method = type . GetMethod ( "InvokeStructConstraint" ) ! ;
77
+
78
+ GenericMethodTestObject instance = new ( ) ;
79
+
80
+ Assert . ThrowsException < InvalidOperationException > ( ( ) =>
81
+ {
82
+ Assert . AreEqual ( 1 , method . Invoke ( instance , [ 1 ] ) ) ;
83
+ } ) ;
84
+
85
+ var value = method . MethodInfo . MakeGenericMethod ( typeof ( int ) ) . Invoke ( instance , [ 1 ] ) ;
86
+ Assert . AreEqual ( 1 , value ) ;
87
+ }
88
+
89
+ [ TestMethod ]
90
+ public void EnumConstraint ( )
91
+ {
92
+ SourceTypeInfo type = SourceReflector . GetRequiredType < GenericMethodTestObject > ( ) ;
93
+
94
+ var method = type . GetMethod ( "InvokeEnumConstraint" ) ! ;
95
+
96
+ GenericMethodTestObject instance = new ( ) ;
97
+
98
+
99
+ var aa = method . Invoke ( instance , [ EnumTestObject . B ] ) ;
100
+
101
+ Assert . AreEqual ( EnumTestObject . A , method . Invoke ( instance , [ EnumTestObject . A ] ) ) ;
102
+ Assert . AreEqual ( EnumTestObject . B , method . Invoke ( instance , [ EnumTestObject . B ] ) ) ;
103
+ Assert . ThrowsException < InvalidCastException > ( ( ) =>
104
+ {
105
+ Assert . AreEqual ( "abc" , method . Invoke ( instance , [ "abc" ] ) ) ;
106
+ } ) ;
107
+ }
108
+
71
109
[ TestMethod ]
72
110
public void DoubleInternfaceConstraint ( )
73
111
{
@@ -87,7 +125,6 @@ public void DoubleInternfaceConstraint()
87
125
} ) ;
88
126
}
89
127
90
-
91
128
[ TestMethod ]
92
129
public void DoubleTypeParameter ( )
93
130
{
@@ -138,6 +175,10 @@ public class GenericMethodTestObject
138
175
public T Invoke5 < T > ( T t ) where T : ICloneable , IComparable => t ;
139
176
public T Invoke6 < T , K > ( T t , K k ) where T : ICloneable where K : IComparable => t ;
140
177
public T [ ] InvokeArray1 < T > ( T [ ] t ) => t ;
178
+
179
+ public T InvokeStructConstraint < T > ( T t ) where T : struct => t ;
180
+ public T InvokeEnumConstraint < T > ( T t ) where T : Enum => t ;
181
+
141
182
}
142
183
143
184
0 commit comments