Click here to Skip to main content
15,908,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

C#
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; }
}


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?
Posted

1 solution

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:

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


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
 
Share this answer
 
Comments
sanket.raj8 19-Jul-13 7:53am    
There is no such requirement. I am using the same. But I want to get the subArray using reflection. Can you provide me with some code examples.
johannesnestler 19-Jul-13 9:06am    
I meant the requirment to find nested sub-Arrays with reflection - but don't mind. To your Problem: So you want to know if any property in the object graph is of type Array, or you want to get a reference to (one, or more) of the sub-Array INSTANCES? Your question was (in my Interpretation) just to find out if there is an array property (possibly for special handling of such typed objects - you didn't tell). So you mean a code example for recursion? But If you want to find a reference to one/any array I can't give you good example code without knowing what you realy want to do (how you want them?, or just the PropertyInfo?, you want all or Limit search-depth? how to handle circular references and so on).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900