public static bool GetFirstDerivedOfGenericType(Type type, Type genericType, out Type derivedType) { if (type.GetIsGenericType() && type.GetGenericTypeDefinition() == genericType) { derivedType = type; return true; } if (type.GetBaseType() != null) return GetFirstDerivedOfGenericType(type.GetBaseType(), genericType, out derivedType); derivedType = null; return false; }
public static AppServerMetadata GetAppServerMetadata(Type serverType) { if (serverType == null) throw new ArgumentNullException("serverType"); var topType = serverType; var attType = typeof(AppServerMetadataAttribute); while (true) { var atts = serverType.GetCustomAttributes(attType, false); if (atts != null && atts.Length > 0) { var metatdata = atts[0] as AppServerMetadataAttribute; return new AppServerMetadata(metatdata, topType); } var baseType = serverType.GetBaseType(); if (baseType == null) return null; serverType = baseType; } }
internal static Type GetFinalSystemType(Type type) { while (typeof(IPythonObject).IsAssignableFrom(type) && !type.GetTypeInfo().IsDefined(typeof(DynamicBaseTypeAttribute), false)) { type = type.GetBaseType(); } return type; }
private static Type GetCommonBaseType(Type xType, Type yType) { if (xType.IsSubclassOf(yType)) { return yType; } else if (yType.IsSubclassOf(xType)) { return xType; } else if (xType == yType) { return xType; } Type xBase = xType.GetBaseType(); Type yBase = yType.GetBaseType(); if (xBase != null) { Type res = GetCommonBaseType(xBase, yType); if (res != null) { return res; } } if (yBase != null) { Type res = GetCommonBaseType(xType, yBase); if (res != null) { return res; } } return null; }
/// <summary> /// Is this a type used for instances Python types (and not for the types themselves)? /// </summary> public static bool IsInstanceType(Type type) { return type.FullName.IndexOf(NewTypeMaker.TypePrefix) == 0 || // Users can create sub-types of instance-types using __clrtype__ without using // NewTypeMaker.TypePrefix ((type.GetBaseType() != null) && IsInstanceType(type.GetBaseType())); }
/// <summary> /// Converts a object to a string value and indicates its type and whether it successfully converted it or not /// </summary> /// <param name="value">Value to Convert</param> /// <param name="newStringValue">New Converted Value</param> /// <param name="valueType">Type of the original Value</param> /// <returns>True if it was converted or false if it was not</returns> internal static bool TryConvertPrimitiveValueToString(object value, out string newStringValue, out Type valueType) { valueType = null; newStringValue = null; if (value == null) { return true; } valueType = value.GetType(); if (valueType == typeof(bool)) { var valueToConvert = (bool)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(byte)) { var valueToConvert = (byte)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(char)) { var valueToConvert = (char)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(DateTime)) { var valueToConvert = (DateTime)value; newStringValue = PlatformHelper.ConvertDateTimeToString(valueToConvert); } else if (valueType == typeof(DateTimeOffset)) { var valueToConvert = (DateTimeOffset)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(decimal)) { var valueToConvert = (decimal)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(double)) { var valueToConvert = (double)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(float)) { var valueToConvert = (float)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(Guid)) { var valueToConvert = (Guid)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(int)) { var valueToConvert = (int)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(long)) { var valueToConvert = (long)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(sbyte)) { var valueToConvert = (sbyte)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(short)) { var valueToConvert = (short)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(string)) { newStringValue = (string)value; } else if (valueType == typeof(TimeSpan)) { var valueToConvert = (TimeSpan)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(uint)) { var valueToConvert = (uint)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(ulong)) { var valueToConvert = (ulong)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType == typeof(ushort)) { var valueToConvert = (ushort)value; newStringValue = XmlConvert.ToString(valueToConvert); } else if (valueType.GetBaseType() == typeof(Enum)) { newStringValue = Enum.GetName(valueType, value); if (newStringValue == null) { Type underlyingValueType; TryConvertPrimitiveValueToString(DataUtilities.ConvertFromEnum(value), out newStringValue, out underlyingValueType); } } else { return false; } return true; }
/// <summary> /// Provides generic type inference for a single parameter. /// </summary> /// <remarks> /// For example: /// M{T}(T x) /// M{T}(IList{T} x) /// M{T}(ref T x) /// M{T}(T[] x) /// M{T}(ref Dictionary{T,T}[] x) /// </remarks> public static Type GetInferedType(Type/*!*/ genericParameter, Type/*!*/ parameterType, Type inputType, Type/*!*/ argType, Dictionary<Type, Type>/*!*/ binding) { Debug.Assert(genericParameter.IsGenericParameter); if (parameterType.IsGenericParameter) { if (inputType != null) { binding[genericParameter] = inputType; if (ConstraintsViolated(inputType, genericParameter, binding)) { return null; } } return inputType; } if (parameterType.IsInterface()) { return GetInferedTypeForInterface(genericParameter, parameterType, inputType, binding); } if (parameterType.IsArray) { return binding[genericParameter] = MatchGenericParameter(genericParameter, argType, parameterType, binding); } if (parameterType.IsByRef) { if (CompilerHelpers.IsStrongBox(argType)) { argType = argType.GetGenericArguments()[0]; } return binding[genericParameter] = MatchGenericParameter(genericParameter, argType, parameterType.GetElementType(), binding); } // see if we're anywhere in our base class hierarchy Type genType = parameterType.GetGenericTypeDefinition(); while (argType != typeof(object)) { if (argType.IsGenericType() && argType.GetGenericTypeDefinition() == genType) { // TODO: Merge w/ the interface logic? return binding[genericParameter] = MatchGenericParameter(genericParameter, argType, parameterType, binding); } argType = argType.GetBaseType(); } return null; }
private static Type[] GetTypeHierarchy(Type type, out PropertyInfo[] keyProperties, out bool hasProperties) { keyProperties = ClientTypeUtil.GetKeyPropertiesOnType(type, out hasProperties); List<Type> list = new List<Type>(); if (keyProperties != null) { Type declaringType; if (keyProperties.Length > 0) { declaringType = keyProperties[0].DeclaringType; } else { declaringType = type; while (!declaringType.GetCustomAttributes(false).OfType<DataServiceEntityAttribute>().Any<DataServiceEntityAttribute>() && (declaringType.GetBaseType() != null)) { declaringType = declaringType.GetBaseType(); } } do { list.Insert(0, type); } while ((type != declaringType) && ((type = type.GetBaseType()) != null)); } else { do { list.Insert(0, type); } while (((type = type.GetBaseType()) != null) && ClientTypeUtil.GetPropertiesOnType(type, false).Any<PropertyInfo>()); } return list.ToArray(); }
/// <summary>Finds whether a non-primitive implements IEnumerable and returns element type if it does.</summary> /// <param name="seqType">Type to check.</param> /// <returns>Type of the element if the <paramref name="seqType"/> implements IEnumerable{T}. Otherwise null.</returns> private static Type FindIEnumerableForNonPrimitiveType(Type seqType) { Debug.Assert(seqType != null, "seqType != null"); Debug.Assert(seqType != typeof(string) && !seqType.IsPrimitive() && !seqType.IsValueType() && Nullable.GetUnderlyingType(seqType) == null, "seqType must not be a primitive type (nullable or not)"); if (seqType.IsArray) { return typeof(IEnumerable<>).MakeGenericType(seqType.GetElementType()); } if (seqType.IsGenericType()) { foreach (Type arg in seqType.GetGenericArguments()) { Type ienum = typeof(IEnumerable<>).MakeGenericType(arg); if (ienum.IsAssignableFrom(seqType)) { return ienum; } } } IEnumerable<Type> ifaces = seqType.GetInterfaces(); if (ifaces != null) { foreach (Type iface in ifaces) { Type ienum = FindIEnumerable(iface); if (ienum != null) { return ienum; } } } if (seqType.GetBaseType() != null && seqType.GetBaseType() != typeof(object)) { return FindIEnumerable(seqType.GetBaseType()); } return null; }
public static MethodInfo GetMethod( Type type, string name, Type[] parameterTypes ) { while ( type != typeof( object ) && type != null ) { #if !NETFX_CORE && !NETSTANDARD1_1 && !NETSTANDARD1_3 var candidate = type.GetMethod( name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, parameterTypes, null ); if ( candidate != null ) { return candidate; } #else var candidates = type.GetTypeInfo().GetDeclaredMethods( name ) .Where( m => !m.IsGenericMethod && !m.IsStatic && m.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) ) .ToArray(); switch ( candidates.Length ) { case 0: { break; } case 1: { return candidates[ 0 ]; } default: { // CallingConvention, static-instance, or so -- extremely rare case. throw new AmbiguousMatchException(); } } #endif // !NETFX_CORE && !NETSTANDARD1_1 && !NETSTANDARD1_3 type = type.GetBaseType(); } return null; }
public static FieldInfo GetField( Type type, string name ) { while ( type != typeof( object ) && type != null ) { var candidate = #if !NETFX_CORE && !NETSTANDARD1_1 && !NETSTANDARD1_3 type.GetField( name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly ); #else type.GetTypeInfo().GetDeclaredField( name ); #endif // !NETFX_CORE && !NETSTANDARD1_1 && !NETSTANDARD1_3 if ( candidate != null ) { return candidate; } type = type.GetBaseType(); } return null; }
private static Type FindIEnumerableForNonPrimitiveType(Type seqType) { if (seqType.IsArray) { return typeof(IEnumerable<>).MakeGenericType(new Type[] { seqType.GetElementType() }); } if (seqType.IsGenericType()) { foreach (Type type in seqType.GetGenericArguments()) { Type type2 = typeof(IEnumerable<>).MakeGenericType(new Type[] { type }); if (type2.IsAssignableFrom(seqType)) { return type2; } } } IEnumerable<Type> interfaces = seqType.GetInterfaces(); if (interfaces != null) { foreach (Type type3 in interfaces) { Type type4 = FindIEnumerable(type3); if (type4 != null) { return type4; } } } if ((seqType.GetBaseType() != null) && (seqType.GetBaseType() != typeof(object))) { return FindIEnumerable(seqType.GetBaseType()); } return null; }