yoyoalphax 2016-12-23 08:50 采纳率: 100%
浏览 772
已采纳

如何取得这些参数(内含源码)

如何取得这些参数
如何取得这些参数

 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace getPropTest
{
    public class dologin
    {
        public static RootObject rootObject = new RootObject();

        public class RootObject
        {
            public string param { get; set; }
        }

        public void exec()
        {
            rootObject.param = "New_param";
        }
    }

    class Program
    {
        public static object getObject(string className)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            assembly.GetTypes();
            object obj = assembly.CreateInstance("getPropTest." + className); 
            return obj;
        }

        public static bool execMethod(object obj, string methodName)
        {
            try
            {
                Type type = obj.GetType();
                MethodInfo methodinfo = type.GetMethod(methodName);
                methodinfo.Invoke(obj, null);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static void getProperties(object obj, string memberName)
        {
            Type type = obj.GetType();

            if (memberName == "RootObject")
            {
                MemberInfo[] memberInfo = type.GetMember("rootObject");
                object sub_obj = memberInfo.GetValue(0);

                Console.WriteLine(sub_obj.ToString());//RootObject rootObject

                /*

                 * 如何获取rootObject下的所有属性,比如此处的param = "New_param"???

                 */
            }
        }

        static void Main(string[] args)
        {
            object obj = getObject("dologin");
            execMethod(obj, "exec");
            getProperties(obj, "RootObject");
        }
    }
}

  • 写回答

2条回答 默认 最新

  • yoyoalphax 2016-12-23 16:54
    关注

    已解决

             public static void getProperties(object obj, string memberName)
            {
                Type type = obj.GetType();
                FieldInfo field = null;
    
                if (memberName == "RootObject")
                {
                    field = type.GetField("rootObject");
                }
    
                object _obj = field.GetValue(obj);
                Type _type = _obj.GetType();
    
                foreach (PropertyInfo propertyInfo in _type.GetProperties())
                {
                    Console.WriteLine("{0}: {1}", propertyInfo.Name, propertyInfo.GetValue(_obj, null));
                }
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码