From 174e415c70843d905a6d40790cbdfdcf0f21cbdb Mon Sep 17 00:00:00 2001 From: Patrick Stevens <3138005+Smaug123@users.noreply.github.com> Date: Fri, 22 Aug 2025 20:14:19 +0100 Subject: [PATCH] Add constraints to generics (#104) --- WoofWare.PawPrint.Domain/MethodInfo.fs | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/WoofWare.PawPrint.Domain/MethodInfo.fs b/WoofWare.PawPrint.Domain/MethodInfo.fs index e5912bb..d17febc 100644 --- a/WoofWare.PawPrint.Domain/MethodInfo.fs +++ b/WoofWare.PawPrint.Domain/MethodInfo.fs @@ -52,6 +52,14 @@ module Parameter = result.ToImmutable () +type GenericVariance = + | Covariant + | Contravariant + +type GenericConstraint = + | Reference + | NonNullableValue + /// /// Represents a generic type or method parameter definition. /// Corresponds to GenericParameter in System.Reflection.Metadata. @@ -66,6 +74,10 @@ type GenericParameter = /// For example, in Dictionary<TKey, TValue&rt;, TKey has index 0 and TValue has index 1. /// SequenceNumber : int + + Variance : GenericVariance option + Constraint : GenericConstraint option + RequiresParameterlessConstructor : bool } [] @@ -79,9 +91,31 @@ module GenericParameter = |> Seq.map (fun param -> let param = metadata.GetGenericParameter param + let requiresParamlessCons = + param.Attributes.HasFlag GenericParameterAttributes.DefaultConstructorConstraint + + let constr = + if param.Attributes.HasFlag GenericParameterAttributes.NotNullableValueTypeConstraint then + Some GenericConstraint.NonNullableValue + elif param.Attributes.HasFlag GenericParameterAttributes.ReferenceTypeConstraint then + Some GenericConstraint.Reference + else + None + + let variance = + if param.Attributes.HasFlag GenericParameterAttributes.Contravariant then + Some GenericVariance.Contravariant + elif param.Attributes.HasFlag GenericParameterAttributes.Covariant then + Some GenericVariance.Covariant + else + None + { Name = metadata.GetString param.Name SequenceNumber = param.Index + Variance = variance + Constraint = constr + RequiresParameterlessConstructor = requiresParamlessCons } ) |> ImmutableArray.CreateRange