private static IUserDataDescriptor PerformRegistration(Type type, IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor) { IUserDataDescriptor result = RegistrationPolicy.HandleRegistration(newDescriptor, oldDescriptor); if (result != oldDescriptor) { if (result == null) { s_TypeRegistry.Remove(type); } else { s_TypeRegistry[type] = result; s_TypeRegistryHistory[type] = result; } } return(result); }
/// <summary> /// Gets the best possible type descriptor for a specified CLR type. /// </summary> /// <param name="type">The CLR type for which the descriptor is desired.</param> /// <param name="searchInterfaces">if set to <c>true</c> interfaces are used in the search.</param> /// <returns></returns> internal static IUserDataDescriptor GetDescriptorForType(Type type, bool searchInterfaces) { lock (s_Lock) { IUserDataDescriptor typeDescriptor = null; // if the type has been explicitly registered, return its descriptor as it's complete if (type.IsGenericType) { IUserDataDescriptor iudd = null; if (s_TypeRegistry.TryGetValue(type.BaseType, out iudd)) { return(iudd); } } else { IUserDataDescriptor iudd = null; if (s_TypeRegistry.TryGetValue(type, out iudd)) { return(iudd); } } if (RegistrationPolicy.AllowTypeAutoRegistration(type)) { // no autoreg of delegates if (!Framework.Do.IsAssignableFrom((typeof(Delegate)), type)) { return(RegisterType_Impl(type, DefaultAccessMode, type.FullName, null)); } } // search for the base object descriptors for (Type t = type; t != null; t = Framework.Do.GetBaseType(t)) { IUserDataDescriptor u; if (s_TypeRegistry.TryGetValue(t, out u)) { typeDescriptor = u; break; } else if (Framework.Do.IsGenericType(t)) { if (s_TypeRegistry.TryGetValue(t.GetGenericTypeDefinition(), out u)) { typeDescriptor = u; break; } } } if (typeDescriptor is IGeneratorUserDataDescriptor) { typeDescriptor = ((IGeneratorUserDataDescriptor)typeDescriptor).Generate(type); } // we should not search interfaces (for example, it's just for statics..), no need to look further if (!searchInterfaces || typeDescriptor != null) { return(typeDescriptor); } if (searchInterfaces) { foreach (Type interfaceType in Framework.Do.GetInterfaces(type)) { IUserDataDescriptor interfaceDescriptor; if (s_TypeRegistry.TryGetValue(interfaceType, out interfaceDescriptor)) { if (interfaceDescriptor is IGeneratorUserDataDescriptor) { interfaceDescriptor = ((IGeneratorUserDataDescriptor)interfaceDescriptor).Generate(type); } if (interfaceDescriptor != null) { return(interfaceDescriptor); } } else if (Framework.Do.IsGenericType(interfaceType)) { if (s_TypeRegistry.TryGetValue(interfaceType.GetGenericTypeDefinition(), out interfaceDescriptor)) { if (interfaceDescriptor is IGeneratorUserDataDescriptor) { interfaceDescriptor = ((IGeneratorUserDataDescriptor)interfaceDescriptor).Generate(type); } if (interfaceDescriptor != null) { return(interfaceDescriptor); } } } } } return(null); } }
/// <summary> /// Gets the best possible type descriptor for a specified CLR type. /// </summary> /// <param name="type">The CLR type for which the descriptor is desired.</param> /// <param name="searchInterfaces">if set to <c>true</c> interfaces are used in the search.</param> /// <returns></returns> internal static IUserDataDescriptor GetDescriptorForType(Type type, bool searchInterfaces) { lock (s_Lock) { IUserDataDescriptor typeDescriptor = null; // if the type has been explicitly registered, return its descriptor as it's complete if (s_TypeRegistry.ContainsKey(type)) { return(s_TypeRegistry[type]); } if (RegistrationPolicy.AllowTypeAutoRegistration(type)) { // no autoreg of delegates if (!typeof(Delegate).IsAssignableFrom(type)) { return(RegisterType_Impl(type, DefaultAccessMode, type.FullName, null)); } } // search for the base object descriptors for (Type t = type; t != null; t = t.BaseType) { IUserDataDescriptor u; if (s_TypeRegistry.TryGetValue(t, out u)) { typeDescriptor = u; break; } else if (t.IsGenericType) { if (s_TypeRegistry.TryGetValue(t.GetGenericTypeDefinition(), out u)) { typeDescriptor = u; break; } } } if (typeDescriptor is IGeneratorUserDataDescriptor) { typeDescriptor = ((IGeneratorUserDataDescriptor)typeDescriptor).Generate(type); } // we should not search interfaces (for example, it's just for statics..), no need to look further if (!searchInterfaces) { return(typeDescriptor); } List <IUserDataDescriptor> descriptors = new List <IUserDataDescriptor>(); if (typeDescriptor != null) { descriptors.Add(typeDescriptor); } if (searchInterfaces) { foreach (Type interfaceType in type.GetInterfaces()) { IUserDataDescriptor interfaceDescriptor; if (s_TypeRegistry.TryGetValue(interfaceType, out interfaceDescriptor)) { if (interfaceDescriptor is IGeneratorUserDataDescriptor) { interfaceDescriptor = ((IGeneratorUserDataDescriptor)interfaceDescriptor).Generate(type); } if (interfaceDescriptor != null) { descriptors.Add(interfaceDescriptor); } } else if (interfaceType.IsGenericType) { if (s_TypeRegistry.TryGetValue(interfaceType.GetGenericTypeDefinition(), out interfaceDescriptor)) { if (interfaceDescriptor is IGeneratorUserDataDescriptor) { interfaceDescriptor = ((IGeneratorUserDataDescriptor)interfaceDescriptor).Generate(type); } if (interfaceDescriptor != null) { descriptors.Add(interfaceDescriptor); } } } } } if (descriptors.Count == 1) { return(descriptors[0]); } else if (descriptors.Count == 0) { return(null); } else { return(new CompositeUserDataDescriptor(descriptors, type)); } } }