C# 泛型:如何在没有对象的情况下检查T的确切类型 我如何检查或评估T的确切类型而不为T对象?我知道我的问题可能混淆,但是考虑这个……/P> public abstract class Business { public abstract string GetBusinessName(); } public class Casino : Business { public override string GetBusinessName() { return "Casino Corp"; } } public class DrugStore : Business { public override string GetBusinessName() { return "DrugStore business"; } } public class BusinessManager<T> where T : Business { private Casino _casino; private DrugStore _drugStore; public string ShowBusinessName() { string businessName; if (T == Casino) // Error: How can I check the type? { _casino = new Casino(); businessName = _casino.GetBusinessName(); } else if (T == DrugStore) // Error: How can I check the type? { _drugStore = new DrugStore(); businessName = _drugStore.GetBusinessName(); } return businessName; } } 公共抽象类业务 { 公共抽象字符串GetBusinessName(); } 公营赌场:商务 { 公共重写字符串GetBusinessName() { 返回“赌场公司”; } } 公营药店:商业 { 公共重写字符串GetBusinessName() { 返回“药店业务”; } } 公共类业务经理,其中T:业务 { 私人赌场;; 私家药房(药房),; 公共字符串ShowBusinessName() { 字符串businessName; if(T==Casino)//错误:如何检查类型? { _赌场=新赌场(); businessName=_casino.GetBusinessName(); } else if(T==DrugStore)//错误:如何检查类型? { _药店=新药店(); businessName=_drugStore.GetBusinessName(); } 返回商业名称; } }

C# 泛型:如何在没有对象的情况下检查T的确切类型 我如何检查或评估T的确切类型而不为T对象?我知道我的问题可能混淆,但是考虑这个……/P> public abstract class Business { public abstract string GetBusinessName(); } public class Casino : Business { public override string GetBusinessName() { return "Casino Corp"; } } public class DrugStore : Business { public override string GetBusinessName() { return "DrugStore business"; } } public class BusinessManager<T> where T : Business { private Casino _casino; private DrugStore _drugStore; public string ShowBusinessName() { string businessName; if (T == Casino) // Error: How can I check the type? { _casino = new Casino(); businessName = _casino.GetBusinessName(); } else if (T == DrugStore) // Error: How can I check the type? { _drugStore = new DrugStore(); businessName = _drugStore.GetBusinessName(); } return businessName; } } 公共抽象类业务 { 公共抽象字符串GetBusinessName(); } 公营赌场:商务 { 公共重写字符串GetBusinessName() { 返回“赌场公司”; } } 公营药店:商业 { 公共重写字符串GetBusinessName() { 返回“药店业务”; } } 公共类业务经理,其中T:业务 { 私人赌场;; 私家药房(药房),; 公共字符串ShowBusinessName() { 字符串businessName; if(T==Casino)//错误:如何检查类型? { _赌场=新赌场(); businessName=_casino.GetBusinessName(); } else if(T==DrugStore)//错误:如何检查类型? { _药店=新药店(); businessName=_drugStore.GetBusinessName(); } 返回商业名称; } },c#,generics,C#,Generics,我只想在客户身上有这样的东西 protected void Page_Load(object sender, EventArgs e) { var businessManager = new BusinessManager<Casino>(); Response.Write(businessManager.ShowBusinessName()); businessManager = new BusinessManage

我只想在客户身上有这样的东西

    protected void Page_Load(object sender, EventArgs e)
    {
        var businessManager = new BusinessManager<Casino>();
        Response.Write(businessManager.ShowBusinessName());

        businessManager = new BusinessManager<DrugStore>();
        Response.Write(businessManager.ShowBusinessName());
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
var businessManager=新businessManager();
Response.Write(businessManager.ShowBusinessName());
businessManager=新businessManager();
Response.Write(businessManager.ShowBusinessName());
}
请注意,在调用BusinessManager时,我实际上并没有为Casino和Drugstore创建实际对象,我只是将其作为类的泛型类型约束传递。我只需要知道我传递给BusinessManager的确切类型,就可以知道要实例化的确切类型。谢谢

注:我不想为赌场和药店创建单独的特定业务经理

您也可以对设计进行评论。。谢谢

附加:如果Casino and DrugStore类是一个抽象类=)您可以编写

if(typeof(T) == typeof(Casino))
但实际上,这种逻辑是一种代码味道

有一种方法可以解决这个问题:

public class BusinessManager<T> where T : Business, new() {
    private readonly T business;
    public BusinessManager() {
        business = new T();
    }
}
公共类BusinessManager,其中T:Business,new(){
私人只读业务;
公共业务经理(){
业务=新T();
}
}
但就我个人而言,我更喜欢

public class BusinessManager<T> where T : Business {
    private readonly T business;
    public BusinessManager(T business) {
        this.business = business;
    }

