C
C#3y ago
Seventy

Get list of all Classes using Interface.

Is there a way i can get a list of all the classes using this interface?
public interface IConsoleCommand
{
string CommandWord { get;
}
bool Process (string[] args);
}
public interface IConsoleCommand
{
string CommandWord { get;
}
bool Process (string[] args);
}
Here is a class using it:
public class ExampleCommand : IConsoleCommand
{
public string CommandWord { get; } = "example";

public bool Process(string[] args)
{
//Run fancy code here!
//Make sure if the execution of your command was successfull => return true;
//else => return false;
//kind of like this:
if(args.Length == 0 || args == null)
{
MaumConsole.instance.Error("Missing Argument(s)", CommandWord);
return false; // This is optional. Just checking if user entered arguments!
}

if (args[0].Equals("pls", StringComparison.OrdinalIgnoreCase))
{
// yay the entered command was "[prefix][CommandWord] pls" and now i will help by sending some cool data > Debug.log();
MaumConsole.instance.Log("Yay you ran the example command!",CommandWord);
return true;
}
else
{
//oh no
MaumConsole.instance.Error("Wrong Argument(s)",CommandWord);
return false;
}
}
}
public class ExampleCommand : IConsoleCommand
{
public string CommandWord { get; } = "example";

public bool Process(string[] args)
{
//Run fancy code here!
//Make sure if the execution of your command was successfull => return true;
//else => return false;
//kind of like this:
if(args.Length == 0 || args == null)
{
MaumConsole.instance.Error("Missing Argument(s)", CommandWord);
return false; // This is optional. Just checking if user entered arguments!
}

if (args[0].Equals("pls", StringComparison.OrdinalIgnoreCase))
{
// yay the entered command was "[prefix][CommandWord] pls" and now i will help by sending some cool data > Debug.log();
MaumConsole.instance.Log("Yay you ran the example command!",CommandWord);
return true;
}
else
{
//oh no
MaumConsole.instance.Error("Wrong Argument(s)",CommandWord);
return false;
}
}
}
Also is there a way i can create a instance of every class using IConsoleCommand?
8 Replies
ero
ero3y ago
a list of all classes (types) implementing this interface, or a list of all instances of classes implementing this interface?
Seventy
SeventyOP3y ago
implementing
ero
ero3y ago
yeah that didn't answer the question
Seventy
SeventyOP3y ago
"a list of all classes (types) implementing this interface"
ero
ero3y ago
and you want those of your own project?
Seventy
SeventyOP3y ago
yes
ero
ero3y ago
sorry to disappoint, but you should really just do typeof(TheClass) for each class that implements it that'll be heaps cleaner and faster
Seventy
SeventyOP3y ago
ight
Want results from more Discord servers?
Add your server