一种使用 C# 在运行时读取参数、属性和返回类型的方法

发布于 2024-10-16 06:26:24 字数 2221 浏览 2 评论 0原文

继续我之前的线程 使用反射读取包含另一个对象数组的对象的属性。我希望使 EvgK 中的这个美妙方法成为可以在我的代码库中的多个位置使用的通用方法。

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 级) - 名称、值和类型。

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

public List<MyPropertyInfo> GetMyProperties(Type type);

public List<MyPropertyInfo> GetMyProperties(object obj);

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

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 方法检查它是否是自定义类型。如果自定义类型,那么它会递归地使用该类型调用自身来构建一个列表,我将其绑定到网格以进行输入。

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

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);
            }
        }


    }
}

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);    
}

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜深人未静 2024-10-23 06:26:24

我不确定我是否正确理解了您的意思,但是如果您想将两种方法合并为一种,您可以在两种情况下传递对象,但检查对象是否为 Type 并为这种情况提供不同的逻辑:

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

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 ...
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文