如何查找和调用特定类型的.Net TypeConverter?
我想实现一个通用的运行时类型转换函数,它使用.Net TypeConverters来进行转换。
有谁知道如何查找和调用特定类型的TypeConverter?
考虑一下这个C#示例:
// // Convert obj to the type specified by 'toType'. // object ConvertTo(object obj, Type toType) { if (TypeIsEqualOrDerivesFrom(obj.GetType(), toType)) <-- I know how to implement this. { // The type of obj is the same as the type specified by 'toType' or // the type of obj derives from the type specified by 'toType'. return obj; } if (TypeConverterExists(obj.GetType(), toType) <-- How do I implement this? { // There exists a type convertor that is capable of converting from // the type of obj to the type specified by 'toType'. return InvokeTypeConverter(obj, toType); <-- How do I implement this? } throw new TypeConversionFailedException(); }
TypeConverter converter = TypeDescriptor.GetConverter(sourceType); if(converter.CanConvertTo(destinationType)) { return converter.ConvertTo(obj, destinationType); } converter = TypeDescriptor.GetConverter(destinationType); if(converter.CanConvertFrom(sourceType)) { return converter.ConvertFrom(obj); }
您还可以查看Convert.ChangeType(obj, destinationType)
这是我们现有应用程序中使用的代码…不确定它是否理想,但它对我们很有用。
/// /// Attempts to convert a value using any customer TypeConverters applied to the member /// /// Object containing the value to be assigned /// Member to be assigned to /// converted to the appropriate type public static Object CustomTypeConversion ( object value, MemberInfo member ) { if ( value == null || value == DBNull.Value ) return value; if ( member == null ) throw new ArgumentNullException ( ); List converters = GetCustomTypeConverters ( member ); foreach ( TypeConverter c in converters ) { if ( c.CanConvertFrom ( value.GetType ( ) ) ) return c.ConvertFrom ( value ); } if ( member is PropertyInfo ) { PropertyInfo prop = member as PropertyInfo; return ConvertToNative( value , prop.PropertyType ); } return ConvertToNative ( value, member.MemberType.GetType ( ) ); } /// /// Extracts and instantiates any customer type converters assigned to a /// derivitive of the property /// /// Any class deriving from MemberInfo /// A list of customer type converters, empty if none found public static List GetCustomTypeConverters ( System.Reflection.MemberInfo member ) { List result = new List (); try { foreach ( TypeConverterAttribute a in member.GetCustomAttributes( typeof( TypeConverterAttribute ) , true ) ) { TypeConverter converter = Activator.CreateInstance( Type.GetType( a.ConverterTypeName ) ) as TypeConverter; if ( converter != null ) result.Add( converter ); } } catch { // Let it go, there were no custom converters } return result; } /// /// Attempts to cast the incoming database field to the property type /// /// Database value to cast /// Type to cast to /// The converted value, if conversion failed the original value will be returned public static object ConvertToNative ( object value , Type castTo ) { try { return Convert.ChangeType( value , castTo , System.Threading.Thread.CurrentThread.CurrentCulture ); } catch { return value; } }
只需调用CustomTypeConversion方法然后你就可以了……可能比你需要的多一点但是彻底不是犯罪(或者是它?)。
除了Marc的回答之外,您可能会认为ASP.NET的ViewState与您要求的function类似。 它试图找到最有效的转换。
可能值得一看这个页面 ,也可能是这个 。
我不知道它有多强大,但我有时会将此代码用于generics类型转换:
public T ConvertTo(object value) { return (T)Convert.ChangeType(value, typeof(T)); }
我不知道ChangeType方法是否使用TypeConverters …
上述就是C#学习教程:如何查找和调用特定类型的.Net TypeConverter?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1003707.html