コード例 #1
0
        /// <summary>
        /// Resolves a value to an instance of <see cref="Lazy{T}"/>, where the lazily-loaded
        /// object is an instance of <see cref="DataObject"/>.
        /// </summary>
        private static Object ResolveLazilyLoadedDataObject <T>(String value) where T : DataObject
        {
            var registry  = DataObjectRegistries.Get <T>();
            var reference = DataObjectRegistries.ResolveReference(value);

            return(new Lazy <T>(() => registry.GetObject(reference)));
        }
コード例 #2
0
        /// <summary>
        /// Translates the specified local identifier in this translation table
        /// into the corresponding local identifier within the current process.
        /// </summary>
        /// <typeparam name="T">The type of data object being evaluated.</typeparam>
        /// <param name="localID">The local identifier to translate.</param>
        /// <returns>The translated local identifier.</returns>
        public UInt16 Translate <T>(UInt16 localID) where T : DataObject
        {
            Guid globalID;

            if (!TryGetValue(localID, out globalID))
            {
                return(0);
            }

            var registry = DataObjectRegistries.Get <T>();
            var obj      = registry.GetObject(globalID);

            return((obj == null) ? (ushort)0 : obj.LocalID);
        }
コード例 #3
0
        /// <summary>
        /// Gets a value indicating whether the specified local identifier in this translation
        /// table translates to an object that is valid within the current process.
        /// </summary>
        /// <typeparam name="T">The type of data object being evaluated.</typeparam>
        /// <param name="localID">The local identifier of the entry within the translation table to validate.</param>
        /// <returns><see langword="true"/> if the entry is valid; otherwise, <see langword="false"/>.</returns>
        public Boolean IsValid <T>(UInt16 localID) where T : DataObject
        {
            Guid globalID;

            if (!TryGetValue(localID, out globalID))
            {
                return(false);
            }

            var registry = DataObjectRegistries.Get <T>();
            var obj      = registry.GetObject(globalID);

            return(obj != null);
        }
コード例 #4
0
        /// <summary>
        /// Creates an object from the specified value string.
        /// </summary>
        /// <param name="value">The value string from which to create the object.</param>
        /// <param name="type">The type of object to create.</param>
        /// <param name="provider">An object that supplies culture-specific formatting information.</param>
        /// <param name="ignoreCase">A value indicating whether to ignore casing whenever relevant (particularly, when converting enum values).</param>
        /// <returns>The object that was created.</returns>
        public static Object FromString(String value, Type type, IFormatProvider provider, Boolean ignoreCase)
        {
            // Ensure that the static constructor for this class has been run, as it
            // might need to register custom resolvers.
            RuntimeHelpers.RunClassConstructor(type.TypeHandle);

            // Handle some special cases...
            if (type == typeof(Char?))
            {
                if (value == null)
                {
                    return(null);
                }
                if (value.Length > 1)
                {
                    throw new FormatException();
                }
                return(value[0]);
            }
            if (type == typeof(Char))
            {
                if (value.Length > 1)
                {
                    throw new FormatException();
                }
                return(value[0]);
            }
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                if (value == null)
                {
                    return(null);
                }
                type = type.GetGenericArguments()[0];
            }

            // Handle custom resolvers.
            CustomObjectResolver customResolver;

            if (registeredCustomResolvers.TryGetValue(type, out customResolver))
            {
                return(customResolver(value, provider));
            }

            // Handle enumerations.
            if (type.IsEnum)
            {
                return(ParseEnum(type, value, ignoreCase));
            }

            // Handle lazy loaders.
            Type dataObjectType;

            if (IsLazilyLoadedDataObjectType(type, out dataObjectType))
            {
                return(miResolveLazilyLoadedDataObject.MakeGenericMethod(dataObjectType)
                       .Invoke(null, new object[] { value }));
            }

            // Handle object references.
            if (type.Equals(typeof(Guid)))
            {
                return(DataObjectRegistries.ResolveReference(value).Value);
            }
            if (type.Equals(typeof(ResolvedDataObjectReference)))
            {
                if (value == "(none)" || String.IsNullOrEmpty(value))
                {
                    return(new ResolvedDataObjectReference());
                }
                return(DataObjectRegistries.ResolveReference(value));
            }

            // Handle the general case by calling the type's Parse() method, if it exists,
            // and doing a type conversion if it does not.
            Object result;

            if (AttemptCultureAwareParse(value, type, provider, out result))
            {
                return(result);
            }
            if (AttemptCultureIgnorantParse(value, type, out result))
            {
                return(result);
            }
            return(Convert.ChangeType(value, type, provider));
        }