我有81个实现接口的类(还在增长),还有一个类具有一个属性,该属性表示接口ex:
public wrapper
{
public Imyinterface instance{get;set;}
public wrapper(string theNameOfTheClass)
{
instance = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(theNameOfTheClass);
instance.Run(); //the interface has a run method
}
}这是最好的方法吗?我如何避免反射,而不必编写任何额外的代码?
发布于 2014-04-09 11:03:06
查看(或任何其他可以通过字符串解析的IoC ):
来自IOC Container Runtime Resolution
string typeName = "MyTypeName";
var type = container.Registrations.FirstOrDefault(r => r.RegisteredType.Name == typeName);
if(type != null)
{
var resolvedInstance = container.Resolve(type.RegisteredType);
}编辑:对于超过配置的会议,请查看本问题中链接的链接。
https://stackoverflow.com/questions/22960248
复制相似问题