Reading elements of an array of objects using a C # reflex

How can I get a list of elements and data types from an object array in C # with reflections?

Scenario: I have a method with an array parameter in my web service (asmx). Using reflection, I read the parameters of the method and view the properties. Here is my code:


Example: I have a webservice http: //localhost/services/myservice.asmx . It has a string GetServiceData(context mycontext) method string GetServiceData(context mycontext) . the context class has the following properties

 - string name - Account[] accounts 

The account, in turn, has

 - string acct_name - string acct_address 

I need to read the service dynamically to create the following output

 <GetServiceData> <mycontext> <name>string</name> <accounts> <acct_name>string</acct_name> <acct_address>string</acct_address> </accounts> </mycontext> </GetServiceData> 

To do this, I dynamically read MethodInfo and get all the parameters. Then I look through the parameters to get a list of all the properties and data types. If there is an array element in the properties, then I need to get all the elements of this array element.

Solution (thanks 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 + ">"); } 

The GetProperties () method reads the type and actually iterates over each property and adds it to the string object. In this method, I want to check if the property is an array, then get all the elements and enter 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(); } 
+4
source share
2 answers

I think that you are looking for the Type.IsArray property (indicates whether the type is an array) and Type.GetElementType (receives, among other things, the type element of the array). Note that the type element of the array does not necessarily coincide with the type of the specific runtime of each of the elements of the array; polymorphism can come into play.

Of course, to just get the values, you can rely on the covariance array: specify the value of the property (suppose you use PropertyInfo.GetValue somewhere) in object[] and foreach, as usual.

EDIT

Your update is pretty confusing. If you already have an object that you think can be an array; you also can:

 object obj = ... object[] array = obj as object[]; if(array != null) { foreach(object item in array) { ... } } 

It really looks like you are mixing metadata with real data here. You cannot list any array if all you have is System.Type , which represents the type of array.

EDIT

I think I finally understood what you want to do. Just use Type.GetElementType to get the type of the element of the array, and then get the properties of that type (recursively?). You will probably change your design a bit to get the desired XML result. XML is a hierarchy; but your return-type method is just a Dictionary<string, string> , which is a flat data structure.

+9
source

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 an array, you can use GetElementType as follows:

  ... //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()); } ... 
+1
source

Source: https://habr.com/ru/post/1343662/


All Articles