0

What I want is to "dynamically" access a method of a generic class inside a class by index or by some better way. "Dynamically" because there will not only be two generic classes inside this main class, so I want an easy way to access the methods of the generic classes, because it would be exhausting to keep calling all the methods all the time. This is the class and what I already tried:

public class ListOfPowerClass
{
    public PowerClass<SomePowerClassOne> PowerOne = new PowerClass<SomePowerClassOne>(new SomePowerClassOne(), false);
    public PowerClass<SomePowerClassTwo> PowerTwo = new PowerClass<SomePowerClassTwo>(new SomePowerClassTwo(), false);    
    .....
    public PowerClass<SomePowerClassN> PowerN = new PowerClass<SomePowerClassN>(new SomePowerClassN(), false);

    public class PowerClass<T> where T : class 
    {
            public bool I_Have;
            public T Power;      

            public PowerClass(T _Power, bool _Active)
            {
                Power = _Power;
                Active = _Active;
            }
    } 
}

This is the methods inside each class.

public class SomePowerClassOne
{
    ///Some methods
}

public class SomePowerClassTwo
{
    ///Some methods
}

And this is what I want to do:

public int index; //This index will change and I will access different classes.
public ListOfPowerClass PowerList = new ListOfPowerClass();
PowerList[index].Print(); ///index = 0, I will access class PowerOne 
PowerList[index].Print(); ///index = 1, I will access class PowerTwo
PowerList[index].Print(); ///index = 2, I will access class PowerThree and so on

I googled and found out about Indexers ([int index]), but I don't now why I can't apply to the "ListOfPowerClass" Class or any of your methods like below, because It's just throw a sintax error.:

public class ListOfPowerClass [int index]
public T ReturnClass<T>(int index) [int index]
public class PowerClass<T> where T : class [int index]

And I also found about reflection, but I honestly don't understant anything that I read and I don't know how to make it work with my code.

Gabriel
  • 35
  • 4
  • What is it you are actually trying to do? Because this seem like a X/Y problem. – JonasH Aug 01 '21 at 18:29
  • 1
    Why do you want to do such a thing? Have you looked at [generics](https://learn.microsoft.com/dotnet/standard/generics/), [polymorphism](https://stackoverflow.com/questions/1031273/) and [AI systems](https://en.wikipedia.org/wiki/Artificial_intelligence) or [dispatch tables](https://en.wikipedia.org/wiki/Dispatch_table)? –  Aug 01 '21 at 18:31
  • @JonasH Yes, I only want to access of the class by index or if it exists, a better way. – Gabriel Aug 01 '21 at 18:31
  • 1
    You are asking about a method to do *something*. That method does not exist as you are describing it. If you actually told us what that *something* is we might be able to point you to a better solution. – JonasH Aug 01 '21 at 18:34
  • @OlivierRogier Sorry that I'm not that genius like you, that I don't looked at generics, polymorphism and AI systems or dispatch tables and that I don't know everything about C#. – Gabriel Aug 01 '21 at 18:58
  • @JonasH I'll draw for you to understand, imagine a class called "Class A", this "Class A" have a bunch of generic class in it, 5 or 100 classes, and i want to access every method inside every class because every class would have diferrent methods, the way that I will access these methods is irrelevant. – Gabriel Aug 01 '21 at 18:59
  • @OlivierRogier Yes it's pseudo code or whatever, this part of the code doesn't work, as said in "what I already tried". If found this in https://stackoverflow.com/questions/9808035/how-do-i-make-the-return-type-of-a-method-generic. – Gabriel Aug 01 '21 at 18:59

1 Answers1

3

If you want to use Indexers to look at the ListOfPowerClass class as an array, change the ListOfPowerClass class code as follows:

public class ListOfPowerClass
{
    public PowerClass<ISomePowerClass>[] powerClasses = new PowerClass<ISomePowerClass>[]
    {
        new PowerClass<ISomePowerClass>(new SomePowerClassOne(), false),
        new PowerClass<ISomePowerClass>(new SomePowerClassTwo(), false)
    };
        
    public PowerClass<ISomePowerClass> this[int index]
    {
        get => powerClasses[index];
        set => powerClasses[index] = value;
    }
       
    public class PowerClass<T> where T : class
    {
        public bool I_Have;
        public T Power;

        public PowerClass(T _Power, bool _Active)
        {
            Power = _Power;
            I_Have = _Active;
        }
    }
}

add interface ISomePowerClass and change SomePowerClassOne,SomePowerClassTwo to :

public interface ISomePowerClass
{
    void Print();
}
public class SomePowerClassOne : ISomePowerClass
{
    public void Print()
    {
       Console.WriteLine("This is the power one");
    }
}

public class SomePowerClassTwo : ISomePowerClass
{
    public void Print()
    {
       Console.WriteLine("This is the power two");
    }
}

now use

ListOfPowerClass PowerList = new ListOfPowerClass();
PowerList[0].Power.Print(); ///index = 0, I will access class PowerOne 
PowerList[1].Power.Print(); ///index = 1, I will access class PowerTwo
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
  • Since you always need to use ISomePowerClass as the generic argument there is no point using a generic class. – Taemyr Aug 01 '21 at 20:53
  • @Meysam Asadi Sorry now I realize that I didn't make it clear that "SomePowerClassOne" and "SomePowerClassTwo" doesn't have the same methods, what I was trying to say is that these two classes would have various methods. But could your solution still resolve in another way? – Gabriel Aug 01 '21 at 21:35
  • No, you can have any implementation within any of the "SomePowerClassOne" and "SomePowerClassTwo" classes, and only access to them is provided in my answer way. – Meysam Asadi Aug 01 '21 at 21:53
  • @MeysamAsadi Ok, I get it, but then I would have to leave the interface empty (Because there's no point in leaving a function inside it that I won't use), and from what I've read about interfaces, it's not a bad practice to let interfaces empty? – Gabriel Aug 01 '21 at 22:08
  • 1
    @Gabriel These are all design issues and you should try to create a clean and logical design. If the two classes have no common field and method, use the abstract class instead of the interface. Try to put the common features of the two classes in the parent class. – Meysam Asadi Aug 01 '21 at 22:19