2

I want to create a method that will return a Response<> object and set the generic properties based on the type of the object passed in as a parameter. General code example below and additional considerations at the bottom of the post.

public class Response<T>
{
  public string ResponseCode { get; get; }
  public T Object { get; set;}
  public List<T> Objects { get; set; }

  public Response()
  {

  }
}

public class ChildrenResponses
{
  public ChildrenResponses() // constructor
  {

  }

  // some properties
}

public class AdultResponses
{
  public AdultResponses() // constructor
  {

  }

  // some properties
}

public Response<T> GetSpecificResponseType<T>(T genericType)
{
  // ... some logic to instantiate a Response<> object using the parameter type passed into the method.

  // ... some logic to set the generic properties of Response<> object based on the parameter type passed into the method.

  // return object
}
  1. I'm new to C# generics/reflection :)
  2. Being as the Response/ChildrenResponse/AdultResponse classes existed in this code base before I began using it, I'd like to avoid changing those classes.
  3. I've attempted multiple methods to accomplish this task, but none of them worked and providing them in this post would likely just add confusion. Instead, I provided a base method with the general idea I was working with.

An example of how I'd like to call the method:

Response<ChildrenResponses> result = GetSpecificResponseType<ChildrenResponses>("ChildrenResponses");

This method will then return the Response< ChildrenResponses > object with the Object property set to new ChildrenResponses() and Objects set to new List< ChildrenResponses >(). No properties within the ChildrenResponses need to be established - just the instantiating the object is enough.

2
  • 2
    The signature you are using in your example of how you'd like to call the method doesn't match the signature of your method. Commented May 12, 2017 at 15:41
  • Note ChildrenResponses and AdultResponses have nothing in common here. They should both be implenting a common interface or derive from a base class in order to be used as T in a generic class. Commented May 12, 2017 at 16:08

2 Answers 2

3

All you need to do is restrict your generic method to require a type with a parameterless constructor. Then you should be able to do this:

public Response<T> GetSpecificResponseType<T>() where T : new()
{
    return new Response<T>()
    {
        Object = new T(),
        Objects = new List<T>()
    }
}

Call like this:

Response<ChildrenResponses> result = GetSpecificResponseType<ChildrenResponses>();
Sign up to request clarification or add additional context in comments.

1 Comment

How would you call this method? var result = GetSpecificResponseType<ChildrenResponses>();
1

I think what you are missing is an interface. It is kind of hard to tell exactly what you are trying to do, so here is the way I interpreted the question:

public class Response<T> where T: IRepsonses, new()
{
    public string ResponseCode { get; set; }
    public T Object { get; set; }
    public List<T> Objects { get; set; }

    public Response()
    {

    }
}

public interface IRepsonses
{
}

public class ChildrenResponses : IRepsonses
{
    public ChildrenResponses() // constructor
    {

    }
    public string ChildSays { get; set; }
    // some properties
}

public class AdultResponses : IRepsonses
{
    public AdultResponses() // constructor
    {

    }
    public string AdultSays { get; set; }
    // some properties
}

class Program
{
    public static Response<T> GetSpecificResponseType<T>() where T: IRepsonses, new()
    {
        // ... some logic to instantiate a Response<> object using the parameter type passed into the method.

        // ... some logic to set the generic properties of Response<> object based on the parameter type passed into the method.

        // return object

        T obj = new T();
        return new Response<T>()
        {
            Object=obj,
            Objects=new List<T>()
        };
    }
    static void Main(string[] args)
    {
        var resp = GetSpecificResponseType<AdultResponses>();
        var adult = resp.Object.AdultSays;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.