-3

I need to pass type as an argument to a generic class. I am trying to get the type from list of types. Example:

 void Main()
{
    var test = new Test();
    test.testMethod();
}

public static class ListClass<T>
{
   public static bool getValues()
   {
       return true;
   }
}

public class X { public int a; public int b; }

public class Y { public string s; public float f; }

class Test
{
    List<Type> listType = new List<Type>();

    public Test()
    {
       listType.Add(typeof(X));
       listType.Add(typeof(Y));
    }

    public void testMethod()
    {
       Console.WriteLine(ListClass<X>.getValues());
       Console.WriteLine(ListClass<Y>.getValues());
    }
}

I want to loop the calls instead of calling in each line.

K M Chaudhary
  • 79
  • 1
  • 8
  • Could you please provide a more meaningful example?What is `listClass`? – MakePeaceGreatAgain Jun 21 '18 at 13:32
  • listClass is a generic class which accpets different class to its methods. – K M Chaudhary Jun 21 '18 at 13:37
  • Please provide some more information or a simple example on what you are trying to achieve? – sharmav1 Jun 21 '18 at 13:56
  • This link should give you the answer: https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method – MakePeaceGreatAgain Jun 21 '18 at 13:58
  • I am trying to load all the class names in an array which is List listType; and then trying to pass it as argument to a generic class which is listClass.getValues(); – K M Chaudhary Jun 21 '18 at 14:09
  • @HimBromBeere I have looked at the solution that you have provided, the solution does not specify how to assign variable as generic type. Thanks for the the help and let me know if you find other solution. – K M Chaudhary Jun 21 '18 at 14:23
  • @KMChaudhary - You need to provide us much code as you can - type defs, initial set ups, etc. The first line doesn't instantiate a list. The second and third add classes `x` and `y` that you haven't provided types for. Finally you have introduced `listClass` without giving us the class definition. You haven't explained what a call to `listClass.getValues()` should do or how that would be even defined. I understand that `listClass.getValues()` doesn't work, but surely you could post how `ListClass.getValues()` should work with working code. – Enigmativity Jun 21 '18 at 14:31
  • @Enigmativity Class Test { List listType; public Test() { listType.Add(typeof(x)); listType.Add(typeof(y)); } public void TestMethod(){ for (int i=0;i<2;i++) { ListClass.getValues();}}. I hope this helps – K M Chaudhary Jun 21 '18 at 14:39
  • @KMChaudhary - Please don't post as a comment. Edit your question. – Enigmativity Jun 21 '18 at 14:53
  • @KMChaudhary - Also that code is no better than your question. Please post code that works - but instead of calling `ListClass < listType[i] >.getValues()` you show working code for `ListClass.getValues();` for some type `T`. You need to post a [mcve]. – Enigmativity Jun 21 '18 at 14:55
  • @Enigmativity my apology as I have not used stackoverflow much. I have edited my question hope it helps – K M Chaudhary Jun 21 '18 at 15:36
  • @KMChaudhary - No, maybe I'm not explaining it well enough. Post code that works for `ListClass.getValues()` then we can help you with how to call `ListClass.getValues()`. You have not given us enough detail to help you so far. – Enigmativity Jun 21 '18 at 22:53
  • @KMChaudhary - I want to see the input, output, and implementation of `ListClass.getValues()`. Not just the superficial code you've posted. – Enigmativity Jun 21 '18 at 23:01
  • Possible duplicate of [How do I use reflection to call a generic method?](https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method) – thehennyy Jun 22 '18 at 06:04
  • @Enigmativity I hope the explanation now helps you. This is maximum I can explain you as it is not possible to paste 1000 lines code here. Anywyas thanks – K M Chaudhary Jun 22 '18 at 08:46
  • @thehennyy I had seen that, basically i need to pass type as parameter dynamically using some sort of array or list. – K M Chaudhary Jun 22 '18 at 08:51
  • @KMChaudhary - I don't want to see `ListClass` in your code. I want something that I can copy, paste and run. Then when I see what you want with `ListClass.getValues` I can then tell you how to do it with `ListClass`. Do you understand what I'm asking for? – Enigmativity Jun 22 '18 at 08:52

1 Answers1

0

Let's assume, for the sake of the argument, that the code you posted in your question, as per my request in the comments, was this:

void Main()
{
    var test = new Test();
    test.testMethod();
}

public static class ListClass<T>
{
    public static bool getValues()
    {
        return true;
    }
}

public class X { public int a; public int b; }

public class Y { public string s; public float f; }

class Test
{
    List<Type> listType = new List<Type>();

    public Test()
    {
        listType.Add(typeof(X));
        listType.Add(typeof(Y));
    }

    public void testMethod()
    {
        Console.WriteLine(ListClass<X>.getValues());
        Console.WriteLine(ListClass<Y>.getValues());
    }
}

That's basically code that will compile and will run. So now you want to know how to actually run this illegal code:

public void testMethod()
{
    foreach (var type in listType)
    {
        Console.WriteLine(ListClass<type>.getValues());
    }
}

Here's how:

public void testMethod()
{
    foreach (var type in listType)
    {
        Console.WriteLine(
            (bool)typeof(ListClass<>)
                .MakeGenericType(type)
                .GetMethod("getValues")
                .Invoke(null, new object[] { }));
    }
}

Now I don't know if this is the code you need because you didn't post the example that I was asking for. Nevertheless, I hope this helps.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • It should work and you have understood it perfectly. After I read that it was not possible to create dynamic type I have created another approach. I was getting local variable used as Type. I had tried reflection but it did not help. Anyways thank you and will keep you updated. Cheers!! – K M Chaudhary Jun 22 '18 at 10:21
  • @KMChaudhary - Could you please now update your question to show your code in the same manner that I showed mine at the start of this answer? – Enigmativity Jun 22 '18 at 10:24