Mit Reflection habe ich alle Typen in der Baugruppe. Wie kann ich werfe den Typen t auf einen ICommand, wenn ich weiß, dass es Interface „ICommand“Wie man einen Typ zur Laufzeit explizit in Interface umwandelt
ICommand C;
foreach(Type t in asm.GetTypes())
{
if (t.GetInterfaces()[0].Name is "ICommand")
{
C = (ICommand)t; //throws Exception here - Unable
//to cast to ICommand
RootDir.AddCommand(C, t.Namespace.Split('.'));
}
}
Ein Beispiel für die Art implementiert I
public interface ICommand
{
string HelpDescription { get; }
void Execute(CommandClass CC);
}
class CurrentDir : ICommand
{
public string HelpDescription => "Current Directory - Change current directory";
public static explicit operator CurrentDir (Type T)
{
return new CurrentDir();
}
void ICommand.Execute(CommandClass CC)
{
throw new NotImplementedException();
}
}
werfen versuchen Wie soll ich das so umsetzen dass es von System.Type in ICommand umgewandelt werden kann?