Exemple #1
0
 /// <summary>
 /// Check if the given type satisfies the constraints of the type parameter
 /// </summary>
 /// <param name="parameterSymbol"></param>
 /// <param name="typeSymbol"></param>
 /// <returns></returns>
 public static bool CanApply(this ITypeParameterSymbol parameterSymbol, ITypeSymbol typeSymbol)
 {
     // Check if the given type symbol is also a type parameter
     if (parameterSymbol.AreEqual(typeSymbol))
     {
         return(true);
     }
     // We have to check if all constraints can be applied to the given type
     if (parameterSymbol.HasConstructorConstraint)
     {
         // TODO: check if is a public ctor
         if (typeSymbol is INamedTypeSymbol namedTypeSymbol && !namedTypeSymbol.Constructors.Any(o => !o.Parameters.Any()))
         {
             return(false);
         }
         return(false);
     }
     if (parameterSymbol.HasReferenceTypeConstraint && !typeSymbol.IsReferenceType)
     {
         return(false);
     }
     if (parameterSymbol.HasReferenceTypeConstraint && !typeSymbol.IsReferenceType)
     {
         return(false);
     }
     return(parameterSymbol.ConstraintTypes.All(typeSymbol.InheritsFromOrEquals));
 }