在运行时使用C#读取参数,属性和返回类型的一种方法 [英] One method to read parameters, properties and return types at runtime using C#

查看:189
本文介绍了在运行时使用C#读取参数,属性和返回类型的一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着我以前的线程的连续使用包含其他对象数组的对象的反射读取属性。我希望通过EvgK做出一个可以在代码库中的多个地方使用的通用方法的这个奇妙的方法。

With continutation to my earlier thread Using reflection read properties of an object containing array of another object. I am hoping to make this wonderful method from EvgK a generic method that can be used in multiple places in my code base.

public static void GetMyProperties(object obj)
{
    List<MyPropertyInfo> oMyProp = new List<MyPropertyInfo>();
    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<MyPropertyInfo> oTempMyProp = GetMyProperties(prop.GetValue(obj, null));
                oMyProp.AddRange(oTempMyProp);
            }
        }


    }
}

再次,我试图读取用户传递的方法。列出参数,它们的属性和值。一旦用户提供输入值,我就会动态调用该方法来获取结果对象。结果传递给GetMyProperties()方法,方法列出所有属性(到n级) - 名称,值和类型。

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.

目前,我有两种方法(下面的定义):

Currently, I have two methods (definations below):

public List<MyPropertyInfo> GetMyProperties(Type type);

public List<MyPropertyInfo> GetMyProperties(object obj);

我使用第一个显示所选方法的所有参数及其属性的列表 - 名称,值和类型。

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.

MethodInfo members = type.GetMethod(this.MethodName);
ParameterInfo[] parameters = members.GetParameters();
List<MyPropertyInfo> oMyProp = new List<MyPropertyInfo>();
foreach (var parameter in parameters)
{
    oMyProp = GetMyProperties(parameter.ParameterType);    
}

..创建我的属性列表,以便用户可以输入参数。我传递ParameterType和GetProperties方法检查是否是自定义类型。如果自定义类型,那么它会以递归方式调用自己的类型来构建一个绑定到网格进行输入的列表。

..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.

第二种方法GetMyProperties(object obj)用于列出返回对象。因为我在编译时不知道所选方法的返回类型,所以使用对象类型。我想知道我是否可以修改第二种方法,以使用它来读取参数,属性和返回类型?而不是单独的方法?尝试重用代码。

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.

推荐答案

我不知道我是否正确理解你,但如果要将两种方法一个,你可以在两种情况下传递对象,但检查对象是否为Type,并为这种情况提供不同的逻辑:

I'm not sure I understand you correctly, but if you want to combine two methods in one, you can pass object in both cases, but check if object is Type or not and provide different logic for that cases:

public static void GetMyProperties(object obj)
{
   if (obj is Type)
   {
      Type type = obj as Type;
      ... here is the first method logic ...
   } 
   else 
   {
      ... here is the second method logic ...
   }
}

这篇关于在运行时使用C#读取参数,属性和返回类型的一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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