0

For context:

I'm trying to not rely on a collection to store "Command" type because of the overhead of iteration when i want to get one

Here what i am trying to do is to do so when ever i instantiate any child class of "Command", the base constructor will get Id and Fields from the static instance of that command, the problem is that i would have thought that i could pass "this" to Registery but unless i pass it the type MyCommand explicitly it wont work with this.GetType()

public static class Registery<T>
where T : Command
{
    public static T instance;
}

public abstract class Command
{
    public int Id { get; protected set; }
    private FieldInfo[] Fields { get; protected set; }

    public Command()
    {
          Id = Registery<this.GetType()>.instance.Id;       <<<-- doesn't work
          Fields = Registery<MyCommand>.instance.Fields;    <<<-- works
    }    
}

public class MyCommand: Command
{

}

What am i doing wrong ?

user2696482
  • 63
  • 2
  • 8
  • Are you trying to create an abstract factory? – Enigmativity May 05 '20 at 11:13
  • When working with types at an inheritance level, whatever class you use this.GetType() in will return the type of class that you're in, not the child class. You have to specify the class or the instance of the class to get the type that you want. I would suggest adding a Type parameter to your constructor so whatever class that inherits Command can pass in a Type of it's own class down into the base. – KingOfArrows May 05 '20 at 12:00
  • @Enigmativity I'm not really familliar with this pattern what i was trying to do is a makeshift collection where Registery is called at the start of the program and store a single instance of every Command types, i did this because any new Command child needs a Id and Fields that is the same for every instance of its type (ex: MyCommand will always have Id = 4), since i need to get this only once i thought that i could have any child type static and new objects will just fetch theirs id and fields based on their type. i'm not sure if this is possible but i just went with a Dictionnary – user2696482 May 05 '20 at 15:59
  • I feard that since i would have +200 Command type that iterating in a dictionnary will be long – user2696482 May 05 '20 at 16:01
  • @KingOfArrows I'm not sure what you mean i've tried in debug and upon entering the constructor "this" really is of type child, i think the problem comes from the fact that GetType is at runtime sadly – user2696482 May 05 '20 at 16:07
  • @KingOfArrows - "this.GetType() in will return the type of class that you're in, not the child class" - No, that's wrong. `this.GetType()` always returns the actual type or the object. – Enigmativity May 06 '20 at 01:38
  • @user2696482 - How do you expect to retrieve each of the types that you put in? `Registry.Instance`? – Enigmativity May 06 '20 at 01:41
  • Through reflection with a foreach Type type is a subclassof Command but i just noticed that i'll have the same problem, i can't feed Registry because its at runtime.. – user2696482 May 06 '20 at 05:57

0 Answers0