如何在下面的InstantiateType<T>方法中实例化类型T?
我得到了一个错误:'T‘是一个’类型参数‘,但被用作’variable‘。
(向下滚动查看REFACTORED答案)
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<Customer>("Jim", "Smith"));
Console.WriteLine(container.InstantiateType<Employee>("Joe", "Thompson"));
Console.ReadLine();
}
}
public class Container
{
public T InstantiateType<T>(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; }
}
}REFACTORED答案:
感谢所有的评论,他们让我走上了正确的道路,这是我想要做的:
using System;
namespace TestGeneric33
{
class Program
{
static void Main(string[] args)
{
Container container = new Container();
Customer customer1 = container.InstantiateType<Customer>("Jim", "Smith");
Employee employee1 = container.InstantiateType<Employee>("Joe", "Thompson");
Console.WriteLine(PersonDisplayer.SimpleDisplay(customer1));
Console.WriteLine(PersonDisplayer.SimpleDisplay(employee1));
Console.ReadLine();
}
}
public class Container
{
public T InstantiateType<T>(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; }
}
}发布于 2009-03-18 16:15:29
像这样声明你的方法:
public string InstantiateType<T>(string firstName, string lastName)
where T : IPerson, new()请注意末尾的附加约束。然后在方法体中创建一个new实例:
T obj = new T(); 发布于 2009-03-18 16:18:39
有几种方法。
如果不指定类型,则必须具有构造函数:
T obj = default(T); //which will produce null for reference types使用构造函数:
T obj = new T();但这需要以下子句:
where T : new()发布于 2009-03-18 16:25:36
为了扩展上面的答案,将where T:new()约束添加到泛型方法将要求T具有公共的、无参数的构造函数。
如果您想避免这种情况-在工厂模式中,有时会强制其他方法通过您的工厂方法,而不是直接通过构造函数-那么另一种选择是使用反射(Activator.CreateInstance...)并保持默认构造函数为私有。当然,这也会带来性能损失。
https://stackoverflow.com/questions/658951
复制相似问题