zoukankan      html  css  js  c++  java
  • 第15章 .NET中的反射

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                //TestClass.ShowMsg();
                //TestClass.ShowMsg(new Message());
    
                //Assembly asm = Assembly.GetExecutingAssembly();
                //Object obj=asm.CreateInstance("ConsoleApplication2.Calculator",true);
                //Calculator cal=(Calculator)obj;
    
                //Object[] parameters = new Object[2] { 3, 5 };
                //Object obj1 = asm.CreateInstance("ConsoleApplication2.Calculator", true, BindingFlags.Default, null, parameters, null, null);
                //Calculator cal = (Calculator)obj1;
    
                //Type t = typeof(Calculator);
                //Calculator cal = new Calculator(3, 5);
                //int result = (int)t.InvokeMember("Add", BindingFlags.InvokeMethod, null, cal, null);
                //Console.WriteLine("result:" + result);
    
                //Type t = typeof(Calculator);
                //Object[] parameters = new Object[2] { 3, 5 };
                //t.InvokeMember("Add", BindingFlags.InvokeMethod, null, t, parameters);
    
                //Type t = typeof(Calculator);
                //Calculator cal = new Calculator(3, 5);
                //MethodInfo mi = t.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
                //mi.Invoke(cal, null);
    
                //Type t = typeof(Calculator);
                //Object[] parameters = new Object[2] { 6, 9 };
                //MethodInfo mi = t.GetMethod("Add", BindingFlags.Public | BindingFlags.Static);
                //mi.Invoke(null, parameters);
    
                Console.ReadKey();
            }
        }
    
        public class Message { }
    
        public class TestClass
        {
            [Obsolete("请使用新方法")]
            public static void ShowMsg()
            {
                Console.WriteLine("旧MSG");
            }
    
            public static void ShowMsg(Message msg)
            {
                Console.WriteLine("新MSG");
            }
        }
    
        public class Calculator
        {
            private int x;
            private int y;
            public Calculator()
            {
                x = 0;
                y = 0;
                Console.WriteLine("Calculate()");
            }
    
            public Calculator(int x, int y)
            {
                this.x = x;
                this.y = y;
                Console.WriteLine("Calculate(int x, int y)");
            }
    
            public int Add()
            {
                int total = 0;
                total = x + y;
                Console.WriteLine("invoke instance method");
                Console.WriteLine(string.Format("{0}+{1}={2}", x, y, total));
                return total;
            }
    
            public static void Add(int x, int y)
            {
                int total = x + y;
                Console.WriteLine("invoke static method");
                Console.WriteLine(string.Format("{0}+{1}={2}", x, y, total));
            }
        }
    }
    View Code
  • 相关阅读:
    生成一个平面矩形网格文件
    生成球 使用openMesh 库
    U盘安装可能会遇见UEFI的问题,使用easyBCD安装即可。
    vs 代码格式化
    跨域问题的出现和解决
    代理服务器的作用 和 推荐
    sublime user 配置
    git 大佬的相关配置
    win10 注册
    【BZOJ3566】—概率充电器(树形+概率dp)
  • 原文地址:https://www.cnblogs.com/liuslayer/p/5944525.html
Copyright © 2011-2022 走看看