@@ -23,7 +23,7 @@ public sealed class GenericParameter : TypeReference, ICustomAttributeProvider {
2323 internal IGenericParameterProvider owner ;
2424
2525 ushort attributes ;
26- Collection < TypeReference > constraints ;
26+ GenericParameterConstraintCollection constraints ;
2727 Collection < CustomAttribute > custom_attributes ;
2828
2929 public GenericParameterAttributes Attributes {
@@ -52,15 +52,15 @@ public bool HasConstraints {
5252 }
5353 }
5454
55- public Collection < TypeReference > Constraints {
55+ public Collection < GenericParameterConstraint > Constraints {
5656 get {
5757 if ( constraints != null )
5858 return constraints ;
5959
6060 if ( HasImage )
6161 return Module . Read ( ref constraints , this , ( generic_parameter , reader ) => reader . ReadGenericConstraints ( generic_parameter ) ) ;
6262
63- return constraints = new Collection < TypeReference > ( ) ;
63+ return constraints = new GenericParameterConstraintCollection ( this ) ;
6464 }
6565 }
6666
@@ -265,4 +265,94 @@ protected override void OnRemove (GenericParameter item, int index)
265265 items [ i ] . position = i - 1 ;
266266 }
267267 }
268+
269+ public sealed class GenericParameterConstraint : ICustomAttributeProvider {
270+
271+ internal GenericParameter generic_parameter ;
272+ internal MetadataToken token ;
273+
274+ TypeReference constraint_type ;
275+ Collection < CustomAttribute > custom_attributes ;
276+
277+ public TypeReference ConstraintType {
278+ get { return constraint_type ; }
279+ set { constraint_type = value ; }
280+ }
281+
282+ public bool HasCustomAttributes {
283+ get {
284+ if ( custom_attributes != null )
285+ return custom_attributes . Count > 0 ;
286+
287+ if ( generic_parameter == null )
288+ return false ;
289+
290+ return this . GetHasCustomAttributes ( generic_parameter . Module ) ;
291+ }
292+ }
293+
294+ public Collection < CustomAttribute > CustomAttributes {
295+ get {
296+ if ( generic_parameter == null )
297+ return custom_attributes = new Collection < CustomAttribute > ( ) ;
298+
299+ return custom_attributes ?? ( this . GetCustomAttributes ( ref custom_attributes , generic_parameter . Module ) ) ;
300+ }
301+ }
302+
303+ public MetadataToken MetadataToken {
304+ get { return token ; }
305+ set { token = value ; }
306+ }
307+
308+ internal GenericParameterConstraint ( TypeReference constraintType , MetadataToken token )
309+ {
310+ this . constraint_type = constraintType ;
311+ this . token = token ;
312+ }
313+
314+ public GenericParameterConstraint ( TypeReference constraintType )
315+ {
316+ Mixin . CheckType ( constraintType , Mixin . Argument . constraintType ) ;
317+
318+ this . constraint_type = constraintType ;
319+ this . token = new MetadataToken ( TokenType . GenericParamConstraint ) ;
320+ }
321+ }
322+
323+ class GenericParameterConstraintCollection : Collection < GenericParameterConstraint >
324+ {
325+ readonly GenericParameter generic_parameter ;
326+
327+ internal GenericParameterConstraintCollection ( GenericParameter genericParameter )
328+ {
329+ this . generic_parameter = genericParameter ;
330+ }
331+
332+ internal GenericParameterConstraintCollection ( GenericParameter genericParameter , int length )
333+ : base ( length )
334+ {
335+ this . generic_parameter = genericParameter ;
336+ }
337+
338+ protected override void OnAdd ( GenericParameterConstraint item , int index )
339+ {
340+ item . generic_parameter = generic_parameter ;
341+ }
342+
343+ protected override void OnInsert ( GenericParameterConstraint item , int index )
344+ {
345+ item . generic_parameter = generic_parameter ;
346+ }
347+
348+ protected override void OnSet ( GenericParameterConstraint item , int index )
349+ {
350+ item . generic_parameter = generic_parameter ;
351+ }
352+
353+ protected override void OnRemove ( GenericParameterConstraint item , int index )
354+ {
355+ item . generic_parameter = null ;
356+ }
357+ }
268358}
0 commit comments