@@ -47,7 +47,7 @@ public void CanProxyGenericMethodWithInterfaceConstraint()
47
47
var c = ( MyClass ) factory . CreateProxy ( typeof ( MyClass ) , new PassThroughInterceptor ( new MyClass ( ) ) , null ) ;
48
48
c . MethodWithInterfaceConstraint < IMyInterface > ( new MyDerivedClass ( ) ) . Should ( ) . Be ( typeof ( IMyInterface ) ) ;
49
49
}
50
-
50
+
51
51
[ Test ]
52
52
public void CanProxyGenericMethodWithReferenceTypeAndInterfaceConstraint ( )
53
53
{
@@ -56,6 +56,160 @@ public void CanProxyGenericMethodWithReferenceTypeAndInterfaceConstraint()
56
56
c . MethodWithReferenceTypeAndInterfaceConstraint < MyDerivedClass > ( ) . Should ( ) . Not . Be . Null ( ) ;
57
57
}
58
58
59
+ [ Test ]
60
+ public void CanProxySelfCastingMethodWithGenericConstaint ( )
61
+ {
62
+ var factory = new ProxyFactory ( ) ;
63
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
64
+ c . As < MyGenericClass < int > > ( ) . Should ( ) . Not . Be . Null ( ) ;
65
+ }
66
+
67
+ [ Test ]
68
+ public void CanProxySelfCastingMethodWithGenericConstaintBase ( )
69
+ {
70
+ var factory = new ProxyFactory ( ) ;
71
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
72
+ c . AsBase < MyGenericClass < int > > ( ) . Should ( ) . Not . Be . Null ( ) ;
73
+ }
74
+
75
+ [ Test ]
76
+ public void CanProxySelfCastingMethodWithGenericInterfaceConstaint ( )
77
+ {
78
+ var factory = new ProxyFactory ( ) ;
79
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
80
+ c . AsInterface < MyGenericClass < int > > ( ) . Should ( ) . Not . Be . Null ( ) ;
81
+ }
82
+
83
+ [ Test ]
84
+ public void CanProxySelfCastingMethodWithGenericInterfaceBaseConstaint ( )
85
+ {
86
+ var factory = new ProxyFactory ( ) ;
87
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
88
+ c . AsInterfaceBase < MyGenericClass < int > > ( ) . Should ( ) . Not . Be . Null ( ) ;
89
+ }
90
+
91
+ [ Test ]
92
+ public void CanProxySelfCastingMethodWithGenericConstaint2 ( )
93
+ {
94
+ var factory = new ProxyFactory ( ) ;
95
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
96
+ c . As2 < MyGenericClass < object > > ( ) . Should ( ) . Not . Be . Null ( ) ;
97
+ }
98
+
99
+ [ Test ]
100
+ public void CanProxySelfCastingMethodWithGenericConstaintBase2 ( )
101
+ {
102
+ var factory = new ProxyFactory ( ) ;
103
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
104
+ c . AsBase2 < MyGenericClass < object > > ( ) . Should ( ) . Not . Be . Null ( ) ;
105
+ }
106
+
107
+ [ Test ]
108
+ public void CanProxySelfCastingMethodWithGenericInterfaceConstaint2 ( )
109
+ {
110
+ var factory = new ProxyFactory ( ) ;
111
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
112
+ c . AsInterface2 < MyGenericClass < object > > ( ) . Should ( ) . Not . Be . Null ( ) ;
113
+ }
114
+
115
+ [ Test ]
116
+ public void CanProxySelfCastingMethodWithGenericInterfaceBaseConstaint2 ( )
117
+ {
118
+ var factory = new ProxyFactory ( ) ;
119
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
120
+ c . AsInterfaceBase2 < MyGenericClass < object > > ( ) . Should ( ) . Not . Be . Null ( ) ;
121
+ }
122
+
123
+ [ Test ]
124
+ public void CanProxySelfCastingMethodWithGenericConstaint3 ( )
125
+ {
126
+ var factory = new ProxyFactory ( ) ;
127
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
128
+ c . As3 < MyGenericClass < object > , object > ( ) . Should ( ) . Not . Be . Null ( ) ;
129
+ }
130
+
131
+ [ Test ]
132
+ public void CanProxySelfCastingMethodWithGenericConstaintBase3 ( )
133
+ {
134
+ var factory = new ProxyFactory ( ) ;
135
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
136
+ c . AsBase3 < MyGenericClassBase < object , object > , object > ( ) . Should ( ) . Not . Be . Null ( ) ;
137
+ }
138
+
139
+ [ Test ]
140
+ public void CanProxySelfCastingMethodWithGenericInterfaceConstaint3 ( )
141
+ {
142
+ var factory = new ProxyFactory ( ) ;
143
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
144
+ c . AsInterface3 < MyGenericClass < object > , object > ( ) . Should ( ) . Not . Be . Null ( ) ;
145
+ }
146
+
147
+ [ Test ]
148
+ public void CanProxySelfCastingMethodWithGenericInterfaceBaseConstaint3 ( )
149
+ {
150
+ var factory = new ProxyFactory ( ) ;
151
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
152
+ c . AsInterfaceBase3 < MyGenericClass < object > , object > ( ) . Should ( ) . Not . Be . Null ( ) ;
153
+ }
154
+
155
+ [ Test ]
156
+ public void CanProxySelfCastingMethodWithGenericConstaint4 ( )
157
+ {
158
+ var factory = new ProxyFactory ( ) ;
159
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
160
+ c . As4 < MyGenericClass < int > > ( ) . Should ( ) . Not . Be . Null ( ) ;
161
+ }
162
+
163
+ [ Test ]
164
+ public void CanProxySelfCastingMethodWithGenericConstaintBase4 ( )
165
+ {
166
+ var factory = new ProxyFactory ( ) ;
167
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
168
+ c . AsBase4 < MyGenericClass < int > > ( ) . Should ( ) . Not . Be . Null ( ) ;
169
+ }
170
+
171
+ [ Test ]
172
+ public void CanProxySelfCastingMethodWithGenericInterfaceConstaint4 ( )
173
+ {
174
+ var factory = new ProxyFactory ( ) ;
175
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
176
+ c . AsInterface4 < MyGenericClass < int > > ( ) . Should ( ) . Not . Be . Null ( ) ;
177
+ }
178
+
179
+ [ Test ]
180
+ public void CanProxySelfCastingMethodWithGenericInterfaceBaseConstaint4 ( )
181
+ {
182
+ var factory = new ProxyFactory ( ) ;
183
+ var c = ( MyGenericClass < int > ) factory . CreateProxy ( typeof ( MyGenericClass < int > ) , new PassThroughInterceptor ( new MyGenericClass < int > ( ) ) , null ) ;
184
+ c . AsInterfaceBase4 < MyGenericClass < int > > ( ) . Should ( ) . Not . Be . Null ( ) ;
185
+ }
186
+
187
+ [ Test ]
188
+ public void GenericTypeConstraint ( )
189
+ {
190
+ var type = typeof ( MyGenericClass < int > ) ;
191
+ var method = type . GetMethod ( "As" ) ;
192
+
193
+ var genericArgument = method . GetGenericArguments ( ) [ 0 ] ; // TRequestedType : class, IMyGenericInterface<TId>
194
+ var typeConstraint = genericArgument . GetGenericParameterConstraints ( ) [ 0 ] ; // MyGenericClass<TId>
195
+
196
+ Assert . That ( typeConstraint , Is . EqualTo ( typeof ( MyGenericClass < > ) ) ) ;
197
+ Assert . That ( typeConstraint . GetGenericTypeDefinition ( ) , Is . EqualTo ( typeof ( MyGenericClass < > ) ) ) ;
198
+ }
199
+
200
+ [ Test ]
201
+ public void GenericInterfaceConstraint ( )
202
+ {
203
+ var type = typeof ( MyGenericClass < int > ) ;
204
+ var method = type . GetMethod ( "AsInterface" ) ;
205
+
206
+ var genericArgument = method . GetGenericArguments ( ) [ 0 ] ; // TRequestedType : class, IMyGenericInterface<TId>
207
+ var typeConstraint = genericArgument . GetGenericParameterConstraints ( ) [ 0 ] ; // IMyGenericInterface<TId>
208
+ var typeConstraintGenericArgument = typeConstraint . GetGenericArguments ( ) [ 0 ] ; // TId
209
+
210
+ Assert . That ( typeConstraint . GetGenericTypeDefinition ( ) , Is . EqualTo ( typeof ( IMyGenericInterface < > ) ) ) ;
211
+ }
212
+
59
213
class MyDerivedClass : MyClass , IMyInterface
60
214
{ }
61
215
}
0 commit comments