0

I want to return Table List with there class object and this call also generic, I don't want to use any Non-Strong type List like ArrayList on any other.

        public List<T> GetTables()
        {
            var tbls = new List<T>();
            tbls.Add(new Table<Table1>() { Name = "Table1"});
            tbls.Add(new Table<Table2>() { Name = "Table2"});
            tbls.Add(new Table<Table3>() { Name = "Table3"});
            return tbls;
        }

in above Method, Table1, Table2, and any type table class object... There classes has no any based class, These classes use to Set of table properties with custom format.

I want to return it.

Please help me if anyone have any idea.

thank you.

3
  • Do Table1, Table2 and Table3 have a common base type? Commented May 8, 2020 at 6:44
  • No, Table1, Table2 and Table3 is not Common based type. Commented May 8, 2020 at 6:46
  • If they don't all inherit from a common base type and it's not a List<CommonBaseType> then the above is not possible how you want it. Substitute "interface" for "base type" in my prior sentence if we're talking about interfaces. Commented May 8, 2020 at 6:52

2 Answers 2

2

If the only common thing of the list's components is that they are Table<> generic objects, if there isn't any interface or common base class between Table1/Table2/Table2, you can simply treat them as object:

        public List<Table<IGenerateData>> GetTables()
        {
            var tbls = new List<Table<IGenerateData>>();
            tbls.Add(new Table<Table1>() { Name = "Table1"});
            tbls.Add(new Table<Table2>() { Name = "Table2"});
            tbls.Add(new Table<Table3>() { Name = "Table3"});
            return tbls;
        }

        public interface IGenerateData
        {
            void GenerateData();
        }

        public class Table1 : IGenerateData
        {           
        }
Sign up to request clarification or add additional context in comments.

3 Comments

I want to use Strong type, not a Object type.
As you suggest on another comment, you need to access the method GenerateData() on Table1 ... Table3... so you can create an Interface IGenerateData with the method, and make Table1 ... Table3 implement that interface. Then your list could be List<Table<IGenerateData>>
Thanks @CicheR. Based on your idea, Now i can move forward. But still there is an one method i can't access based on above idea. "void GenerateData(T data)" But it not big deal, I can move forward without this method. Thank you again to both of you.
1

You can't use generic type with the List without specifying type parameter explicitly. Alternatively, you can create a base class or interface that would be a parameter for the List.

I mean:

        public static List<ITable> GetTables()
        {
            var tbls = new List<ITable>();
            tbls.Add(new Table<Table1> { Name = "Table1"});
            tbls.Add(new Table<Table2> { Name = "Table2"});
            tbls.Add(new Table<Table3> { Name = "Table3"});
            return tbls;
        }

        public class Table<T> : ITable
        {
            public T TableInstance { get; set; }
            public Type TableType => typeof(T);
            public string Name { get; set; }
        }

        public interface ITable
        {
            Type TableType { get; }
            public string Name { get; set; }
        }

5 Comments

Thank you @konstanin. but the issue is i still need to pass Table1, table to while accessing "TableInstance". I want this Direct access. I have idea is it possible or not.
i want like this. public void Run() { var lst = GetTables(); foreach (var item in lst) { item.TableInstance.GenerateData(); } }
@AshishSojitra - If you want to do that then you must have a common base type or interface.
Thanks @Konstantin-Dubrovnyi Based on your idea, Now i can move forward. But still there is an one method i can't access based on above idea. "void GenerateData(T data)" But it not big deal, I can move forward without this method. Thank you again to both of you.
@AshishSojitra then you may do the following: create base class or interface (ITableInstance) from Table1, Table2, Table3 with method GenerateData(), create List<ITable<ITableIntance>> and use it like you suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.