Boo.Lang.Compiler.TypeSystem.GenericsServices.GetTypeGenerity C# (CSharp) Method

GetTypeGenerity() public static method

Determines the number of open generic parameters in the specified type.
public static GetTypeGenerity ( IType type ) : int
type IType
return int
        public static int GetTypeGenerity(IType type)
        {
            // Dive into arrays and refs
            if (type.IsByRef || type.IsArray)
            {
                return GetTypeGenerity(type.GetElementType());
            }

            // A generic parameter has a generity of one
            if (type is IGenericParameter)
            {
                return 1;
            }

            // Generic parameters and generic arguments both contribute
            // to a types genrity
            int generity = 0;

            // A generic type gets a generity equals to the number its type parameters
            if (type.GenericInfo != null)
            {
                generity += type.GenericInfo.GenericParameters.Length;
            }

            // A constructed type gets the accumulated generity of its type arguments
            if (type.ConstructedInfo != null)
            {
                foreach (IType typeArg in type.ConstructedInfo.GenericArguments)
                {
                    generity += GetTypeGenerity(typeArg);
                }
            }

            return generity;
        }

Usage Example

        private int MoreSpecific(IType t1, IType t2)
        {
            // Dive into array types and ref types
            if (t1.IsArray && t2.IsArray || t1.IsByRef && t2.IsByRef)
            {
                return(MoreSpecific(t1.ElementType, t2.ElementType));
            }

            // A more concrete type is more specfic
            int result = GenericsServices.GetTypeConcreteness(t1) - GenericsServices.GetTypeConcreteness(t2);

            if (result != 0)
            {
                return(result);
            }

            // With equal concreteness, the more generic type is more specific.
            //First search for open args, then for all args
            result = GenericsServices.GetTypeGenerity(t1) - GenericsServices.GetTypeGenerity(t2);
            if (result != 0)
            {
                return(result);
            }
            result = GenericsServices.GetTypeGenericDepth(t1) - GenericsServices.GetTypeGenericDepth(t2);
            if (result != 0)
            {
                return(result);
            }

            // If both types have the same genrity, the deeper-nested type is more specific
            return(GetLogicalTypeDepth(t1) - GetLogicalTypeDepth(t2));
        }
All Usage Examples Of Boo.Lang.Compiler.TypeSystem.GenericsServices::GetTypeGenerity