Gabriel
Gabriel

Reputation: 35

How to access a generic class method inside another class by index or some better way?

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.

Upvotes: 0

Views: 442

Answers (1)

persian-theme
persian-theme

Reputation: 6638

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

Upvotes: 3

Related Questions