我该如何处理使用反射阵列 [英] How do I deal with arrays using reflection

查看:104
本文介绍了我该如何处理使用反射阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一些验证代码。该代码将传递到Web服务的数据,并决定是否可以做的动作,或返回一个消息,他们已经错过了某些字段等来电。

I am writing some validation code. The code will take data passed into a web service and decide whether it can do the action, or return a message to the caller that they have missed out some fields etc.

我除了阵列它主要的工作。我打了一个[RequiredField]属性的属性,代表了所需的字段。因此,如果这是我的一些数据,

I have it mostly working except for arrays. I am marking up the properties with a [RequiredField] attribute to represent fields that are required. So if this is some of my data,

public enum EnumTest
{
    Value1,
    Value2
}

[DataContract]
public class DummyWebserviceData
{
    [DataMember]
    [RequiredField]
    public EnumTest[] EnumTest{ get; set; }

    [DataMember]
    [RequiredField]
    public DummyWebserviceData2[] ArrayOfData { get; set; }
}

[DataContract]
public class DummyWebserviceData2
{
    [DataMember]
    [RequiredField]
    public string FirstName { get; set;}

    [DataMember]
    [RequiredField]
    public string LastName { get; set;}

    [DataMember]
    public string Description { get; set;}
}



所以,我有什么工作吗?我有日期的确认和处理字符串。它采用递归深入下去所需要的数据的任何水平。

So what do I have working? I have validation of dates, and strings working. It uses recursion to go any level deep required on the data.

但是......那么,关于两个数组那里。第一是枚举的阵列。我想在该数组不为空那种情况下进行检查。

But ... so what about the two arrays there. The first is an array of enums. I want to check in that case that the array is not empty.

二是DummyWebserviceData2值的数组。我需要拉出每个值,并看看它单独。

The second is the array of DummyWebserviceData2 values. I need to pull each value out and have a look at it individually.

要简化我写它看起来像这样的代码,

To simplify the code I have written it looks something like this,

foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
{
    if (propertyInfo.PropertyType.IsArray)
    {
        // this craps out

        object[] array = (object[])propertyInfo.GetValue(data, new object[] { 0 });

    }
}



所以,在我看来,该第一件事是,我可以告诉它是一个数组。但我怎么那么能告诉多少个项目数组中?

So it seems to me that the first thing is that I can tell it is an array. But how then can I tell how many items are in the array?

推荐答案

在运行时,对象将被动态地从子类阵列数据类型(这个MSDN主题细节, ),因此你不需要反映到数组,你可以施放对象阵列,然后使用 Array.GetValue 实例方法:

At runtime the object will have been dynamically subclassed from the Array data type (this MSDN topic details that), therefore you don't need to reflect into the array, you can cast the object to Array, and then use the Array.GetValue instance method:

Array a = (Array)propertyInfo.GetValue(data);
for(int i = 0; i< a.Length; i++)
{
  object o = a.GetValue(i);
}

您也可以遍历数组,以及 - 从.net 2.0,因为起

You can also iterate over an array as well - since from .Net 2.0 onwards:

在.NET Framework 2.0版,Array类实现了System.Collections.Generic :: IList中,System.Collections.Generic :: ICollection的和System.Collections.Generic IEnumerable的::通用接口。

In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic::IList, System.Collections.Generic::ICollection, and System.Collections.Generic::IEnumerable generic interfaces.

您不需要知道 T ,因为从这些你可以得到一个IEnumerable;然后你可以使用CAST()操作,或者实际上只是在对象级别上工作。

You don't need to know the T, since from these you can get an IEnumerable; which you can then use a Cast() operation on, or indeed just work at the object level.

顺便说一句为什么你的代码是不工作的原因是因为你不能施放的MyType [] 的数组 [对象] ,因为 [对象] 不是基本类型的的MyType [] - 仅对象

Incidentally, the reason why your code isn't working is because you can't cast an array of MyType[] to object[] because object[] is not a base type of MyType[] - only object is.

这篇关于我该如何处理使用反射阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