Note that there are some explanatory texts on larger screens.

plurals
  1. POOne method to read parameters, properties and return types at runtime using C#
    primarykey
    data
    text
    <p>With continutation to my earlier thread <a href="https://stackoverflow.com/questions/4879197/using-reflection-read-properties-of-an-object-containing-array-of-another-object">Using reflection read properties of an object containing array of another object</a>. I am hoping to make this wonderful method from EvgK a generic method that can be used in multiple places in my code base.</p> <pre><code>public static void GetMyProperties(object obj) { List&lt;MyPropertyInfo&gt; oMyProp = new List&lt;MyPropertyInfo&gt;(); foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { if (!Helper.IsCustomType(pinfo.PropertyType)) { //add properties - name, value, type to the list } else { var getMethod = pinfo.GetGetMethod(); if (getMethod.ReturnType.IsArray) { var arrayObject = getMethod.Invoke(obj, null); foreach (object element in (Array)arrayObject) { foreach (PropertyInfo arrayObjPinfo in element.GetType().GetProperties()) { //add properties - name, value, type to the list } } } else { List&lt;MyPropertyInfo&gt; oTempMyProp = GetMyProperties(prop.GetValue(obj, null)); oMyProp.AddRange(oTempMyProp); } } } } </code></pre> <p>Again, I am trying to read a method passed by the user. I list the parameters, their properties and values. Once user provides the input values, I call the method dynamically to get the result object. The result is passed to GetMyProperties() method and the method list all the properties (to n level) - name, value and type. </p> <p>Currently, I have two methods (definations below):</p> <pre><code>public List&lt;MyPropertyInfo&gt; GetMyProperties(Type type); public List&lt;MyPropertyInfo&gt; GetMyProperties(object obj); </code></pre> <p>I use the first one to show the list of all the parameters of the selected method along with it's properties - name, value and type.</p> <pre><code>MethodInfo members = type.GetMethod(this.MethodName); ParameterInfo[] parameters = members.GetParameters(); List&lt;MyPropertyInfo&gt; oMyProp = new List&lt;MyPropertyInfo&gt;(); foreach (var parameter in parameters) { oMyProp = GetMyProperties(parameter.ParameterType); } </code></pre> <p>..creating the list of my properties so that user can input the params. I pass ParameterType and GetProperties method checks if it is custom type or not. If custom type then it calls itself with the type recursively to build a list that I bind to a grid for input.</p> <p>The second method GetMyProperties(object obj) is used to list the return object. Since I don't know the return type of the selected method at compile time so using object type. I want to know if I can modify the second method somehow to use it for reading the parameters, properties and return types? Instead of having separate methods? Trying to reuse the code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload