/// <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))); }
/// <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)); }