private Dictionary <Type, ConstructorInfo> CreateConstructorDictionary() { Dictionary <Type, ConstructorInfo> dict = new Dictionary <Type, ConstructorInfo>(); ConstructorInfo[] constructors = _enumType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic); foreach (ConstructorInfo constructor in constructors) { ParameterInfo[] constructorParams = constructor.GetParameters(); if (constructorParams.Length != 1) { continue; } Type paramType = constructorParams[0].ParameterType; if (ObjectEnum.IsSupportedSerializationType(paramType)) { dict.Add(paramType, constructor); } } if (dict.Count == 0) { throw new NotSupportedException($"The {nameof(ObjectEnum)} type '{_enumType.FullName}' doesn't have any " + $"suitable constructors for deserialization."); } return(dict); }
/// <summary> /// Creates a new instance of the <see cref="ObjectEnum"/>-type this factory is for. /// If there is no suitable constructor for the type <paramref name="valueType"/>, a /// <see cref="NotSupportedException"/> will be thrown. /// <para> /// Use this method if the type of <paramref name="value"/> is already known /// (and you're sure about it). /// </para> /// </summary> /// <param name="value">The value used for instantiating the <see cref="ObjectEnum"/>.</param> /// <param name="valueType">The <see cref="Type"/> of <paramref name="value"/>.</param> public ObjectEnum Create(object value, Type valueType) { if (_constructorCache.TryGetValue(valueType, out ConstructorInfo constructor)) { return((ObjectEnum)constructor.Invoke(new[] { value })); } if (ObjectEnum.IsSupportedSerializationType(valueType)) { throw new NotSupportedException($"The object enum '{_enumType.FullName}' doesn't have a constructor which takes a single " + $"argument of type '{valueType.FullName}'."); } else { throw new NotSupportedException($"The type '{valueType}' isn't supported for serialization within {nameof(ObjectEnum)}."); } }