Beispiel #1
0
        public T GetAttribute <T>(Type type) where T : Attribute
        {
            T attribute;

            Type metadataType = XBaseTypes.GetAssociatedMetadataType(type);

            if (metadataType != null)
            {
                attribute = XAttributes.GetAttribute <T>(metadataType, true);
                if (attribute != null)
                {
                    return(attribute);
                }
            }

            attribute = XAttributes.GetAttribute <T>(type, true);
            if (attribute != null)
            {
                return(attribute);
            }

            foreach (Type typeInterface in type.GetInterfaces())
            {
                attribute = XAttributes.GetAttribute <T>(typeInterface, true);
                if (attribute != null)
                {
                    return(attribute);
                }
            }

            return(null);
        }
Beispiel #2
0
        public bool ImplementInterface(Type type, Type interfaceType)
        {
            for (Type currentType = type; currentType != null; currentType = XBaseTypes.BaseType(currentType))
            {
                IEnumerable <Type> interfaces = currentType.GetInterfaces();
                foreach (Type i in interfaces)
                {
                    if (i == interfaceType || (i != null && ImplementInterface(i, interfaceType)))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Beispiel #3
0
            private static int ChooseMorePreciseType(Type type1, Type type2)
            {
                if (type1.IsByRef || type2.IsByRef)
                {
                    if (type1.IsByRef && type2.IsByRef)
                    {
                        type1 = type1.GetElementType();
                        type2 = type2.GetElementType();
                    }
                    else if (type1.IsByRef)
                    {
                        type1 = type1.GetElementType();
                        if (type1 == type2)
                        {
                            return(1);
                        }
                    }
                    else
                    {
                        type2 = type2.GetElementType();
                        if (type2 == type1)
                        {
                            return(-1);
                        }
                    }
                }

                bool c1FromC2, c2FromC1;

                if (XBaseTypes.IsPrimitive(type1) && XBaseTypes.IsPrimitive(type2))
                {
                    c1FromC2 = CanConvertPrimitive(type2, type1);
                    c2FromC1 = CanConvertPrimitive(type1, type2);
                }
                else
                {
                    c1FromC2 = type1.IsAssignableFrom(type2);
                    c2FromC1 = type2.IsAssignableFrom(type1);
                }

                if (c1FromC2 == c2FromC1)
                {
                    return(0);
                }

                return(c1FromC2 ? 1 : -1);
            }
Beispiel #4
0
 public bool HasDefaultConstructor(Type type, bool nonPublic)
 {
     return(XBaseTypes.HasDefaultConstructor(type, nonPublic));
 }
Beispiel #5
0
 public Assembly Assembly(Type type)
 {
     return(XBaseTypes.Assembly(type));
 }
Beispiel #6
0
 public Type BaseType(Type type)
 {
     return(XBaseTypes.BaseType(type));
 }
Beispiel #7
0
 public bool IsGenericTypeDefinition(Type type)
 {
     return(XBaseTypes.IsGenericTypeDefinition(type));
 }
Beispiel #8
0
 public string GetFullyQualifiedTypeName(Type t, ISerializationBinder binder)
 {
     return(XBaseTypes.GetFullyQualifiedTypeName(t, binder));
 }
Beispiel #9
0
        /// <summary>
        /// Checks if a set of values with given <paramref name="types"/> can be used
        /// to invoke a method with specified <paramref name="parameters"/>.
        /// </summary>
        /// <param name="parameters">Method parameters.</param>
        /// <param name="types">Argument types.</param>
        /// <param name="enableParamArray">Try to pack extra arguments into the last parameter when it is marked up with <see cref="ParamArrayAttribute"/>.</param>
        /// <returns><c>true</c> if method can be called with given arguments, <c>false</c> otherwise.</returns>
        private static bool FilterParameters(ParameterInfo[] parameters, IList <Type> types, bool enableParamArray)
        {
            XValidation.ArgumentNotNull(parameters, nameof(parameters));
            XValidation.ArgumentNotNull(types, nameof(types));

            if (parameters.Length == 0)
            {
                // fast check for parameterless methods
                return(types.Count == 0);
            }
            if (parameters.Length > types.Count)
            {
                // not all declared parameters were specified (optional parameters are not supported)
                return(false);
            }

            // check if the last parameter is ParamArray
            Type paramArrayType = null;

            if (enableParamArray)
            {
                ParameterInfo lastParam = parameters[parameters.Length - 1];
                if (lastParam.ParameterType.IsArray && lastParam.IsDefined(typeof(ParamArrayAttribute)))
                {
                    paramArrayType = lastParam.ParameterType.GetElementType();
                }
            }

            if (paramArrayType == null && parameters.Length != types.Count)
            {
                // when there's no ParamArray, number of parameters should match
                return(false);
            }

            for (int i = 0; i < types.Count; i++)
            {
                Type paramType = (paramArrayType != null && i >= parameters.Length - 1) ? paramArrayType : parameters[i].ParameterType;

                if (paramType == types[i])
                {
                    // exact match with provided type
                    continue;
                }

                if (paramType == typeof(object))
                {
                    // parameter of type object matches anything
                    continue;
                }

                if (XBaseTypes.IsPrimitive(paramType))
                {
                    if (!XBaseTypes.IsPrimitive(types[i]) || !CanConvertPrimitive(types[i], paramType))
                    {
                        // primitive parameter can only be assigned from compatible primitive type
                        return(false);
                    }
                }
                else
                {
                    if (!paramType.IsAssignableFrom(types[i]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #10
0
 public Type GetObjectType(object v)
 {
     return(XBaseTypes.GetObjectType(v));
 }
Beispiel #11
0
 public bool IsVisible(Type type)
 {
     return(XBaseTypes.IsVisible(type));
 }
Beispiel #12
0
 public bool IsAbstract(Type type)
 {
     return(XBaseTypes.IsAbstract(type));
 }
Beispiel #13
0
 public bool IsSealed(Type type)
 {
     return(XBaseTypes.IsSealed(type));
 }
Beispiel #14
0
 public bool IsAttribute(Type type)
 {
     return(XBaseTypes.IsAttribute(type));
 }
Beispiel #15
0
 public bool ContainsGenericParameters(Type type)
 {
     return(XBaseTypes.ContainsGenericParameters(type));
 }
Beispiel #16
0
 public bool IsGenericType(Type type)
 {
     return(XBaseTypes.IsGenericType(type));
 }
Beispiel #17
0
 public bool IsPrimitive(Type type)
 {
     return(XBaseTypes.IsPrimitive(type));
 }
Beispiel #18
0
 public string RemoveAssemblyDetails(string fullyQualifiedTypeName)
 {
     return(XBaseTypes.RemoveAssemblyDetails(fullyQualifiedTypeName));
 }
Beispiel #19
0
 public bool AssignableToTypeName(Type type, string fullTypeName, bool searchInterfaces)
 {
     return(XBaseTypes.AssignableToTypeName(type, fullTypeName, searchInterfaces));
 }
Beispiel #20
0
 public static bool IsDictionaryType(Type type)
 {
     return(XBaseTypes.IsDictionaryType(type));
 }
Beispiel #21
0
 public string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, ISerializationBinder binder)
 {
     return(XBaseTypes.GetTypeName(t, assemblyFormat, binder));
 }