通过不同属性类型的反射设置对象的属性

发布于 2024-07-21 00:22:08 字数 1873 浏览 3 评论 0原文

我正在使用反射来填充对象的属性。

这些属性有不同的类型:String、Nullable(double) 和 Nullable(long)(不知道如何转义这里的尖括号...)。 这些属性的值来自(字符串、对象)对的字典。

因此,例如我的类具有以下属性:(

string Description { get; set; } 
Nullable<long> Id { get; set; }
Nullable<double> MaxPower { get; set; }

实际上有大约十几个属性)并且字典将具有诸如 <“Description”,“A description”>,<“Id”,123456> 之类的条目, <“MaxPower”,20000>

现在我使用类似以下内容来设置值:

foreach (PropertyInfo info in this.GetType().GetProperties())
{
    if (info.CanRead)
    {
         object thisPropertyValue = dictionary[info.Name];

         if (thisPropertyValue != null && info.CanWrite)
         {
             Type propertyType = info.PropertyType;

             if (propertyType == typeof(String))
             {
                 info.SetValue(this, Convert.ToString(thisPropertyValue), null);
             }
             else if (propertyType == typeof(Nullable<double>))
             {
                 info.SetValue(this, Convert.ToDouble(thisPropertyValue), null);
             }
             else if (propertyType == typeof(Nullable<long>))
             {
                 info.SetValue(this, Convert.ToInt64(thisPropertyValue), null);
             }
             else
             {
                 throw new ApplicationException("Unexpected property type");
             }
         }
     }
}

所以问题是:在分配值之前我真的必须检查每个属性的类型吗? 我可以执行类似强制转换的操作,以便将属性值分配给相应属性的类型吗?

理想情况下,我希望能够执行以下操作(我天真地认为可能有效):

         if (thisPropertyValue != null && info.CanWrite)
         {
             Type propertyType = info.PropertyType;

             if (propertyType == typeof(String))
             {
                 info.SetValue(this, (propertyType)thisPropertyValue, null);
             }
        }

谢谢, 斯特凡诺

I am using reflection to populate the properties of an object.

These properties have different types: String, Nullable(double) and Nullable(long) (don't know how to escape the angle brackets here ...). The values for these properties are coming from a dictionary of (string, object) pairs.

So, for example my class has the following properties:

string Description { get; set; } 
Nullable<long> Id { get; set; }
Nullable<double> MaxPower { get; set; }

(in reality there are about a dozen properties) and the dictionary will have entries like <"Description", "A description">, <"Id", 123456>, <"MaxPower", 20000>

Now I am using something like the following to set the values:

foreach (PropertyInfo info in this.GetType().GetProperties())
{
    if (info.CanRead)
    {
         object thisPropertyValue = dictionary[info.Name];

         if (thisPropertyValue != null && info.CanWrite)
         {
             Type propertyType = info.PropertyType;

             if (propertyType == typeof(String))
             {
                 info.SetValue(this, Convert.ToString(thisPropertyValue), null);
             }
             else if (propertyType == typeof(Nullable<double>))
             {
                 info.SetValue(this, Convert.ToDouble(thisPropertyValue), null);
             }
             else if (propertyType == typeof(Nullable<long>))
             {
                 info.SetValue(this, Convert.ToInt64(thisPropertyValue), null);
             }
             else
             {
                 throw new ApplicationException("Unexpected property type");
             }
         }
     }
}

So the question is: do I really have to check the type of each property before assigning the value? Is there anything like a cast that I can perform so that the property value is assigned the type of the corresponding property?

Ideally I would like to be able to do something like the following (which I naively thought might have worked):

         if (thisPropertyValue != null && info.CanWrite)
         {
             Type propertyType = info.PropertyType;

             if (propertyType == typeof(String))
             {
                 info.SetValue(this, (propertyType)thisPropertyValue, null);
             }
        }

Thanks,
Stefano

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不爱素颜 2024-07-28 00:22:08

如果值的类型已经正确,则不需要:您无需执行任何操作。 如果它们可能不正确(int vs float等),一个简单的方法可能是:(

编辑针对空值进行调整)

Type propertyType = info.PropertyType;
if (thisPropertyValue != null)
{
    Type underlyingType = Nullable.GetUnderlyingType(propertyType);
    thisPropertyValue = Convert.ChangeType(
        thisPropertyValue, underlyingType ?? propertyType);
}
info.SetValue(this, thisPropertyValue, null);

If the values are already of the correct type, then no: you don't have to do anything. If they might not be right (int vs float, etc), the a simple approach might be:

(edit adjusted for nulls)

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