2008-08-21 18 views

Respuesta

8

Su pregunta es muy confuso ...

Si usted quiere encontrar tipos que implementan iStep, a continuación, haga lo siguiente:

foreach (Type t in Assembly.GetCallingAssembly().GetTypes()) 
{ 
    if (!typeof(IStep).IsAssignableFrom(t)) continue; 
    Console.WriteLine(t.FullName + " implements " + typeof(IStep).FullName); 
} 

Si conoce ya el nombre del tipo requerido, simplemente hacer esto

IStep step = (IStep)Activator.CreateInstance(Type.GetType("MyNamespace.MyType")); 
1

Sobre la base de lo que otros han señalado, esto es lo que terminó la escritura:

 
/// 
/// 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); 
    } 
} 
0

Bueno Assembly.CreateInstance parece ser el camino a seguir - el único problema con esto es que se necesita el nombre completo del tipo, es decir, que incluye el espacio de nombres.

Cuestiones relacionadas