第一次尝试将属性表单对象复制到另一个对象的扩展方法

发布于 2024-09-01 01:22:25 字数 2475 浏览 7 评论 0原文

我正在尝试编写一种扩展方法,只要属性名称和类型完全匹配,就可以使用该扩展方法将值从一个对象属性复制到另一个不同类型的对象。

这就是我所拥有的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2024-09-08 01:22:25

您正在寻找 Nullable.GetUnderlyingType

例如:

toType = Nullable.GetUnderlyingType(toType) ?? toType;

You're looking for Nullable.GetUnderlyingType.

For example:

toType = Nullable.GetUnderlyingType(toType) ?? toType;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文