3

I am fairly certain this is possible, and I am thinking it is easy and I just don't know the right way to ask the question in Google. What I want to do is take pass a type into a method and return back a list of objects of that type. something like this:

public List<T> GetComponentsOfType(Type thisType)
{
    return Components.OfType<thisType>().ToList();
}

which of course doesn't compile because I don't know what I am doing.

1 Answer 1

3

I guess you want to pass in the type parameter so you can get the typed list:

public List<T> GetComponentsOfType<T>()
{
    return Components.OfType<T>().ToList();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I was so close. =) -- do you know what the technical terms are in here, i.e. what I could have searched for in Google to find this answer?
The first effective one I found was method pass in type return c#. First but is decent.

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.