Setting different properties based on T in method that returns List<T>
-
Hi All, I have a method that returns List, T can be a number of types but based on the type I want set different properties. I can detect the type passed into the method and can switch on the name but I'm a bit lost on how to convert my List to list so the return type is correct. the error is Cannot implicitly convert type List to List which is understandable but how do I return List as List where T is type?
-
Hi All, I have a method that returns List, T can be a number of types but based on the type I want set different properties. I can detect the type passed into the method and can switch on the name but I'm a bit lost on how to convert my List to list so the return type is correct. the error is Cannot implicitly convert type List to List which is understandable but how do I return List as List where T is type?
Well, type (in your example), must be derived from type T, so the easiest way to tell it to do this would be to put a constraint on T in your declaration, e.g.
public List<T> MyMethod<T>(Type type) where T : MyBaseType
{
}Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Well, type (in your example), must be derived from type T, so the easiest way to tell it to do this would be to put a constraint on T in your declaration, e.g.
public List<T> MyMethod<T>(Type type) where T : MyBaseType
{
}Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Thanks Pete, I've tried putting constraints on the method but I'm still getting the error, I think I'm doing something fundamentally wrong. perhaps an example would help
public List getall()where T : employee
{
type t = typeof(T)
switch (t.name)
{
case "manager":
List all = this.GetManagers()
break;
default
List all = this.getSales()} return all
}
This is something along the lines of what I'm trying to do (I haven't tried compiling it but it's the types I have the issue with, not syntax)- Imagine both Sales and managers are of type employee but managers are paid monthly and do not get commission but sales are paid weekly and do get commission, so whilst they have much in common I want to treat them differently. Hope this makes more sense.
-
Thanks Pete, I've tried putting constraints on the method but I'm still getting the error, I think I'm doing something fundamentally wrong. perhaps an example would help
public List getall()where T : employee
{
type t = typeof(T)
switch (t.name)
{
case "manager":
List all = this.GetManagers()
break;
default
List all = this.getSales()} return all
}
This is something along the lines of what I'm trying to do (I haven't tried compiling it but it's the types I have the issue with, not syntax)- Imagine both Sales and managers are of type employee but managers are paid monthly and do not get commission but sales are paid weekly and do get commission, so whilst they have much in common I want to treat them differently. Hope this makes more sense.
You're starting to get there, but you need to cast to the appropriate list type as in:
public List<T> GetAll<T>() where T : Employee { List<T> all = new List<T>(); if (typeof(T) == typeof(Manager)) all = GetManagers() as List<T>; else all = GetSales() as List<T>; return all; }
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
You're starting to get there, but you need to cast to the appropriate list type as in:
public List<T> GetAll<T>() where T : Employee { List<T> all = new List<T>(); if (typeof(T) == typeof(Manager)) all = GetManagers() as List<T>; else all = GetSales() as List<T>; return all; }
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Ah yes, got it! Thank you very much I didn't think of casting the whole list. Much appreciated. Paul
-
Ah yes, got it! Thank you very much I didn't think of casting the whole list. Much appreciated. Paul
No problems. Glad to be of service.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Thanks Pete, I've tried putting constraints on the method but I'm still getting the error, I think I'm doing something fundamentally wrong. perhaps an example would help
public List getall()where T : employee
{
type t = typeof(T)
switch (t.name)
{
case "manager":
List all = this.GetManagers()
break;
default
List all = this.getSales()} return all
}
This is something along the lines of what I'm trying to do (I haven't tried compiling it but it's the types I have the issue with, not syntax)- Imagine both Sales and managers are of type employee but managers are paid monthly and do not get commission but sales are paid weekly and do get commission, so whilst they have much in common I want to treat them differently. Hope this makes more sense.
I'm curious what are you trying to achieve with a method like this. Since you are forced to specify an exact type at the point of call anyway, for example,
var managers = getall();
you might as well call a method with a different name, like this:
var managers = getAllManagers();
In other words, your getall method hides certain details, yet the caller is forced to reveal these details back! Could you please explain your intent?
-
I'm curious what are you trying to achieve with a method like this. Since you are forced to specify an exact type at the point of call anyway, for example,
var managers = getall();
you might as well call a method with a different name, like this:
var managers = getAllManagers();
In other words, your getall method hides certain details, yet the caller is forced to reveal these details back! Could you please explain your intent?
-
Thanks Pete, I've tried putting constraints on the method but I'm still getting the error, I think I'm doing something fundamentally wrong. perhaps an example would help
public List getall()where T : employee
{
type t = typeof(T)
switch (t.name)
{
case "manager":
List all = this.GetManagers()
break;
default
List all = this.getSales()} return all
}
This is something along the lines of what I'm trying to do (I haven't tried compiling it but it's the types I have the issue with, not syntax)- Imagine both Sales and managers are of type employee but managers are paid monthly and do not get commission but sales are paid weekly and do get commission, so whilst they have much in common I want to treat them differently. Hope this makes more sense.
Paul E Davies wrote:
List<manager> all = this.GetManagers()
List<T> all = this.GetManagers()
-
I'm curious what are you trying to achieve with a method like this. Since you are forced to specify an exact type at the point of call anyway, for example,
var managers = getall();
you might as well call a method with a different name, like this:
var managers = getAllManagers();
In other words, your getall method hides certain details, yet the caller is forced to reveal these details back! Could you please explain your intent?
I'm sure there are a number of ways to do it, this is the scenario I have been presented with. I have some big text files and depending on their origin will contain slightly differing sets of data. I have regex patterns that will return a collection of matches - each match describes an object of type T, Once I have the matches I know what type T is and can process the list on this basis. 90% of the processing is the same but differs slightly based on T. Hope that makes some sense.
-
I'm sure there are a number of ways to do it, this is the scenario I have been presented with. I have some big text files and depending on their origin will contain slightly differing sets of data. I have regex patterns that will return a collection of matches - each match describes an object of type T, Once I have the matches I know what type T is and can process the list on this basis. 90% of the processing is the same but differs slightly based on T. Hope that makes some sense.
Are you certain that you need a generic for this scenario? It sounds like a simpler
List<employee>
would do just fine, no? -
I'm sure there are a number of ways to do it, this is the scenario I have been presented with. I have some big text files and depending on their origin will contain slightly differing sets of data. I have regex patterns that will return a collection of matches - each match describes an object of type T, Once I have the matches I know what type T is and can process the list on this basis. 90% of the processing is the same but differs slightly based on T. Hope that makes some sense.