111,126
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;
namespace _4._0Test
{
class Program
{
static void Main(string[] args)
{
var temp = GetInstance("IA");
Console.Read();
}
public static List<IA> GetInstance(string interfaceName)
{
var asm = Assembly.Load("4.0Test");//改成你的程序集名称或者路劲
var types = asm.GetTypes();
var instances = new List<IA>();
foreach (var temp in types)
{
var tempInterface = temp.GetInterface(interfaceName);
if (tempInterface != null && tempInterface == typeof(IA))
{
var instance = Activator.CreateInstance(temp);
if (instance is IA)
{
instances.Add(instance as IA);
}
}
}
return instances;
}
}
public interface IA
{ }
public class B : IA
{
}
public class C : IA
{
}
}