/// <summary> /// Guesses the IType by the type /// </summary> /// <param name="type">The type.</param> /// <returns></returns> public static IType GuessType(System.Type type) { if(type.IsGenericType && typeof(Nullable<>).Equals(type.GetGenericTypeDefinition())) { type = type.GetGenericArguments()[0]; } if (clrTypeToNHibernateType.ContainsKey(type)) { return clrTypeToNHibernateType[type]; } else if (type.IsEnum) { return (IType) Activator.CreateInstance(typeof (EnumType<>).MakeGenericType(type)); } else if ( typeof(IUserType).IsAssignableFrom(type) || typeof(ICompositeUserType).IsAssignableFrom(type)) { return Custom(type); } else { return Entity(type); } }
public Type FromSystemType(System.Type runtimeType) { if (runtimeType.IsGenericType) { runtimeType = runtimeType.GetGenericTypeDefinition(); } var query = from type in AllTypes where type.Key.FullName.Equals(runtimeType.FullName) select type; return query.Single(); }
internal static string Type(System.Type type, bool dropNamespaces = false) { string assemblyQualifiedName; Exception exception; if (type == null) { return string.Empty; } if (type.IsGenericType && !type.IsGenericTypeDefinition) { string str2 = Type(type.GetGenericTypeDefinition(), dropNamespaces); int num = str2.LastIndexOf('`'); int length = str2.Length - (str2.Length - num); StringBuilder builder = new StringBuilder(str2, 0, length, 0x200); builder.Append('['); bool flag = true; foreach (System.Type type2 in type.GetGenericArguments()) { if (!flag) { builder.Append(','); } flag = false; builder.Append(Type(type2, dropNamespaces)); } builder.Append(']'); assemblyQualifiedName = builder.ToString(); } else if (type.IsArray) { string str3 = Type(type.GetElementType(), dropNamespaces); StringBuilder builder2 = new StringBuilder(str3, str3.Length + 10); builder2.Append("["); for (int i = 0; i < (type.GetArrayRank() - 1); i++) { builder2.Append(","); } builder2.Append("]"); assemblyQualifiedName = builder2.ToString(); } else { assemblyQualifiedName = TypeAccelerators.FindBuiltinAccelerator(type) ?? (dropNamespaces ? type.Name : type.ToString()); } if ((!type.IsGenericParameter && !type.ContainsGenericParameters) && (!dropNamespaces && (LanguagePrimitives.ConvertStringToType(assemblyQualifiedName, out exception) != type))) { assemblyQualifiedName = type.AssemblyQualifiedName; } return assemblyQualifiedName; }
public ISerializationSurrogate GetSurrogate(System.Type type, StreamingContext context, out ISurrogateSelector selector) { if (type.IsGenericType) { System.Type genericTypeDefinition = type.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof (List<>)) { selector = (ISurrogateSelector) this; return ListSerializationSurrogate.Default; } if (genericTypeDefinition == typeof (Dictionary<,>)) { selector = (ISurrogateSelector) this; return (ISerializationSurrogate) Activator.CreateInstance(typeof (DictionarySerializationSurrogate<,>).MakeGenericType(type.GetGenericArguments())); } } selector = (ISurrogateSelector) null; return (ISerializationSurrogate) null; }
/// <summary> /// Guesses the IType by the type /// </summary> /// <param name="type">The type.</param> /// <returns></returns> public static IType GuessType(System.Type type) { if(type.IsGenericType && typeof(Nullable<>) == type.GetGenericTypeDefinition()) { type = type.GetGenericArguments()[0]; } IType value; if (clrTypeToNHibernateType.TryGetValue(type, out value)) return value; if (type.IsEnum) { return (IType) Activator.CreateInstance(typeof (EnumType<>).MakeGenericType(type)); } if (typeof(IUserType).IsAssignableFrom(type) || typeof(ICompositeUserType).IsAssignableFrom(type)) { return Custom(type); } return Entity(type); }
public bool IsPairWithStringKey(System.Type type, out System.Type typeOfValue) { typeOfValue = typeof(object); if (type.IsGenericType) { Type open = type.GetGenericTypeDefinition(); Type[] args = type.GetGenericArguments(); if (open == typeof(KeyValuePair<,>) && (args[0] == typeof(string) || args[0] == typeof(StringBuilder))) { typeOfValue = args[1]; return true; } } else if (type == typeof(DictionaryEntry)) return true; return false; }
public static object NullData(System.Type type, object dataSourceNullValue) { if (!type.IsGenericType || !(type.GetGenericTypeDefinition() == typeof(Nullable<>))) { return dataSourceNullValue; } if ((dataSourceNullValue != null) && (dataSourceNullValue != DBNull.Value)) { return dataSourceNullValue; } return null; }
private static bool IsNullableOfT(System.Type type) { return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)); }
private static bool IQueryableTypeFilter(System.Type m, object filterCriteria) { return (m.IsGenericType && (m.GetGenericTypeDefinition() == typeof(IQueryable<>))); }
private Type ImportImpl(System.Type type) { if (type.Assembly == typeof(IKVM.Reflection.Type).Assembly) { throw new ArgumentException("Did you really want to import " + type.FullName + "?"); } if (type.HasElementType) { if (type.IsArray) { if (type.Name.EndsWith("[]")) { return Import(type.GetElementType()).MakeArrayType(); } else { return Import(type.GetElementType()).MakeArrayType(type.GetArrayRank()); } } else if (type.IsByRef) { return Import(type.GetElementType()).MakeByRefType(); } else if (type.IsPointer) { return Import(type.GetElementType()).MakePointerType(); } else { throw new InvalidOperationException(); } } else if (type.IsGenericParameter) { if (type.DeclaringMethod != null) { throw new NotImplementedException(); } else { return Import(type.DeclaringType).GetGenericArguments()[type.GenericParameterPosition]; } } else if (type.IsGenericType && !type.IsGenericTypeDefinition) { System.Type[] args = type.GetGenericArguments(); Type[] importedArgs = new Type[args.Length]; for (int i = 0; i < args.Length; i++) { importedArgs[i] = Import(args[i]); } return Import(type.GetGenericTypeDefinition()).MakeGenericType(importedArgs); } else { return Import(type.Assembly).GetType(type.FullName); } }
private static System.Type BuildTypeConstraint(System.Type typeConstraint, System.Type declaringType) { var constraintGenericArguments = typeConstraint.GetGenericArguments(); var declaringTypeGenericArguments = declaringType.GetGenericArguments(); var parametersMap = declaringType .GetGenericTypeDefinition() .GetGenericArguments() .ToDictionary(x => x, x => declaringTypeGenericArguments[x.GenericParameterPosition]); var args = new System.Type[constraintGenericArguments.Length]; var make = false; for (int index = 0; index < constraintGenericArguments.Length; index++) { var genericArgument = constraintGenericArguments[index]; System.Type result; if (parametersMap.TryGetValue(genericArgument, out result)) { make = true; } else { result = genericArgument; } args[index] = result; } if (make) { return typeConstraint.GetGenericTypeDefinition().MakeGenericType(args); } return typeConstraint; }
private static bool IsAssignableToGenericType(System.Type givenType, System.Type genericType) { var interfaceTypes = givenType.GetInterfaces(); foreach (var it in interfaceTypes) { if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType) return true; } if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType) return true; System.Type baseType = givenType.BaseType; if (baseType == null) return false; return IsAssignableToGenericType(baseType, genericType); }
internal static bool IsNullableType(System.Type type) { return type != null && type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); }
private void GetTypeParts(System.Type type, out System.Type baseType, out ParameterData[] parameterDataArray, out int[] arrayRanks) { baseType = null; parameterDataArray = null; arrayRanks = null; if (type.IsArray) { ArrayList list = new ArrayList(); this.GetTypeParts(type.GetElementType(), out baseType, out parameterDataArray, out arrayRanks); if (arrayRanks != null) { list.AddRange(arrayRanks); } list.Add(type.GetArrayRank()); arrayRanks = (int[]) list.ToArray(typeof(int)); } else if (type.IsGenericType) { System.Type underlyingSystemType = null; System.Type type3 = null; if (type.ContainsGenericParameters) { type3 = null; underlyingSystemType = type.UnderlyingSystemType; } else { type3 = type; underlyingSystemType = type.GetGenericTypeDefinition().UnderlyingSystemType; } ArrayList list2 = new ArrayList(); for (int i = 0; i < underlyingSystemType.GetGenericArguments().Length; i++) { ParameterData data = new ParameterData { ParameterType = underlyingSystemType.GetGenericArguments()[i] }; if (type3 != null) { data.Type = type.GetGenericArguments()[i]; } list2.Add(data); } parameterDataArray = (ParameterData[]) list2.ToArray(typeof(ParameterData)); baseType = underlyingSystemType; } else { baseType = type; } }
public static bool IsArrayOrList(System.Type listType) { return listType.IsArray || (listType.IsGenericType && listType.GetGenericTypeDefinition() == typeof(List<>)); }
private static System.Type GetArrayOrListElementType(System.Type listType) { if (listType.IsArray) { return listType.GetElementType(); } if (listType.IsGenericType && listType.GetGenericTypeDefinition() == typeof(List<>)) { return listType.GetGenericArguments()[0]; } return null; }
string GetConstructorName(System.Type type) { if (type.IsGenericTypeDefinition) return FixGenericName(type.Name); if (type.IsGenericType) return GetConstructorName(type.GetGenericTypeDefinition()); return type.Name; }
private static ConstructorInfo GetCollectionConstructor(System.Type collectionType, System.Type elementType) { if (collectionType.IsInterface) { if (collectionType.IsGenericType && collectionType.GetGenericTypeDefinition() == typeof(ISet<>)) { return typeof(HashSet<>).MakeGenericType(elementType).GetConstructor(new[] { typeof(IEnumerable<>).MakeGenericType(elementType) }); } return null; } return collectionType.GetConstructor(new[] { typeof(IEnumerable<>).MakeGenericType(elementType) }); }
private string GetTypeName(System.Type type, bool full) { if (type.IsGenericParameter) return type.ToString(); if (type.IsByRef && type.HasElementType) return GetTypeName(type.GetElementType(), full); if (type.IsArray) return GetTypeName(type.GetElementType(), true) + "[]"; String prefix = ""; if (type.IsNestedPublic) prefix = GetTypeName(type.DeclaringType, false) + "+" + prefix; if (type.IsGenericType || type.IsGenericTypeDefinition) { StringBuilder sb = new StringBuilder(); String name = FixGenericName(type.IsGenericTypeDefinition ? type.Name : type.GetGenericTypeDefinition().Name); if (full && !String.IsNullOrEmpty(type.Namespace)) sb.Append(type.Namespace).Append("."); sb.Append(name); sb.Append(BuildTemplateParamsString(type.GetGenericArguments())); return prefix + sb.ToString(); } return full ? type.FullName : prefix + type.Name; }
private static bool IsNullable(System.Type returnType) { return returnType.IsGenericType && typeof(Nullable<>) == returnType.GetGenericTypeDefinition(); }
private static bool IsCollectionType(System.Type type) { return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>); }
public static bool IsNullableType(System.Type colType) { return ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(System.Nullable<>))); }
private RubyMethod FindCLRMethod(string methodId, System.Type clrtype) { BindingFlags flags = BindingFlags.Instance | BindingFlags.Public; if (this._type == Type.IClass) { // static methods if (methodId == "new") { if (clrtype.IsSubclassOf(typeof(System.Delegate))) { return new RubyMethod(new DelegateConstructor(clrtype), 0, Access.Public, this); } else { ConstructorInfo[] ci = clrtype.GetConstructors(); if (ci == null || ci.Length == 0) return null; MethodBase[] mi = new MethodBase[ci.Length]; System.Array.Copy(ci, mi, ci.Length); return new RubyMethod(new MultiMethod(mi), -1, Access.Public, this); } } else if (methodId == "allocator") { return new RubyMethod(Methods.rb_class_allocate_instance.singleton, 0, Access.Private, this); } else if (methodId == "[]") { // instantiate a generic type // ruby: type = ns::List[System::Int32] if (clrtype.IsGenericType) { // clrtype is Type`n+Inner but we are looking for Type`n+Inner`n // we need to strip generic arguments from the name, but supply // them to the GenericTypeGetter return new RubyMethod( new GenericTypeGetter( clrtype.Assembly, clrtype.GetGenericTypeDefinition().FullName, clrtype.GetGenericArguments()), -1, Access.Public, this); } else { return new RubyMethod( new GenericTypeGetter(clrtype.Assembly, clrtype.FullName, null), -1, Access.Public, this); } } flags = BindingFlags.Static | BindingFlags.Public; } bool is_setter = false; // methods ending with "=" are expected to be either // field or property setters if (methodId.EndsWith("=")) { is_setter = true; methodId = methodId.Substring(0, methodId.Length - 1); } // default member access, an Indexer in C# if (methodId == "[]") { object[] attributes = clrtype.GetCustomAttributes( typeof(System.Reflection.DefaultMemberAttribute), true); if (attributes.Length > 0) { methodId = ((DefaultMemberAttribute)attributes[0]).MemberName; } } MemberInfo[] members = clrtype.GetMember(methodId, flags); if (members.Length == 0) { // we didn't find a member with the exact name // but we still need to check for nested types with // additional type parameters string genericNestedId = methodId + "`"; foreach (System.Type nested in clrtype.GetNestedTypes(flags)) { if (nested.Name.StartsWith(genericNestedId)) { return new RubyMethod( new ValueMethod( new GenericContainer( clrtype.Assembly, clrtype.Name + "+" + methodId, clrtype.GetGenericArguments())), 0, Access.Public, this); } } return null; } if (members[0] is MethodBase) { if (is_setter) return null; MethodBase[] methods = new MethodBase[members.Length]; System.Array.Copy(members, methods, members.Length); return new RubyMethod(new MultiMethod(methods), -1, Access.Public, this); } if (members[0] is PropertyInfo) { // not all the property overloads may have the getter/setter // we're looking for, so we maintain a count and resize // the methods array later if necessary int count = 0; MethodBase[] methods = new MethodBase[members.Length]; foreach (PropertyInfo pi in members) { MethodInfo method = is_setter ? pi.GetSetMethod() : pi.GetGetMethod(); if (method != null) methods[count++] = method; } if (count == 0) return null; if (count < members.Length) System.Array.Resize(ref methods, count); return new RubyMethod(new MultiMethod(methods), -1, Access.Public, this); } FieldInfo field = members[0] as FieldInfo; if (field != null) { if (is_setter) return new RubyMethod(new FieldSetter(field), 1, Access.Public, this); else return new RubyMethod(new FieldGetter(field), 0, Access.Public, this); } //EventInfo eventinfo = members[0] as EventInfo; //if (eventinfo != null) //{ // return ...; //} // nested types System.Type type = members[0] as System.Type; if (type != null) { // see section 10.7.1 of ECMA if (type.IsGenericTypeDefinition) type = type.MakeGenericType(clrtype.GetGenericArguments()); return new RubyMethod( new NestedTypeGetter(Load(type, null, false)), 0, Access.Public, this); } return null; }
private static Boolean IsNullableEnum(System.Type typeClass) { if (!typeClass.IsGenericType) return false; System.Type nullable = typeof(Nullable<>); if (!nullable.Equals(typeClass.GetGenericTypeDefinition())) return false; System.Type genericClass = typeClass.GetGenericArguments()[0]; return genericClass.IsSubclassOf(typeof(Enum)); }
public static void Apply(Schema schema, SchemaRegistry schemaRegistry, System.Type type) { var workStr = type.ToString(); if (type.IsEnum) { EnumRegist(schemaRegistry, type); } if (IsNullableEnumType(type)) { var innerType = type.GetGenericArguments()[0]; EnumRegist(schemaRegistry, innerType); } if (schema.type == "array") { if (type.IsArray) { Apply(schema.items, schemaRegistry, type.GetElementType()); } else { Apply(schema.items, schemaRegistry, type.GetGenericArguments()[0]); } return; } if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { schema.vendorExtensions.Add("CsType", type.GetGenericArguments()[0].Name + "?"); schema.vendorExtensions.Add("CsNamespace", type.Namespace); } else { schema.vendorExtensions.Add("CsType", type.Name); schema.vendorExtensions.Add("CsNamespace", type.Namespace); } if (schema.properties != null) { foreach (var prop in schema.properties) { var pi = type.GetProperty(prop.Key); if (pi != null) { Apply(prop.Value, schemaRegistry, pi.PropertyType); } } } }
// TODO - code duplicated in LinqExtensionMethods private static bool IsNullableType(System.Type type) { return (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)); }
private static Hashtable typeMap; //contains weak references /// <summary> /// Gets a TypeNode instance corresponding to the given System.Type instance. /// </summary> /// <param name="type">A runtime type.</param> /// <returns>A TypeNode instance.</returns> public static TypeNode GetTypeNode(System.Type type) { if(type == null) return null; Hashtable typeMap = TypeNode.typeMap; if(typeMap == null) TypeNode.typeMap = typeMap = Hashtable.Synchronized(new Hashtable()); TypeNode result = null; WeakReference wr = (WeakReference)typeMap[type]; if(wr != null) { result = wr.Target as TypeNode; if(result == Class.DoesNotExist) return null; if(result != null) return result; } if(type.IsGenericType && !type.IsGenericTypeDefinition) { try { TypeNode template = TypeNode.GetTypeNode(type.GetGenericTypeDefinition()); if(template == null) return null; TypeNodeList templateArguments = new TypeNodeList(); foreach(Type arg in type.GetGenericArguments()) templateArguments.Add(TypeNode.GetTypeNode(arg)); return template.GetGenericTemplateInstance(template.DeclaringModule, templateArguments); } catch { //TODO: log error return null; } } if(type.IsGenericParameter) { try { int parIndx = type.GenericParameterPosition; System.Reflection.MethodInfo mi = type.DeclaringMethod as System.Reflection.MethodInfo; if(mi != null) { Method m = Method.GetMethod(mi); if(m == null) return null; if(m.TemplateParameters != null && m.TemplateParameters.Count > parIndx) return m.TemplateParameters[parIndx]; } else { System.Type ti = type.DeclaringType; TypeNode t = TypeNode.GetTypeNode(ti); if(t == null) return null; if(t.TemplateParameters != null && t.TemplateParameters.Count > parIndx) return t.TemplateParameters[parIndx]; } return null; } catch { //TODO: log error return null; } } if(type.HasElementType) { TypeNode elemType = TypeNode.GetTypeNode(type.GetElementType()); if(elemType == null) return null; if(type.IsArray) result = elemType.GetArrayType(type.GetArrayRank()); else if(type.IsByRef) result = elemType.GetReferenceType(); else if(type.IsPointer) result = elemType.GetPointerType(); else { Debug.Assert(false); result = null; } } else if(type.DeclaringType != null) { TypeNode dType = TypeNode.GetTypeNode(type.DeclaringType); if(dType == null) return null; result = dType.GetNestedType(Identifier.For(type.Name)); } else { AssemblyNode assem = AssemblyNode.GetAssembly(type.Assembly); if(assem != null) { result = assem.GetType(Identifier.For(type.Namespace), Identifier.For(type.Name)); } } if(result == null) typeMap[type] = new WeakReference(Class.DoesNotExist); else typeMap[type] = new WeakReference(result); return result; }
internal static System.Type GetDrawerTypeForType(System.Type type) { if (ScriptAttributeUtility.s_DrawerTypeForType == null) ScriptAttributeUtility.BuildDrawerTypeForTypeDictionary(); ScriptAttributeUtility.DrawerKeySet drawerKeySet; ScriptAttributeUtility.s_DrawerTypeForType.TryGetValue(type, out drawerKeySet); if (drawerKeySet.drawer != null || !type.IsGenericType) return drawerKeySet.drawer; ScriptAttributeUtility.s_DrawerTypeForType.TryGetValue(type.GetGenericTypeDefinition(), out drawerKeySet); return drawerKeySet.drawer; }
private static System.Type RemoveNullableFromType(System.Type type) { if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>))) { type = type.GetGenericArguments()[0]; return type; } if (type.IsByRef) { type = type.GetElementType(); } return type; }