MongoDB.Bson.Serialization.TypeNameDiscriminator.GetActualType C# (CSharp) Метод

GetActualType() публичный статический Метод

Resolves a type name discriminator.
public static GetActualType ( string typeName ) : Type
typeName string The type name.
Результат System.Type
        public static Type GetActualType(string typeName)
        {
            var type = Type.GetType(typeName);
            if (type != null)
            {
                return type;
            }

            foreach (var assembly in __wellKnownAssemblies)
            {
                type = assembly.GetType(typeName);
                if (type != null)
                {
                    return type;
                }
            }

            string genericTypeDefinitionName;
            string[] typeArgumentNames;
            if (TryParseGenericTypeName(typeName, out genericTypeDefinitionName, out typeArgumentNames))
            {
                var genericTypeDefinition = GetActualType(genericTypeDefinitionName);
                if (genericTypeDefinition != null)
                {
                    var typeArguments = new List<Type>();
                    foreach (var typeArgumentName in typeArgumentNames)
                    {
                        var typeArgument = GetActualType(typeArgumentName);
                        if (typeArgument == null)
                        {
                            break;
                        }
                        typeArguments.Add(typeArgument);
                    }

                    if (typeArguments.Count == genericTypeDefinition.GetGenericArguments().Length)
                    {
                        return genericTypeDefinition.MakeGenericType(typeArguments.ToArray());
                    }
                }
            }

            return null;
        }

Usage Example

        /// <summary>
        /// Looks up the actual type of an object to be deserialized.
        /// </summary>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="discriminator">The discriminator.</param>
        /// <returns>The actual type of the object.</returns>
        public static Type LookupActualType(Type nominalType, BsonValue discriminator)
        {
            if (discriminator == null)
            {
                return(nominalType);
            }

            // note: EnsureKnownTypesAreRegistered handles its own locking so call from outside any lock
            EnsureKnownTypesAreRegistered(nominalType);

            __configLock.EnterReadLock();
            try
            {
                Type actualType = null;

                HashSet <Type> hashSet;
                if (__discriminators.TryGetValue(discriminator, out hashSet))
                {
                    foreach (var type in hashSet)
                    {
                        if (nominalType.IsAssignableFrom(type))
                        {
                            if (actualType == null)
                            {
                                actualType = type;
                            }
                            else
                            {
                                string message = string.Format("Ambiguous discriminator '{0}'.", discriminator);
                                throw new BsonSerializationException(message);
                            }
                        }
                    }
                }

                if (actualType == null && discriminator.IsString)
                {
                    actualType = TypeNameDiscriminator.GetActualType(discriminator.AsString); // see if it's a Type name
                }

                if (actualType == null)
                {
                    string message = string.Format("Unknown discriminator value '{0}'.", discriminator);
                    throw new BsonSerializationException(message);
                }

                if (!nominalType.IsAssignableFrom(actualType))
                {
                    string message = string.Format(
                        "Actual type {0} is not assignable to expected type {1}.",
                        actualType.FullName, nominalType.FullName);
                    throw new BsonSerializationException(message);
                }

                return(actualType);
            }
            finally
            {
                __configLock.ExitReadLock();
            }
        }