troubleshooting Question

Making a method more generic with T and interface

Avatar of David DB
David DB asked on
C#
1 Comment1 Solution50 ViewsLast Modified:
Hi.

I have a method in C#  that I would like to be more generic.

public List<T> Test<T>(DateTimeOffset afterDate) where T:IType
      {
         string afterCursor = null;
         T data;
         bool morePages = false;
         List<T> retData = new List<T>();

         do
         {
            data = GetData<T>(afterCursor);
            if (data != null)
            {
               foreach (var item in data.Orders.Edges)
               {
                  afterCursor = item.Cursor;
                  retData.Add(item.Node);
               }
               morePages = data.Orders.PageInfo.HasNextPage;
            }
            else return null;
         } while (morePages);
         return retData;
      }

The IType interface is a collection of Orders, Customers and Products classes.

PS: The reason for using the Interface is to restrict the T types. If there is a better way, please let me know.  

The class structure on the top is the same, except for the property Orders. It's called Customers in Customers, and Products in Products. So the data.Orders.Edges will be data.Customers.Edges for Customers, and data.Products.Edges for Products.

So the name after data is the only thing that will change in this method. The rest is the same (Edges, PageInfo etc)

How can I accomplish this  so that it can be used for Orders, Customers and Products?

I know about checking if T is Orders, Customers or Products.
ASKER CERTIFIED SOLUTION
David DB

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros