Note that there are some explanatory texts on larger screens.

plurals
  1. POIn C#, how to instantiate a passed generic type inside a method?
    primarykey
    data
    text
    <p>How can I instantiate the type T inside my <code>InstantiateType&lt;T&gt;</code> method below?</p> <p>I'm getting the error: <strong>'T' is a 'type parameter' but is used like a 'variable'.</strong>:</p> <h2>(SCROLL DOWN FOR REFACTORED ANSWER)</h2> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestGeneric33 { class Program { static void Main(string[] args) { Container container = new Container(); Console.WriteLine(container.InstantiateType&lt;Customer&gt;("Jim", "Smith")); Console.WriteLine(container.InstantiateType&lt;Employee&gt;("Joe", "Thompson")); Console.ReadLine(); } } public class Container { public T InstantiateType&lt;T&gt;(string firstName, string lastName) where T : IPerson { T obj = T(); obj.FirstName(firstName); obj.LastName(lastName); return obj; } } public interface IPerson { string FirstName { get; set; } string LastName { get; set; } } public class Customer : IPerson { public string FirstName { get; set; } public string LastName { get; set; } public string Company { get; set; } } public class Employee : IPerson { public string FirstName { get; set; } public string LastName { get; set; } public int EmployeeNumber { get; set; } } } </code></pre> <h2>REFACTORED ANSWER:</h2> <p>Thanks for all the comments, they got me on the right track, this is what I wanted to do:</p> <pre><code>using System; namespace TestGeneric33 { class Program { static void Main(string[] args) { Container container = new Container(); Customer customer1 = container.InstantiateType&lt;Customer&gt;("Jim", "Smith"); Employee employee1 = container.InstantiateType&lt;Employee&gt;("Joe", "Thompson"); Console.WriteLine(PersonDisplayer.SimpleDisplay(customer1)); Console.WriteLine(PersonDisplayer.SimpleDisplay(employee1)); Console.ReadLine(); } } public class Container { public T InstantiateType&lt;T&gt;(string firstName, string lastName) where T : IPerson, new() { T obj = new T(); obj.FirstName = firstName; obj.LastName = lastName; return obj; } } public interface IPerson { string FirstName { get; set; } string LastName { get; set; } } public class PersonDisplayer { private IPerson _person; public PersonDisplayer(IPerson person) { _person = person; } public string SimpleDisplay() { return String.Format("{1}, {0}", _person.FirstName, _person.LastName); } public static string SimpleDisplay(IPerson person) { PersonDisplayer personDisplayer = new PersonDisplayer(person); return personDisplayer.SimpleDisplay(); } } public class Customer : IPerson { public string FirstName { get; set; } public string LastName { get; set; } public string Company { get; set; } } public class Employee : IPerson { public string FirstName { get; set; } public string LastName { get; set; } public int EmployeeNumber { get; set; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload