public static object ToType(object source, Type targetType) { ITypeConverter typeConverter = TypeConverterRegistry.GetConverter(targetType); if (typeConverter == null) { return(Convert.ChangeType(source, targetType)); } return(typeConverter.ConvertFrom(source)); }
/// <summary> /// Populates <see cref="TypeConverterRegistry"/> using values specified in /// the <c>typeConverters</c> config section. /// </summary> /// <param name="parent"> /// The configuration settings in a corresponding parent /// configuration section. /// </param> /// <param name="configContext"> /// The configuration context when called from the ASP.NET /// configuration system. Otherwise, this parameter is reserved and /// is <see langword="null"/>. /// </param> /// <param name="section"> /// The <see cref="System.Xml.XmlNode"/> for the section. /// </param> /// <returns> /// This method always returns <see langword="null"/>, because the /// <see cref="TypeConverterRegistry"/> is populated as a side-effect of /// its execution and thus there is no need to return anything. /// </returns> public object Create(object parent, object configContext, XmlNode section) { if (section != null) { XmlNodeList converters = ((XmlElement)section).GetElementsByTagName(ConverterElementName); foreach (XmlElement aliasElement in converters) { string forType = GetRequiredAttributeValue(aliasElement, ForAttributeName, section); string converterType = GetRequiredAttributeValue(aliasElement, TypeAttributeName, section); TypeConverterRegistry.RegisterConverter(forType, converterType); } } return(null); }
/// <summary> /// Register the given custom <see cref="System.ComponentModel.TypeConverter"/> /// for all properties of the given <see cref="System.Type"/>. /// </summary> /// <param name="requiredType"> /// The <see cref="System.Type"/> of property. /// </param> /// <param name="typeConverter"> /// The <see cref="System.ComponentModel.TypeConverter"/> to register. /// </param> public virtual void RegisterCustomConverter( Type requiredType, TypeConverter typeConverter) { TypeConverterRegistry.RegisterConverter(requiredType, typeConverter); }
private void RegisterTypeConverters() { TypeConverterRegistry.RegisterConverter(typeof(WriteConcern), new WriteConcernTypeConverter()); }
private void RegisterTypeConverters() { TypeConverterRegistry.RegisterConverter(typeof(WriteConcern), new WriteConcernTypeConverter()); TypeConverterRegistry.RegisterConverter(typeof(ReadPreference), new ReadPreferenceTypeConverter()); TypeConverterRegistry.RegisterConverter(typeof(MongoServerAddress), new MongoServerAddressTypeConverter()); }