1
为了解决我在使用反射的解决方案中遇到的问题,我需要指定以下代码以向用户显示一个CheckedListBox,它显示了它们具有的条件列表选择,并根据他们的选择修改应用程序中的某种行为。 在这个时候,我没有问题得到继承类的字符串名称感谢this后,但我不知道如何获得每个实例。获取实现接口的每个类的实例
DataTable table = new DataTable();
table.Columns.Add("Intance", typeof(IConditions)); //INSTANCE of the inherited class
table.Columns.Add("Description", typeof(string)); //name of the inherited class
//list of all types that implement IConditions interface
var interfaceName = typeof(IConditions);
List<Type> inheritedTypes = (AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => interfaceName.IsAssignableFrom(p) && p != interfaceName)).ToList();
foreach (Type type in inheritedTypes)
{
IConditions i; //here is where I don't know how to get the instance of the Type indicated by 'type' variable
//I.E: IConditions I = new ConditionOlderThan20(); where 'ConditionOlderThan20' is a class which implements IConditions interface
table.Rows.Add(i, type.Name);
}
可能得到一个对象吗?处理这样的问题的更好的方法是什么?
优秀的Konrad!这是我需要的!我的情况构造函数不是一个问题,但是是一个有效的声音。 – 2014-11-02 19:59:06