Note record-C# reflection

(1) Create, add, and traverse the output List<int> 

        object intList;
        //获取List<int>类型
        var listIntType = typeof(List<>).MakeGenericType(new Type[] { Type.GetType("System.Int32") });
        //创建List<int>对象
        intList = Activator.CreateInstance(listIntType, new object[] { });
        //获取List<int>的Add方法
        BindingFlags flag = BindingFlags.Instance | BindingFlags.Public;
        MethodInfo addMethod = intList.GetType().GetMethod("Add", flag);
        //调用Add方法
        addMethod.Invoke(intList, new object[] { 1 });
        //第2种.调用Add方法
        //BindingFlags flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod;
        //intList.GetType().InvokeMember("Add", flag, null, intList, new object[] { 1 });
        //遍历List输出
        IEnumerable list = intList as IEnumerable;
        foreach (var v in list)
        {
            Debug.LogError(v);
        }

 

Guess you like

Origin blog.csdn.net/qq_39574690/article/details/109173003