-1

Is it possible to join two types together (like doing .AddRange in a list) using for example reflection? Something like in this reponse https://stackoverflow.com/a/8702650/13112059, but here the data from the target object gets replaced.

public class ItemsResponse
{
    public List<Items> Items{ get; set; } = new List<Items>();
}

In my case I receive a response from an API and cast the data to this type for example which is only defined at runtime, but then I also need to do another request where I receive data from the same type and I wanted to join this objects together to return them as one.

Example of a request:

public static async Task<T> Query()
{
    var response = new T();
    var response2 = new T();
    response = (T)await RequestAsync(); // First request
    /// ...
    response2 = (T)await RequestAsync(); // Second request
   // I want to return an object containing data from the two responses
}

Thanks!

João Fernandes
  • 75
  • 1
  • 10
  • What do you mean by "cast the data to this type for example which is only defined at runtime"? I don't really understand how that is supposed to work. – JonasH Nov 08 '22 at 14:52
  • My bad, I added an example. In my real code I have that call twice and I wanted to somehow have both results in only one object. – João Fernandes Nov 08 '22 at 14:55
  • given the code there must be `MyClass.Query()` somwhere. Then do something like `MyClass.Query().Concat(MyClass.Query())` – Firo Nov 08 '22 at 15:13
  • Yes there is, but I want to return the complete object from this method. Maybe now is more explicit @Firo – João Fernandes Nov 08 '22 at 15:15
  • if you just want to get an object back containing two responses then either build a type that matches your data shape or return the two objects as a `Tuple` - that way you can get both responses when querying. – Charleh Nov 08 '22 at 15:20

1 Answers1

1

A generic type parameter, i.e. <T> is not a runtime type. The type is still statically known, just not by this particular piece of code. Sooner or later you have to supply an actual type for your T, this might be done in some third party code, but it will still be known at compile time. Ofc, it might be defined at runtime, but that is not really relevant here.

If you want to create a list of responses you just need to include the generic type parameter in any classes or methods that handles these types.

public static List<T> MakeRequests<T>(MyRequestObject obj){

    var items = new List<T>();
    items.Add(obj.Query());
    // Do more queries, or whatever else you want
    

    return items;
}

You cannot really use the returned objects from your query, other than putting them in a collection, or converting them to string or object. So generic code like this is typically limited to fairly simple things that needs to be done regardless of the actual type.

Collections may include things like tuples, i.e. (T Request1, T Request2), but you cannot combine two object into an object of the same type, at least not without a horrible amount of reflection.

JonasH
  • 28,608
  • 2
  • 10
  • 23