C
C#8mo ago
Akex

❔ Which types should I use for this dictionary

I am making a command interpreter from a game, which the players can use via /command <parameters>, each command can have different parameters, and different types for each parameter.
@Command(name="help",permission=Permission.User)
public void help(CommandContext context, Player player) {...}
@Command(name="help",permission=Permission.User)
public void help(CommandContext context, Player player) {...}
The only necessary parameter is context, player can be any other type, for example a string. So now I wanted to create a dictionary like { Player=PlayerConverter, Foo=FooConverter } which takes parameter type of the method, and converts it with the converter class from string to x. So the issue is that I do not know which types I need in the dictionary
5 Replies
Pobiega
Pobiega8mo ago
Mhm, so you're essentially re-inventing DI but for methods I would do this using a CommandHandler style approach myself
Akex
Akex8mo ago
could you give me a small example?
Pobiega
Pobiega8mo ago
[GameCommand("help")]
public class HelpCommandHandler : ICommandHandler
{
private readonly Player _player;

public HelpCommandHandler(Player player)
{
_player = player;
}

public void Handle(CommandContext context)
{
..
}
}
[GameCommand("help")]
public class HelpCommandHandler : ICommandHandler
{
private readonly Player _player;

public HelpCommandHandler(Player player)
{
_player = player;
}

public void Handle(CommandContext context)
{
..
}
}
use the attributes to build a keyed type map (string to Type), then resolve the correct type from a DI container aware of your domain (players etc), cast it to ICommandHandler, then just pass the context to the handler and you're done
Akex
Akex8mo ago
thanks! Ill give it a try
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.