Ash*_*vis 10 .net c# type-conversion
我想实现一个通用的运行时类型转换函数,它使用.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();
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ell 35
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);
}
Run Code Online (Sandbox Code Playgroud)
你也可以看看 Convert.ChangeType(obj, destinationType)
Ste*_*nan 10
这是我们现有应用程序中使用的代码...不确定它是否理想,但它对我们很有用.
/// <summary>
/// Attempts to convert a value using any customer TypeConverters applied to the member
/// </summary>
/// <param name="value">Object containing the value to be assigned</param>
/// <param name="member">Member to be assigned to</param>
/// <returns><paramref name="value"/> converted to the appropriate type</returns>
public static Object CustomTypeConversion ( object value, MemberInfo member )
{
if ( value == null || value == DBNull.Value )
return value;
if ( member == null )
throw new ArgumentNullException ( );
List<TypeConverter> 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 ( ) );
}
/// <summary>
/// Extracts and instantiates any customer type converters assigned to a
/// derivitive of the <see cref="System.Reflection.MemberInfo"/> property
/// </summary>
/// <param name="member">Any class deriving from MemberInfo</param>
/// <returns>A list of customer type converters, empty if none found</returns>
public static List<TypeConverter> GetCustomTypeConverters ( System.Reflection.MemberInfo member )
{
List<TypeConverter> result = new List<TypeConverter>();
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;
}
/// <summary>
/// Attempts to cast the incoming database field to the property type
/// </summary>
/// <param name="value">Database value to cast</param>
/// <param name="castTo">Type to cast to</param>
/// <returns>The converted value, if conversion failed the original value will be returned</returns>
public static object ConvertToNative ( object value , Type castTo )
{
try
{
return Convert.ChangeType( value , castTo , System.Threading.Thread.CurrentThread.CurrentCulture );
}
catch
{
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
只需调用CustomTypeConversion方法和远离你去...大概一点比你更需要但作为彻底的不是罪(是吗?).
| 归档时间: |
|
| 查看次数: |
11474 次 |
| 最近记录: |