C# 获取泛型类型的实例

C# 获取泛型类型的实例,c#,generics,C#,Generics,我有两个Customer类BusinessCustomer和NormalCustomer,它们有各自的属性和一个Validate方法。从implement类中,根据某些条件,我可以创建Customer1或Customer2。 如何基于Customer类中的T创建BusinessCustomer或NormalCustomer的实例,以便调用这两个类通用的validate方法 public class Customer<T> where T : class {

我有两个Customer类BusinessCustomer和NormalCustomer,它们有各自的属性和一个Validate方法。从implement类中,根据某些条件,我可以创建Customer1或Customer2。 如何基于Customer类中的T创建BusinessCustomer或NormalCustomer的实例,以便调用这两个类通用的validate方法

    public class Customer<T> where T : class
    {
        public T CustomerType;
        bool isValid;

        public Customer() //constructor
        {
            //for customer1, i want a new instance of BusinessCustomer 
            //for customer2, i want a new instance of NormalCustomer 
        }

        public bool Validate()
        {
            isValid = CustomerType.Validate();
        }
    }


public class BusinessCustomer
{
    public string CustomerHobby { get; set; }
    public bool Validate()
    {
        return true;
    }
}

public class NormalCustomer
{
    public string CustomerEducation { get; set; }
    public bool Validate()
    {
        return false;
    }
}

public class Implement
{
    public void ImplementCustomer()
    {
        var customer1 = new Customer<BusinessCustomer>();
        customer1.CustomerType = new BusinessCustomer {CustomerHobby="Singing"};
        customer1.Validate();

        var customer2 = new Customer<NormalCustomer>();
        customer2.CustomerType = new NormalCustomer { CustomerEducation = "High School" };
        customer2.Validate();

    }
}
公共类客户,其中T:class
{
公共T客户类型;
bool是有效的;
public Customer()//构造函数
{
//对于customer1,我需要BusinessCustomer的新实例
//对于customer2,我需要一个NormalCustomer的新实例
}
公共bool验证()
{
isValid=CustomerType.Validate();
}
}
公营业务客户
{
公共字符串CustomerHobby{get;set;}
公共bool验证()
{
返回true;
}
}
公共类普通客户
{
公共字符串自定义教育{get;set;}
公共bool验证()
{
返回false;
}
}
公共类实现
{
公共客户()
{
var customer1=新客户();
customer1.CustomerType=新业务客户{CustomerHobby=“Singing”};
customer1.Validate();
var customer2=新客户();
customer2.CustomerType=新普通客户{CustomerEducation=“High School”};
customer2.Validate();
}
}

您的第一个问题是以下内容:

isValid = CustomerType.Validate();
由于
CustomerType
的类型为
T
,可以是任何类,因此编译器不能保证调用
Validate()
方法。您需要通过创建一个公共接口来解决这个问题。让我们称之为
iccustomer

interface ICustomer
{
   bool Validate();
}
现在,
BusinessCustomer
NormalCustomer
都需要实现上述接口:

public class BusinessCustomer : ICustomer
{
   // Same code
}

public class NormalCustomer : ICustomer
{
   // Same code
}
接下来,您必须更改:

public class Customer<T> where T : class
但是等等。如果
T
没有默认的公共构造函数,或者是抽象的,该怎么办?我们还需要将此约束添加到泛型类型:

public class Customer<T> where T : class, new()
要么需要返回bool(例如
isValid
),要么签名需要是
void Validate()
。现在,您将得到一个编译器错误,因为并非所有代码路径都返回一个值


希望这有帮助

我还没来得及写答案,问题就解决了,但基本上您希望
BusinessCustomer
NormalCustomer
都实现一个接口,
iccustomer
,该接口有一个
Validate
方法。然后将
Customer where T:class
更改为
Customer where T:iccustomer
。此时,您可以在
CustomerType
上调用
Validate
,因为编译器现在可以知道该方法存在了。@Mike,我遗漏了什么吗?在我看来,OP希望澄清如何完成他在构造函数中的评论,即:“
//对于customer1,我需要一个新的BusinessCustomer实例”
”显然,答案可以在通过副本引用的答案中找到。@KirkWoll-我很难理解这个问题,但从我所能看出,他们希望能够在
CustomerType
上调用
Validate()
,这需要
T
共享一个公共接口。但是,是的,他们似乎也希望在构造函数中创建该类型的默认实例,即使他们已经在
ImplementCustomer
中这样做了。我宁愿这个问题以不清楚的方式结束。@Mike,我明白你的意思,并重新开始这个问题。请随意尝试。:)@柯克沃尔-谢谢!可能花了太多时间在这上面了。
public Customer()
{
   CustomerType = new T();
}
public class Customer<T> where T : class, new()
public bool Validate()
{
   isValid = CustomerType.Validate();
}