|
|
1
8
你的问题很困惑… 如果要查找实现ISTEP的类型,请执行以下操作:
如果您已经知道所需类型的名称,只需执行此操作
|
|
|
2
2
如果实现具有无参数的构造函数,则可以使用System.Activator类来实现。除了类名之外,还需要指定程序集名称:
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx |
|
|
3
1
根据其他人所指出的,这就是我最后写的: ///
/// Some magic happens here: Find the correct action to take, by reflecting on types
/// subclassed from IStep with that name.
///
private IStep GetStep(string sName)
{
Assembly assembly = Assembly.GetAssembly(typeof (IStep));
try
{
return (IStep) (from t in assembly.GetTypes()
where t.Name == sName && t.GetInterface("IStep") != null
select t
).First().GetConstructor(new Type[] {}
).Invoke(new object[] {});
}
catch (InvalidOperationException e)
{
throw new ArgumentException("Action not supported: " + sName, e);
}
}
|
|
4
0
好吧,assembly.createInstance似乎是一种可行的方法——唯一的问题是它需要类型的完全限定名,即包括名称空间。 |