您正在寻找反射或 XML 序列化。
使用反射,您可以使用类似这样的方式查找类型
public IYourInterface GetClass(string className)
{
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type type in asm.GetTypes())
{
if (type.Name == className)
return Activator.CreateInstance(type) as IYourInterface;
}
}
return null;
}
请注意,这将遍历所有程序集。您可能希望将其减少为仅包含当前正在执行的程序集。
对于分配属性值,您还可以使用反射。类似的东西
IYourInterface o = GetClass("class1");
o.GetType().GetProperty("prop1").SetValue(o, "foo", null);
虽然反射可能是最灵活的解决方案,但您还应该看看XML-serialization,以便自己跳过繁重的工作。