接上篇。

自定义一个泛型类(继承于接口)

 

    public interface IStack<T>
{

int Count { get; }
bool PushStack(T value);
T PopStack();
bool ClearStack();
}


 

类定义:

 

namespace stack
{
public class Mystack<T> :IStack<T>
{
private Stack<T> stack = new Stack<T>();
public int Count
{
get { return stack.Count; }
}

public Mystack()
{
}

public bool PushStack(T value)
{
stack.Push(value);
return true;
}

public T PopStack()
{
T value = stack.Pop();
return value;
}
public bool ClearStack()
{
stack.Clear();
return true;
}
}
}


 

调用:

 

using stack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionDemo
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.LoadFrom("stack.dll");
Console.WriteLine(assembly.FullName);
Type t = typeof(Mystack<int>);
object obj = assembly.CreateInstance(t.FullName); //创建其实例
MethodInfo mi = t.GetMethod("PushStack"); //调用方法
MethodInfo mi2 = t.GetMethod("PopStack");
object[] argss = { 1 }; //参数
object returnValue = mi.Invoke(obj, argss); // 触发此方法
Console.WriteLine(mi2.Invoke(obj,null));
Console.ReadLine();
}
}
}