C# 4.0 使用隐式conversion在运行时强制转换
我有以下代码,通过匹配属性名称将属性值从一个对象复制到另一个对象:C# 4.0 使用隐式conversion在运行时强制转换,c#-4.0,casting,C# 4.0,Casting,我有以下代码,通过匹配属性名称将属性值从一个对象复制到另一个对象: public static void CopyProperties(object source, object target,bool caseSenstive=true) { PropertyInfo[] targetProperties = target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
public static void CopyProperties(object source, object target,bool caseSenstive=true)
{
PropertyInfo[] targetProperties = target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] sourceProperties = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo tp in targetProperties)
{
var sourceProperty = sourceProperties.FirstOrDefault(p => p.Name == tp.Name);
if (sourceProperty == null && !caseSenstive)
{
sourceProperty = sourceProperties.FirstOrDefault(p => p.Name.ToUpper() == tp.Name.ToUpper());
}
// If source doesn't have this property, go for next one.
if(sourceProperty ==null)
{
continue;
}
// If target property is not writable then we can not set it;
// If source property is not readable then cannot check it's value
if (!tp.CanWrite || !sourceProperty.CanRead)
{
continue;
}
MethodInfo mget = sourceProperty.GetGetMethod(false);
MethodInfo mset = tp.GetSetMethod(false);
// Get and set methods have to be public
if (mget == null)
{
continue;
}
if (mset == null)
{
continue;
}
var sourcevalue = sourceProperty.GetValue(source, null);
tp.SetValue(target, sourcevalue, null);
}
}
当目标和源上的属性类型相同时,这种方法工作得很好。但是当需要强制转换时,代码就不起作用了
例如,我有以下对象:
class MyDateTime
{
public static implicit operator DateTime?(MyDateTime myDateTime)
{
return myDateTime.DateTime;
}
public static implicit operator DateTime(MyDateTime myDateTime)
{
if (myDateTime.DateTime.HasValue)
{
return myDateTime.DateTime.Value;
}
else
{
return System.DateTime.MinValue;
}
}
public static implicit operator MyDateTime(DateTime? dateTime)
{
return FromDateTime(dateTime);
}
public static implicit operator MyDateTime(DateTime dateTime)
{
return FromDateTime(dateTime);
}
}
如果我执行以下操作,则会调用隐式强制转换,并且一切正常:
MyDateTime x= DateTime.Now;
但是当我有两个对象,其中一个有DateTime,另一个有MyDateTime,并且我使用上面的代码将属性从一个对象复制到另一个对象时,它不会,并生成一个错误,说明DateTime不能转换为MyTimeDate
我如何解决这个问题?一个可怕的方法应该是将
动态
和反射混合使用:
private static T ConvertValue<T>(dynamic value)
{
return value; // This will perform conversion automatically
}
我们需要使用反射来调用具有正确类型参数的泛型方法,但是动态类型将为您使用正确的转换运算符
哦,还有最后一个请求:请不要在代码附近包含我的名字,无论它是在注释、提交日志中。啊。一种可怕的方法应该是把
动态的和反射结合起来:
private static T ConvertValue<T>(dynamic value)
{
return value; // This will perform conversion automatically
}
我们需要使用反射来调用具有正确类型参数的泛型方法,但是动态类型将为您使用正确的转换运算符
哦,还有最后一个请求:请不要在代码附近包含我的名字,无论它是在注释、提交日志中。啊。当然cource不在一般情况下,但也许它可以帮助你:当然cource不在一般情况下,但也许它可以帮助你:它是有效的,但为什么不把你的名字放在这段代码附近?@user654019:我在进行反射或动态键入时非常紧张。把两者放在一起对我来说太难了:)(更严重的是,这完全取决于上下文。如果失败了会发生什么?是在一个属性控制得很好的环境中吗?诸如此类的事情。)是的,我想用它来复制MVC应用程序中实体对象和模型对象之间的属性。所以属性是众所周知的。它很有效,但是为什么不把你的名字放在这个代码附近呢?@user654019:我在进行反射或动态键入时非常紧张。把两者放在一起对我来说太难了:)(更严重的是,这完全取决于上下文。如果失败了会发生什么?是在一个属性控制得很好的环境中吗?诸如此类的事情。)是的,我想用它来复制MVC应用程序中实体对象和模型对象之间的属性。因此,这些特性是众所周知的。