第一次尝试将属性表单对象复制到另一个对象的扩展方法
我正在尝试编写一种扩展方法,只要属性名称和类型完全匹配,就可以使用该扩展方法将值从一个对象属性复制到另一个不同类型的对象。
这就是我所拥有的:
public static T CopyFrom<T>(this T toObject, object fromObject)
{
var fromObjectType = fromObject.GetType();
var fromProperties = fromObjectType.GetProperties();
foreach (PropertyInfo toProperty in toObject.GetType().GetProperties())
{
PropertyInfo fromProperty = fromObjectType.GetProperty(toProperty.Name);
if (fromProperty != null) // match found
{
// check types
var fromType = fromProperty.PropertyType.UnderlyingSystemType;
var toType = toProperty.PropertyType.UnderlyingSystemType;
if (toType.IsAssignableFrom(fromType))
{
toProperty.SetValue(toObject, fromProperty.GetValue(fromObject, null), null);
}
}
}
return toObject;
}
这对于非装箱类型非常有用,但是当我调用时 Nullable
返回 false,
toType.IsAssignableFrom(fromType)
因为它的类型是 Nullable
而不是底层类型T
。
我在此处读到GetType()
应该取消 Nullable
的装箱,以便它返回 T
但如果我在 PropertyInfo.PropertyType
上调用它,我会得到 ReflectedMemberInfo
> 而不是我正在寻找的类型 T
。
我想我在这里遗漏了一些明显的东西,所以我想我会把它开放给SO以获得一些建议。
有人有什么想法吗?
更新:这是任何搜索此内容的人的最终方法。
public static T CopyFrom<T>(this T toObject, object fromObject)
{
var fromObjectType = fromObject.GetType();
foreach (PropertyInfo toProperty in toObject.GetType().GetProperties())
{
PropertyInfo fromProperty = fromObjectType.GetProperty(toProperty.Name);
if (fromProperty != null) // match found
{
// check types
var fromType = Nullable.GetUnderlyingType(fromProperty.PropertyType) ?? fromProperty.PropertyType;
var toType = Nullable.GetUnderlyingType(toProperty.PropertyType) ?? toProperty.PropertyType;
if (toType.IsAssignableFrom(fromType))
{
toProperty.SetValue(toObject, fromProperty.GetValue(fromObject, null), null);
}
}
}
return toObject;
}
I'm trying to write an extension method that I can use to copy values from one object property to another object of a different type, as long as the property names and types match exactly.
This is what I have:
public static T CopyFrom<T>(this T toObject, object fromObject)
{
var fromObjectType = fromObject.GetType();
var fromProperties = fromObjectType.GetProperties();
foreach (PropertyInfo toProperty in toObject.GetType().GetProperties())
{
PropertyInfo fromProperty = fromObjectType.GetProperty(toProperty.Name);
if (fromProperty != null) // match found
{
// check types
var fromType = fromProperty.PropertyType.UnderlyingSystemType;
var toType = toProperty.PropertyType.UnderlyingSystemType;
if (toType.IsAssignableFrom(fromType))
{
toProperty.SetValue(toObject, fromProperty.GetValue(fromObject, null), null);
}
}
}
return toObject;
}
This is working great for non boxed types, but Nullable<T>
returns false when I call
toType.IsAssignableFrom(fromType)
because its type is Nullable<T>
and is not the underlying type T
.
I read here that GetType()
should unbox the Nullable<T>
so it returns T
but if I call that on PropertyInfo.PropertyType
I get ReflectedMemberInfo
and not the type T
im looking for.
I think I'm missing something obvious here, so I thought I would throw it open to SO to get some advice.
Anyone have any ideas?
UPDATE: Here is the final method for anyone searching for this.
public static T CopyFrom<T>(this T toObject, object fromObject)
{
var fromObjectType = fromObject.GetType();
foreach (PropertyInfo toProperty in toObject.GetType().GetProperties())
{
PropertyInfo fromProperty = fromObjectType.GetProperty(toProperty.Name);
if (fromProperty != null) // match found
{
// check types
var fromType = Nullable.GetUnderlyingType(fromProperty.PropertyType) ?? fromProperty.PropertyType;
var toType = Nullable.GetUnderlyingType(toProperty.PropertyType) ?? toProperty.PropertyType;
if (toType.IsAssignableFrom(fromType))
{
toProperty.SetValue(toObject, fromProperty.GetValue(fromObject, null), null);
}
}
}
return toObject;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找
Nullable.GetUnderlyingType
。例如:
You're looking for
Nullable.GetUnderlyingType
.For example: