从数组中获取数组 [英] Get an array from an array

查看:66
本文介绍了从数组中获取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个有一些属性和数组的数组。我需要从数组中找到数组。



Hi,

I have an array which has some properties and an array. I need to find the array from the array.

public class User
{
    public MainArray[] mainArray { get; set; }
}

public class MainArray
{
    public bool IsFlat { get; set; }
    public bool IsStandard { get; set; }
    public bool IsCustom { get; set; }
    public string CustomName { get; set; }
    public SubArray[] subArray { get; set; }
}

public class SubArray
{
    public string Amount { get; set; }
    public string Fee { get; set; }
}





我需要使用反射从mainArray中找到subArray。有没有办法做到这一点?我有使用反射的mainArray。有没有办法检查mainArray是否包含任何数组?



I need to find subArray from mainArray using reflection. Is there any way to do that? I have got my mainArray using reflection. Is there any way to check if mainArray contains any array or not?

推荐答案

嗨sanket.raj8,



我想我明白你想要什么。但我不知道你可能被困在哪里。对你来说这似乎是一个非常可管理的任务,因为你提到你已经发现你的 mainArray 属性是一个数组。



无论如何 - 所以你有这样的方法/功能:



Hi sanket.raj8,

I think I understand what you want. But I don't see where you possibly have been stuck. It seems quite a managable task for you because you mentioned you already found out that your mainArray property is an Array.

Anyway - so you have a method/function like this:

public static bool HasAtLeastOneArrayProperty(object obj)
{
   PropertyInfo[] aPropertyInfos = obj.GetType().GetProperties();
   return aPropertyInfos.Any(p => p.PropertyType.IsArray);
}





现在,如果您想要查看完整的对象图(请注意参考圈),应该能够对每个属性及其子属性等进行此方法的递归调用。 (或者你打算问你是否可以避免这种情况?)



听起来像是一个不寻常的要求 - 我很抱你有充分的理由 - 只是想知道为什么要这样做?你可以开导我吗?



如果你有任何进一步/其他问题随时问我 - 也许我可以反思我的一些记忆并帮助你。< br $>




亲切的问候



Johannes



Now if you want to go down the complete object graph (be aware of "reference circles"), you should be able to just do recursive calls to this methods for every property and their subproperties and so on. (Or was your intention to ask whether you can avoid that?)

Sounds like a "unusual" requirement - I'm shure you have good reasons for it - just wondering why want to do this? Can you enlighten me?

If you have any further/other problems feel free to ask me - Maybe I can REFLECT on some of my memories and help you out.


Kind regards

Johannes


这篇关于从数组中获取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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