• Assembly(程序集) 反射和缓存


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Winfrom
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
            }
    
            private void Form4_Load(object sender, EventArgs e)
            {
                Assembly asm = Assembly.Load("Winfrom");
                var type = asm.GetType("Winfrom.Test");
    
                var instance = asm.CreateInstance("Winfrom.Test");
    
                type.GetProperty("Name").SetValue(instance, "hello world", 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);
            }
        }
    }
     protected static List<Tuple<string, object, object>> AssembleyCache = new List<Tuple<string, object, object>>();
    
    
                            object myObj = null;      
                            MethodInfo myMethod = null;
    
                            if (AssembleyCache.Exists(c => c.Item1 == labelMethod))
                            {
                                myObj = AssembleyCache.First(c => c.Item1 == labelMethod).Item2;
                                myMethod = (MethodInfo)AssembleyCache.First(c => c.Item1 == labelMethod).Item3;
                            }
                            else
                            {
                                Assembly assembly = Assembly.Load("Yanwen.Online.Common.Print");
                                Type type = assembly.GetType("Yanwen.Online.Common.Print.CreatPdfUtil");
    
                                myMethod = type.GetMethod(方法名称);
                                myObj = Activator.CreateInstance(type, null);
    
                                AssembleyCache.Add(new Tuple<string, object, object>(方法名称, myObj, myMethod));
                            }
                            if (myObj != null)
                            {
                                object[] parameter = { expressTypeModel };
                                var result = myMethod.Invoke(myObj, parameter);
                                stream = (MemoryStream)result;
                            }
  • 相关阅读:
    【并查集】hdu 1198 Farm Irrigation
    【并查集】hdu 1325 Is It A Tree?
    【并查集】hdu 1272 小希的迷宫
    【并查集】hdu 1856 More is better
    【并查集】hdu 3635 Dragon Balls
    【并查集】poj 1988 Cube Stacking
    【并查集】poj 2492 A Bug's Life(二集合问题)
    【并查集】poj 2236 Wireless Network
    【并查集】poj 1703 Find them, Catch them(二集合问题)
    【并查集】poj 1611 The Suspects
  • 原文地址:https://www.cnblogs.com/xiaoqi742709106/p/5477321.html
Copyright © 2020-2023  润新知