0

I want to create a C# struct that stores a class type property which later can be instantiated.

I created the struct like this:

internal struct Command<T>
{
    internal string trigger;
    internal T clazz;
    internal string category;
}

Then I want to store these Command structs in a dictionary:

private Dictionary<string, Command> _commandMap;

How do I define the dict so that its value type is set correctly for Command? Or is my whole approach wrong?

BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • 2
    Are you trying to store multiple different kinds of command in the dictionary, e.g. a `Command` and a `Command`? If so, you're definitely going to run into problems. (I'd also strongly recommend keeping fields private, and using properties, as well as making the struct immutable.) – Jon Skeet Feb 23 '19 at 14:54
  • @JonSkeet The command shouldn't be of types like int or string but of class types (e.g. MyCustomCommandClass). – BadmintonCat Feb 23 '19 at 14:56
  • Well string is a class as well... it sounds like you might want a constraint on `T`. But back to the dictionary - would you want to be able to store a `Command` and a `Command` in the same dictionary? If you could give us more context, it will be easier to help you. – Jon Skeet Feb 23 '19 at 14:57
  • @JonSkeet yes there can be different command classes but they all implement a certain interface, e.g. ICommand. Sorry for the lack of context! I'm still figuring the whole logic out in my head while trying to port it from some old AS3 code). – BadmintonCat Feb 23 '19 at 15:01
  • @JonSkeet can this be accomplished with a covariant generic interface, such as `ICommand where T: someKnownBaseType`? I am not familiar enough with the topic, unfortunately, but it sounds like the right use case. – CoolBots Feb 23 '19 at 16:24
  • Not if there are going to be multiple types in the same dictionary. But you could create a *non-generic* interface, and have `Dictionary`. Note that that would box each of the values though. – Jon Skeet Feb 23 '19 at 18:21

1 Answers1

2

If you want your dictionary to be type specific:

private Dictionary<string, Command<my_type>> _commandMap;

If you want everything in one dictionary, then don't use a generic for the class.
You wrote, that you want to store a class type property to instantiate it later. Yet in your code you store a class instance, not a class type.
I think this is what you are looking for:

internal struct Command
{
    internal string trigger;
    internal Type clazz;
    internal string category;
}

or

internal struct Command
{
    internal string trigger;
    internal IMyInterface clazz;
    internal string category;
}

and finally:

private Dictionary<string, Command> _commandMap;
G. B.
  • 528
  • 2
  • 15
  • Yes, I think `Type` is what I was looking for, thanks! But one question... how do I instantiate `clazz` later? It seems `var c = new command.clazz()` isn't the way it works in C#. – BadmintonCat Feb 23 '19 at 17:25
  • I think the question may need some clarification, but, from context and comments, I am pretty certain OP wants to store an instance of a generic type `T` within the `Command` struct. – CoolBots Feb 23 '19 at 17:25
  • @CoolBots no, not an instance. I want to store the actual class type so I can later instantiate new objects from it. – BadmintonCat Feb 23 '19 at 17:26
  • object instance = Activator.CreateInstance(type); like here: https://stackoverflow.com/questions/11958825/c-sharp-creating-instance-of-class-and-set-properties-by-name-in-string – G. B. Feb 23 '19 at 17:29
  • https://riptutorial.com/csharp/example/15938/creating-an-instance-of-a-type – G. B. Feb 23 '19 at 17:31
  • @G.B. Thanks for the links! That's going to helpful! – BadmintonCat Feb 23 '19 at 17:35
  • If what you want to store is a reference value then you can add a constraint for `T` like `internal struct Command where T : new()` then you can do `var instance = new T()` – Diego Osornio Feb 23 '19 at 18:56