Read elements of an array of objects using C# reflection
Solution 1
I think you're for looking for theType.IsArray
property (indicates whether a type is an array) and the Type.GetElementType
method (gets, amongst other things, the element-type of an array). Do note that the element-type of the array isn't necessarily the same as the specific run-time type of each of the elements of the array; polymorphism could come into play.
Of course, to simply get the values, you can rely on array-covariance: cast the value of the property (I assume you're using PropertyInfo.GetValue
somewhere) to an object[]
and foreach that as usual.
EDIT:
Your update is quite confusing. If you already have an object that you think might be an array; you might as well do:
object obj = ...
object[] array = obj as object[];
if(array != null)
{
foreach(object item in array)
{
...
}
}
It really appears like you're confusing meta-data with real-data here. You can't actually enumerate any array if all you have is a System.Type
that represents an array-type.
EDIT:
I think I've finally understood what you want to do. Just use Type.GetElementType
to get the array's element-type, and then get that type's properties (recursively?). You're probably going to change your design a bit to able to get the XML output you want. XML represents a hierarchy; but your method's return-type is just a Dictionary<string, string>
, which is a flat data-structure.
Solution 2
You can check the PropertyInfo.PropertyType Property. Like this:
if (propertyInfo.PropertyType.IsArray)
{
// Type is an array
}
Update: If you just want to get the properties of the array, you can use GetElementType like this:
...
//How to check for an array??? and add it to objProp dictionary
if (property.PropertyType.IsArray)
{
//??? how to read all the elements of an array
objProp = GetProperties(property.PropertyType.GetElementType());
}
...
Sri Reddy
Updated on June 26, 2022Comments
-
Sri Reddy almost 2 years
How can I get the list of elements and datatypes of an array of an object in c# with reflections?
Scenario: I have a method with an array parameter in my web service (asmx). Using reflection I am reading the method parameters and loop through the properties. Here is my code:
Example: I have a webservice http://localhost/services/myservice.asmx. It has a method
string GetServiceData(context mycontext)
. context class has following properties- string name - Account[] accounts
Account in turn has
- string acct_name - string acct_address
I need to read the service dynamically to generate the following output
<GetServiceData> <mycontext> <name>string</name> <accounts> <acct_name>string</acct_name> <acct_address>string</acct_address> </accounts> </mycontext> </GetServiceData>
To do so I am dynamically reading the MethodInfo and get all the parameters. Then I loop through the parameters to get the list of all the properties and the datatypes. If there is an array element in the properties then I need to get all the elements of that array element.
Solution (Thanks to Ani)
foreach (MethodInfo method in methods) { sb.Append("<" + method.Name + ">"); ParametetInfo parameters = method.GetParameters(); foreach(ParameterInfo parameter in parameters) { sb.Append("<" + parameter.Name + ">"); if (IsCustomType(parameter.ParameterType)) { sb.Append(GetProperties(parameter.ParameterType)); } else { sb.Append(parameter.ParameterType.Name); } sb.Append("</" + parameter.Name + ">"); } sb.Append("</" + sMethodName + ">"); }
GetProperties() method reads the type and actually loop through each property and add it to string object. In this method I want to check if the property is an array then get all the elements and type for it.
public static string GetProperties(Type type) { StringBuilder sb = new StringBuilder(); foreach(PropertyInfo property in type.GetProperties()) { sb.Append("<" + property.Name + ">"); if (property.PropertyType.IsArray) { Type t = property.PropertyType.GetElementType(); sb.Append(GetProperties(t)); } else { sb.Append(property.PropertyType.name); } } sb.ToString(); }