zoukankan      html  css  js  c++  java
  • 进入内存看反射

    using System;

     

    namespace Reflection

    {

          publicclassMyClass

          {

               publicstring MyField;

     

               publicvoid MyMethod()

               {

                     Console.WriteLine("调用MyMethod! MyField: {0}", MyField);

               }

     

               publicvoid MyMethod(string param)

               {

                     Console.WriteLine("调用MyMethod! MyField: {0}. 参数: {1}", MyField, param);

              }

          }

    }

    using System;

    using System.Reflection;

     

    namespace Reflection

    {

          /// <summary>

          publicclassReflection

          {

               publicstaticvoid Main()

               {

    // 从文件中加载程序集MyAssembly

                     Assembly a = Assembly.LoadFrom("MyAssembly.dll");

                     // 获取程序集中名为"MyClass"Type, 这里必须用全名

                     Type type = a.GetType("Reflection.MyClass");

                     // 动态创建MyClass 的一个实例

                     Object obj = Activator.CreateInstance(type);

                     // 动态设置obj 的字段

                     FieldInfo field = type.GetField("MyField");

                     field.SetValue(obj, "动态设置的字段");

                     // 动态调用obj 的方法

                     MethodInfo method = type.GetMethod("MyMethod", newType[0]);

                     method.Invoke(obj, null);

                     method = type.GetMethod("MyMethod", newType[]{typeof(string)});

                     method.Invoke(obj, newObject[]{"一个参数"});

                Console.Read();

               }

          }

    }

  • 相关阅读:
    springmvc 注解式开发 处理器方法的返回值
    springmvc 注解式开发 接收请求参数
    【洛谷P1379】八数码难题 状压bfs
    【模板】ST表
    【POJ1741】Tree
    【洛谷P1073】最优贸易
    【POJ3662】Telephone Lines dij + 二分答案
    【模板】spfa
    【洛谷P2384】最短乘积路径
    【bzoj2038】小Z的袜子
  • 原文地址:https://www.cnblogs.com/yongbufangqi1988/p/1749027.html
Copyright © 2011-2022 走看看