object ConvertFromString(string valueAsString, Type targetType) { // If it is a string or an Enum, convert directly, otherwise call the appropriate converter from the Core assembly: if (targetType == typeof(string)) { return(valueAsString); } else if (targetType.IsEnum) { return(Enum.Parse(targetType, valueAsString)); } else { // Get a reference to the "TypeFromStringConverters" class in the Core assembly: Type typeFromStringConverter; Assembly coreAssembly; ReflectionInUserAssembliesHelper.TryGetTypeInCoreAssembly("DotNetForHtml5.Core", null, "TypeFromStringConverters", out typeFromStringConverter, out coreAssembly); // Call the "ConvertFromInvariantString" method: MethodInfo convertFromInvariantStringMethod = typeFromStringConverter.GetMethod("ConvertFromInvariantString"); object convertedValue = convertFromInvariantStringMethod.Invoke(null, new object[] { targetType, valueAsString }); return(convertedValue); } }
bool IsItPossibleToConvertFromString(Type typeToWhichWeWouldLikeToConvert) { // If it is a string or an Enum, we can convert from string, otherwise, we need to look for a converter in the Core assembly: if (typeToWhichWeWouldLikeToConvert == typeof(string) || typeToWhichWeWouldLikeToConvert.IsEnum) { return(true); } else { // Get a reference to the "TypeFromStringConverters" class in the Core assembly: Assembly coreAssembly; Type typeFromStringConverter; ReflectionInUserAssembliesHelper.TryGetTypeInCoreAssembly("DotNetForHtml5.Core", null, "TypeFromStringConverters", out typeFromStringConverter, out coreAssembly); // Call the "CanTypeBeConverted" method: MethodInfo canTypeBeConvertedMethod = typeFromStringConverter.GetMethod("CanTypeBeConverted"); bool canTypeBeConverted = (bool)canTypeBeConvertedMethod.Invoke(null, new object[] { typeToWhichWeWouldLikeToConvert }); return(canTypeBeConverted); } }