I want to do all of the same code twice, but just for two different objects. I am getting these objects from a SOAP API (rather outdated one). I want to avoid writing duplicate code over and over again. I'll be taking my original object and manipulating it a lot (all the same way) the only difference is that the object type is the difference, so that's why I need to make a generic object. I want it to return the same type as it takes as a parameter. I am having an issue that if I do code like this
public static class ParamsHelper<T>
{
public static async Task<T[]> Whatever(T[] rptParams)
{
//do some stuff to rptparams
return rptParams;
}
}
// then I call it like this below:
var params = await ParamsHelper.Whatever<ItemP[]>(new ItemP[]{});
// it says can't convert type ItemP[] to type ItemP[][].
Additionally, I am using LINQ to do all of the manipulating. I would love advice on how to access the object fields (maybe reflection?)
You have to declare the generic type on the method.
public static async Task<T[]> Whatever<T>(T[] rptParams)
The usage generally is implied from usage so you don't have to pass it.
if you do have to pass it, dont make it an array.
ParamsHelper.Whatever<ItemP>(new ItemP[] { });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.