示例#1
0
        /// <summary> Returns the element type of any class implementing <see cref="IEnumerable{T}" /> interface. Returns null when it is not found. </summary>
        public static TypeReference GetElementTypeFromIEnumerable(this SpecializedType collectionType, MetadataContext cx, bool allowIEnumerator, out bool?isGeneric)
        {
            bool foundNonGenericIEnumerable = false;

            foreach (var baseType in cx.GetBaseTypes(collectionType))
            {
                if (baseType.Type == TypeSignature.IEnumerableOfT || (allowIEnumerator && baseType.Type == TypeSignature.IEnumeratorOfT))
                {
                    isGeneric = true;
                    return(Assert.Single(baseType.TypeArguments));
                }
                if (baseType.Type == TypeSignature.IEnumerable || (allowIEnumerator && baseType.Type == TypeSignature.IEnumerator))
                {
                    foundNonGenericIEnumerable = true;
                }
            }
            // System.Collections.IEnumerable found in type hierarchy -> Object is element type.
            if (foundNonGenericIEnumerable)
            {
                isGeneric = false;
                return(TypeSignature.Object);
            }
            isGeneric = null;
            return(null);
        }
示例#2
0
 /// <summary>
 /// Gets whether this type definition is derived from the base type definition.
 /// </summary>
 public static bool IsDerivedFrom(this SpecializedType type, SpecializedType baseType, MetadataContext cx)
 {
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     if (baseType == null)
     {
         return(false);
     }
     return(cx.GetBaseTypes(type).Contains(baseType));
 }
示例#3
0
        /// <summary>
        /// Returns all declaring types of this type.
        /// The output is ordered so that inner types occur before outer types.
        /// </summary>
        public static IEnumerable <SpecializedType> GetDeclaringTypes(this SpecializedType type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            while (type is object)
            {
                yield return(type);

                type = type.DeclaringType();
            }
        }
示例#4
0
 /// <summary>
 /// Gets the invoke method for a delegate type.
 /// </summary>
 /// <remarks>
 /// Returns null if the type is not a delegate type; or if the invoke method could not be found.
 /// </remarks>
 public static MethodReference GetDelegateInvokeMethod(this SpecializedType @delegate, MetadataContext cx)
 {
     if (@delegate == null)
     {
         throw new ArgumentNullException("type");
     }
     if (@delegate.Type.Kind == "delegate")
     {
         return(cx.GetMemberMethods(@delegate).FirstOrDefault(m => m.Name() == "Invoke"));
     }
     else
     {
         return(null);
     }
 }
示例#5
0
        static partial void ValidateObjectExtension(ref CoreLib.ValidationErrorsBuilder e, SpecializedType t)
        {
            if (t.Type is null)
            {
                return;
            }
            var expectedCount = t.Type.TotalParameterCount();

            if (expectedCount != t.TypeArguments.Length)
            {
                e.Add(ValidationErrors.Create($"Type {t.Type} expected {expectedCount} parameters, got [{string.Join(", ", t.TypeArguments)}]"));
            }
        }
示例#6
0
 public static TypeSignature UnwrapSignature(SpecializedType st) =>
 st.TypeArguments.Length == 0 ? st.Type : throw new NotSupportedException($"Generic type is not supported in this context.");
示例#7
0
        /// <summary>
        /// Converts a delegate to a matching function type.
        /// </summary>
        /// <remarks>
        /// Returns null if the type is not a delegate type; or if the invoke method could not be found.
        /// </remarks>
        public static FunctionType DelegateToFunction(this SpecializedType @delegate, MetadataContext cx)
        {
            var invoke = @delegate.GetDelegateInvokeMethod(cx);

            return(invoke?.ToFunctionType());
        }