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); }
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); }
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); }
public bool HasDefaultConstructor(Type type, bool nonPublic) { return(XBaseTypes.HasDefaultConstructor(type, nonPublic)); }
public Assembly Assembly(Type type) { return(XBaseTypes.Assembly(type)); }
public Type BaseType(Type type) { return(XBaseTypes.BaseType(type)); }
public bool IsGenericTypeDefinition(Type type) { return(XBaseTypes.IsGenericTypeDefinition(type)); }
public string GetFullyQualifiedTypeName(Type t, ISerializationBinder binder) { return(XBaseTypes.GetFullyQualifiedTypeName(t, binder)); }
/// <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); }
public Type GetObjectType(object v) { return(XBaseTypes.GetObjectType(v)); }
public bool IsVisible(Type type) { return(XBaseTypes.IsVisible(type)); }
public bool IsAbstract(Type type) { return(XBaseTypes.IsAbstract(type)); }
public bool IsSealed(Type type) { return(XBaseTypes.IsSealed(type)); }
public bool IsAttribute(Type type) { return(XBaseTypes.IsAttribute(type)); }
public bool ContainsGenericParameters(Type type) { return(XBaseTypes.ContainsGenericParameters(type)); }
public bool IsGenericType(Type type) { return(XBaseTypes.IsGenericType(type)); }
public bool IsPrimitive(Type type) { return(XBaseTypes.IsPrimitive(type)); }
public string RemoveAssemblyDetails(string fullyQualifiedTypeName) { return(XBaseTypes.RemoveAssemblyDetails(fullyQualifiedTypeName)); }
public bool AssignableToTypeName(Type type, string fullTypeName, bool searchInterfaces) { return(XBaseTypes.AssignableToTypeName(type, fullTypeName, searchInterfaces)); }
public static bool IsDictionaryType(Type type) { return(XBaseTypes.IsDictionaryType(type)); }
public string GetTypeName(Type t, TypeNameAssemblyFormatHandling assemblyFormat, ISerializationBinder binder) { return(XBaseTypes.GetTypeName(t, assemblyFormat, binder)); }