private bool IsSpecificTypeSpecialCollection(Type type)
            {
                if (type.IsGenericTypeEx())
                {
                    var fullName = type.GetSafeFullName();

                    if (fullName.StartsWith("System.Collections.ObjectModel.ObservableCollection") ||
                        fullName.StartsWith("System.Collections.Generic."))
                    {
                        // Note: this is not a bug, we need to add type to the collection
                        return true;
                    }
                }

                return false;
            }
        /// <summary>
        /// Determines whether the type should be handled.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="serializerTypeInfo">The serializer type info.</param>
        /// <returns><c>true</c> if the type should be handled; otherwise, <c>false</c>.</returns>
        protected virtual bool ShouldTypeBeIgnored(Type type, XmlSerializerTypeInfo serializerTypeInfo)
        {
            if (type == null)
            {
                return true;
            }

            // Never include generic type definitions, otherwise we will get this:
            // Error while getting known types for Type 'Catel.Test.Data.PropertyDataManagerFacts+SupportsGenericClasses+GenericClass`1[T]'. The type must not be an open or partial generic class.
            if (type.IsGenericTypeDefinitionEx())
            {
                return true;
            }

            // Note, although resharper says this isn't possible, it might be
            var fullName = type.GetSafeFullName();
            if (string.IsNullOrWhiteSpace(fullName))
            {
                serializerTypeInfo.AddTypeAsHandled(type);
                return true;
            }

            // Ignore non-generic .NET
            if (!type.IsGenericTypeEx() && fullName.StartsWith("System."))
            {
                // Log.Debug("Non-generic .NET system type, can be ignored");
                serializerTypeInfo.AddTypeAsHandled(type);
                return true;
            }

            if (type.IsCOMObjectEx())
            {
                serializerTypeInfo.AddTypeAsHandled(type);
                return true;
            }

            return serializerTypeInfo.ContainsKnownType(type) ||
                   serializerTypeInfo.IsTypeAlreadyHandled(type) ||
                   serializerTypeInfo.IsCollectionAlreadyHandled(type);
        }
            /// <summary>
            /// Adds the type to the list of known types.
            /// </summary>
            /// <param name="type">The type.</param>
            public bool AddKnownType(Type type)
            {
                if (!IsTypeSerializable(type))
                {
                    return false;
                }

                _knownTypes.Add(type);

                if (IsSpecialCollectionType(type))
                {
                    _specialCollectionTypes.Add(type);

                    if (type.IsGenericTypeEx())
                    {
                        _specialGenericCollectionTypes.Add(type);
                    }
                }

                return true;
            }
        /// <summary>
        /// Determines whether the type should be handled.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="serializerTypeInfo">The serializer type info.</param>
        /// <returns><c>true</c> if the type should be handled; otherwise, <c>false</c>.</returns>
        protected virtual bool ShouldTypeBeIgnored(Type type, XmlSerializerTypeInfo serializerTypeInfo)
        {
            if (type == null)
            {
                return true;
            }

            // Note, although resharper says this isn't possible, it might be
            if (type.FullName == null)
            {
                serializerTypeInfo.AddTypeAsHandled(type);
                return true;
            }

            // Ignore non-generic .NET
            if (!type.IsGenericTypeEx() && type.GetSafeFullName().StartsWith("System."))
            {
                // Log.Debug("Non-generic .NET system type, can be ignored");
                serializerTypeInfo.AddTypeAsHandled(type);
                return true;
            }

            return serializerTypeInfo.ContainsKnownType(type) || serializerTypeInfo.IsTypeAlreadyHandled(type);
        }
示例#5
0
        /// <summary>
        /// Determines whether the specified service type is registered.
        /// </summary>
        /// <param name="serviceType">The type of the service.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <returns><c>true</c> if the specified service type is registered; otherwise, <c>false</c>.</returns>
        /// <remarks>Note that the actual implementation lays in the hands of the IoC technique being used.</remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType"/> is <c>null</c>.</exception>
        public bool IsTypeRegistered(Type serviceType, object tag = null)
        {
            Argument.IsNotNull("serviceType", serviceType);

            var serviceInfo = new ServiceInfo(serviceType, tag);

            lock (this)
            {
                if (_registeredInstances.ContainsKey(serviceInfo))
                {
                    return true;
                }

                if (_registeredTypes.ContainsKey(serviceInfo))
                {
                    return true;
                }

                // CTL-161, support generic types
                if (serviceType.IsGenericTypeEx())
                {
                    var genericArguments = serviceType.GetGenericArgumentsEx().ToList();
                    var hasRealGenericArguments = (from genericArgument in genericArguments
                                                   where !string.IsNullOrEmpty(genericArgument.FullName)
                                                   select genericArgument).Any();
                    if (hasRealGenericArguments)
                    {
                        var genericType = serviceType.GetGenericTypeDefinitionEx();
                        var isOpenGenericTypeRegistered = IsTypeRegistered(genericType, tag);
                        if (isOpenGenericTypeRegistered)
                        {
                            Log.Debug("An open generic type '{0}' is registered, registering new closed generic type '{1}' based on the open registration", genericType.GetSafeFullName(), serviceType.GetSafeFullName());

                            var registrationInfo = GetRegistrationInfo(genericType, tag);
                            var finalType = registrationInfo.ImplementingType.MakeGenericType(genericArguments.ToArray());

                            RegisterType(serviceType, finalType, tag, registrationInfo.RegistrationType);

                            return true;
                        }
                    }
                }

                // CTL-271 Support generic lists (array specific type)
                // TODO: Can register, 

                // Last resort
                var eventArgs = new MissingTypeEventArgs(serviceType);
                MissingType.SafeInvoke(this, eventArgs);

                if (eventArgs.ImplementingInstance != null)
                {
                    Log.Debug("Late registering type '{0}' to instance of type '{1}' via MissingTypeEventArgs.ImplementingInstance", serviceType.FullName, eventArgs.ImplementingInstance.GetType().FullName);

                    RegisterInstance(serviceType, eventArgs.ImplementingInstance, eventArgs.Tag, this);
                    return true;
                }

                if (eventArgs.ImplementingType != null)
                {
                    Log.Debug("Late registering type '{0}' to type '{1}' via MissingTypeEventArgs.ImplementingType", serviceType.FullName, eventArgs.ImplementingType.FullName);

                    RegisterType(serviceType, eventArgs.ImplementingType, eventArgs.Tag, eventArgs.RegistrationType, true, this, null);
                    return true;
                }
            }

            return false;
        }
示例#6
0
 protected static bool IsDataServiceCollection(Type type)
 {
     while (type != null)
     {
         if (type.IsGenericTypeEx() && WebUtil.IsDataServiceCollectionType(type.GetGenericTypeDefinition()))
         {
             return true;
         }
         type = type.BaseType;
     }
     return false;
 }
示例#7
0
 private static bool IsConstructedGeneric(Type type, Type genericTypeDefinition)
 {
     return ((type.IsGenericTypeEx() && (type.GetGenericTypeDefinition() == genericTypeDefinition)) && !type.ContainsGenericParametersEx());
 }
示例#8
0
 internal static bool CanAssignNull(Type type)
 {
     return (!type.IsValueTypeEx() || (type.IsGenericTypeEx() && (type.GetGenericTypeDefinition() == typeof(Nullable<>))));
 }