    public string GetBusinessName() { 
        return this.business.GetBusinessName();
    }
}
公共类BusinessManager,其中T:Business{
私人只读业务;
公共业务经理(T业务){
这就是生意;
}
公共字符串GetBusinessName(){
返回此.business.GetBusinessName();
}
}

您可以执行以下操作:

if (typeof(T) == typeof(SomeType))
{
    // Same
}
IBusiness casino = new Casino();
Response.Write(casino.Name);

IBusiness drugStore = new DrugStore();
Response.Write(drugStore.Name);
你应该这样做

public class BusinessManager<T> where T : Business, new()
...

T _business = new T();
string businessName = _business.GetBusinessName();
return businessName;
公共类BusinessManager,其中T:Business,new()
...
T_business=新T();
字符串businessName=_business.GetBusinessName();
返回商业名称;
我不知道C#语法,但不可能做到:

public class BusinessManager<T> where T : Business, new()
    {
        private T _business;

        public string ShowBusinessName()
        {
            string businessName;
            _business = new T();
            return _business.GetBusinessName();
        }
    }
公共类BusinessManager,其中T:Business,new()
{
私人信托事业;
公共字符串ShowBusinessName()
{
字符串businessName;
_业务=新T();
return_business.GetBusinessName();
}
}

业务经理
类定义如下:

public class BusinessManager<T> where T : Business
{ 
    Business biz;
    public BusinessManager()
    {
        biz = new T();
    }

    public string ShowBusinessName()
    {
        return biz.GetBusinessName();
    }
}
    var businessManager = new BusinessManager<Casino>();
    Response.Write(businessManager.ShowBusinessName());

    var anotherBusinessManager = new BusinessManager<DrugStore>();
    Response.Write(businessManager.ShowBusinessName());
公共类BusinessManager,其中T:Business
{ 
商业;
公共业务经理()
{
biz=newt();
}
公共字符串ShowBusinessName()
{
return biz.GetBusinessName();
}
}
并将其用作以下命令:

public class BusinessManager<T> where T : Business
{ 
    Business biz;
    public BusinessManager()
    {
        biz = new T();
    }

    public string ShowBusinessName()
    {
        return biz.GetBusinessName();
    }
}
    var businessManager = new BusinessManager<Casino>();
    Response.Write(businessManager.ShowBusinessName());

    var anotherBusinessManager = new BusinessManager<DrugStore>();
    Response.Write(businessManager.ShowBusinessName());
var businessManager=new businessManager();
Response.Write(businessManager.ShowBusinessName());
var anotherBusinessManager=newbusinessmanager();
Response.Write(businessManager.ShowBusinessName());

您使用you的方式将失去封装性

因为其他人已经给出了您第一个问题的各种答案,我想谈谈第二个问题:设计

1。
业务经理的角色

在您的示例中,
BusinessManager
类的实际角色不太清楚。因为这个类是泛型的,它不应该关心
t
的实际类型,所以它只不过是在业务类和程序的其余部分之间添加了另一个不必要的层

换句话说,您可以简单地使用:

Business casino = new Casino();
Response.Write(casino.GetBusinessName());

Business drugStore = new DrugStore();
Response.Write(drugStore.GetBusinessName());
将其包装到另一个泛型类中对您没有多大帮助。另一方面,如果希望所有这些类都具有一些通用功能,可以直接将其添加到抽象类中,或者提取接口并为该接口创建扩展方法

2。为getter使用属性

第二件事,当您有一个简单的getter方法时,使用属性更合适。换句话说,您应该将
GetBusinessName()
方法替换为
Name
属性(我还从名称中省略了“Business”,因为它不是必需的:

public interface IBusiness
{
    string Name { get; }
}

public abstract class Business : IBusiness
{
    public abstract string Name { get; }
}

public class Casino : Business  
{
    public override string Name
    {
        get { return "Casino Corp"; }
    }
}

public class DrugStore : Business 
{
    public override string Name
    {
        get { return "DrugStore business"; }
    }
}
然后你可以这样使用它:

if (typeof(T) == typeof(SomeType))
{
    // Same
}
IBusiness casino = new Casino();
Response.Write(casino.Name);

IBusiness drugStore = new DrugStore();
Response.Write(drugStore.Name);
另外,您可以看到,我引入了一个
IBusiness
接口。这样做的原因是允许您以更多样化的方式实现此接口。现在,您将尝试从抽象
业务
类派生所有类,并尝试提取抽象类中尽可能多的公共功能(这就是这门课的目的)

但是提取大量常用功能需要付出代价:总是有可能需要创建一个不是从
Business
派生的类。如果您是通过
IBusiness
接口访问所有这些方法,那么程序的其他部分就不会在意这一点实现是否源于
业务