• 反射


    一、方法带参数
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Linq.Dynamic;
    using System.Reflection;
    using System.Text.RegularExpressions;
    
     
    
    namespace Demo
    {
    public partial class Form6 : Form
    {
    public Form6()
    {
    InitializeComponent();
    }
    
    private void Form6_Load(object sender, EventArgs e)
    {
    
    //Assembly assembly = Assembly.Load("Demo");
    
    Assembly assembly = Assembly.Load(Assembly.GetExecutingAssembly().ToString());
    
    Type type = assembly.GetType("Demo.Form6");
    
    
    MethodInfo met = type.GetMethod("Add");
    object obj = Activator.CreateInstance(type, null);
    Object[] num = { 10, 11 };
    MessageBox.Show(met.Invoke(obj, num).ToString());
    
    }
    
    public int Add(int p1, int p2)
    {
    return p1 + p2;
    }
    
     
    
    }
    
    }
    View Code
    二、方法属性赋值
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Linq.Dynamic;
    using System.Reflection;
    using System.Text.RegularExpressions;
    
    namespace Demo
    {
    public partial class Form6 : Form
    {
    public Form6()
    {
    InitializeComponent();
    }
    
    private void Form6_Load(object sender, EventArgs e)
    {
    //Assembly asm = Assembly.Load("Demo");
    
    Assembly assembly = Assembly.Load(Assembly.GetExecutingAssembly().ToString());
    var type = asm.GetType("Demo.Test");
    
    var instance = asm.CreateInstance("Demo.Test");
    
    type.GetProperty("Name").SetValue(instance, "http://greenerycn.cnblogs.com", null);
    type.GetProperty("Id").SetValue(instance, 1, null);
    
    var method = type.GetMethod("Hello");
    method.Invoke(instance, null);
    
    }
    }
    
    public class Test
    {
    private int id;
    private string name;
    
    public int Id
    {
    get { return this.id; }
    set { this.id = value; }
    }
    
    public string Name
    {
    get { return this.name; }
    set { this.name = value; }
    }
    
    public void Hello()
    {
    MessageBox.Show(Name);
    }
    }
    
    }
    View Code
  • 相关阅读:
    RMAN 高级恢复
    从问题域出发认识Hadoop生态系统
    下一代 Hadoop YARN :相比于MRv1,YARN的优势
    Sensei:分布式, 实时, 半结构化数据库
    盘点2012:云计算的春天
    Apache Tajo:一个运行在YARN上支持SQL的分布式数据仓库
    实现多个jetty实例开机后自动启动
    淘宝在数据处理领域的项目及开源产品介绍
    SenseiDB架构设计分析
    在Hadoop上运行SQL:程序员需知晓的13种数据工具
  • 原文地址:https://www.cnblogs.com/hanmian4511/p/5473016.html
Copyright © 2020-2023  润新知