zoukankan      html  css  js  c++  java
  • C#反射方式调用泛型方法

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    
    namespace WindowsFormsApplication8
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private  void  Paster<Ent>(String dicNo)
            where Ent : class, new()
            {
                var ent = new Ent();
                var list = new List<Ent>();
                for (int i = 0; i < 10; i++)
                {
                    var e = new Ent();
                    list.Add(e);
                }
                Console.WriteLine(typeof(Ent).ToString() + "__" + dicNo +"___" + ent);
                
            }
            private void button1_Click(object sender, EventArgs e)
            {
    
    
    
                Type t = this.GetType();
                BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
                var m1 = this.GetType().GetMethod("Paster", flags);
                var info= m1.MakeGenericMethod(typeof(SEnt));
    
                 //object list = t.InvokeMember(null,
                 //    BindingFlags.DeclaredOnly |
                 //    BindingFlags.Public | BindingFlags.NonPublic |
                 //    BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { });
    
    
                info.Invoke(this,new Object[]{"ttt"});
         
            }
        }
        public class SEnt
        {
            public String Name { get; set; }
            public override string ToString()
            {
                return DateTime.Now.ToString();
            }
        }
    
    }
    View Code
  • 相关阅读:
    二分查找 && 三分查找
    LeetCode || 大杂烩w
    LeetCode || 递归 / 回溯
    LeetCode || 双指针 / 单调栈
    HITICS || 2018大作业 程序人生 Hello's P2P
    思维 || Make It Equal
    国庆集训 || Wannafly Day4
    国庆集训 || Wannafly Day1
    区间DP || HDU 6249 Alice’s Stamps
    10进制转k进制
  • 原文地址:https://www.cnblogs.com/wdfrog/p/11993257.html
Copyright © 2011-2022 走看看