zoukankan      html  css  js  c++  java
  • 反射

    一、方法带参数
    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
  • 相关阅读:
    领会一些比较巧妙的算法
    操作系统os常识
    C++中的继承与虚函数各种概念
    我学shell程序的记录
    matlab:linux环境中将m文件编译成动态链接库
    struct内存对齐:gcc与VC的差别
    fedora中丢失或损坏fstab,无法启动,如何补救
    判断一个字符串中的字符是否都在另一个中出现
    linux下的不错的小软件:apvlv,zathura和vifm
    C语言中将结构体写入文件
  • 原文地址:https://www.cnblogs.com/hanmian4511/p/5473016.html
Copyright © 2011-2022 走看看