C# 将T限制为string和int?
我自己构建了一个通用的集合类,定义如下C# 将T限制为string和int?,c#,class,generics,types,C#,Class,Generics,Types,我自己构建了一个通用的集合类,定义如下 public class StatisticItemHits<T>{...} 不会编译。我做错了什么?使用“where”不可能做到这一点。你不能做“或” 无法使用“where”执行此操作。你不能做“或” 类型限制用于接口。您的示例建议您允许继承int和string的类,这有点胡说八道。我建议您设计一个包含将在泛型类StatistististItemHits中使用的方法的接口,并将该接口用作限制。但是,我在这里看不到您的需求,也许您可以发布一些
public class StatisticItemHits<T>{...}
不会编译。我做错了什么?使用“where”不可能做到这一点。你不能做“或” 无法使用“where”执行此操作。你不能做“或” 类型限制用于接口。您的示例建议您允许继承int和string的类,这有点胡说八道。我建议您设计一个包含将在泛型类StatistististItemHits中使用的方法的接口,并将该接口用作限制。但是,我在这里看不到您的需求,也许您可以发布一些关于您的场景的详细信息?类型限制旨在与接口一起使用。您的示例建议您允许继承int和string的类,这有点胡说八道。我建议您设计一个包含将在泛型类StatistististItemHits中使用的方法的接口,并将该接口用作限制。但是我在这里看不到您的需求,也许您可以发布一些关于您的场景的详细信息?您不能从where子句中将其限制为字符串和int。您可以在构造函数中检查它,但这可能不是检查的好地方。我的方法是专门化类并将类创建抽象为(半)工厂模式:
class MyRestrictedGeneric<T>
{
protected MyRestrictedGeneric() { }
// Create the right class depending on type T
public static MyRestrictedGeneric<T> Create<T>()
{
if (typeof(T) == typeof(string))
return new StringImpl() as MyRestrictedGeneric<T>;
if (typeof(T) == typeof(int))
return new IntImpl() as MyRestrictedGeneric<T>;
throw new InvalidOperationException("Type not supported");
}
// The specialized implementation are protected away
protected class StringImpl : MyRestrictedGeneric<string> { }
protected class IntImpl : MyRestrictedGeneric<int> { }
}
类MyRestrictedGeneric
{
受保护的MyRestrictedGeneric(){}
//根据类型T创建正确的类
公共静态MyRestrictedGeneric Create()
{
if(typeof(T)=typeof(string))
将新的StringImpl()作为MyRestrictedGeneric返回;
if(typeof(T)=typeof(int))
以MyRestrictedGeneric返回新的IntImpl();
抛出新的InvalidOperationException(“不支持类型”);
}
//专门的实现受到保护
受保护类StringImpl:MyRestrictedGeneric{}
受保护类IntImpl:MyRestrictedGeneric{}
}
通过这种方式,您可以将类的使用限制为仅在类内部使用string和int。您不能从where子句将其限制为string和int。您可以在构造函数中检查它,但这可能不是检查的好地方。我的方法是专门化类并将类创建抽象为(半)工厂模式:
class MyRestrictedGeneric<T>
{
protected MyRestrictedGeneric() { }
// Create the right class depending on type T
public static MyRestrictedGeneric<T> Create<T>()
{
if (typeof(T) == typeof(string))
return new StringImpl() as MyRestrictedGeneric<T>;
if (typeof(T) == typeof(int))
return new IntImpl() as MyRestrictedGeneric<T>;
throw new InvalidOperationException("Type not supported");
}
// The specialized implementation are protected away
protected class StringImpl : MyRestrictedGeneric<string> { }
protected class IntImpl : MyRestrictedGeneric<int> { }
}
类MyRestrictedGeneric
{
受保护的MyRestrictedGeneric(){}
//根据类型T创建正确的类
公共静态MyRestrictedGeneric Create()
{
if(typeof(T)=typeof(string))
将新的StringImpl()作为MyRestrictedGeneric返回;
if(typeof(T)=typeof(int))
以MyRestrictedGeneric返回新的IntImpl();
抛出新的InvalidOperationException(“不支持类型”);
}
//专门的实现受到保护
受保护类StringImpl:MyRestrictedGeneric{}
受保护类IntImpl:MyRestrictedGeneric{}
}
通过这种方式,您可以将类的使用限制为仅在类内部使用string和int。您可以将
statistististictemhits
作为一个抽象类,并创建两个子类:
statististitemHitsInt:statististitemHits{}
statististitemHitsString:statististitemHits{}
这样,statistististitemHits只能有int和string表示形式您可以将
statistististitemHits
作为一个抽象类,并创建两个子类:
statististitemHitsInt:statististitemHits{}
statististitemHitsString:statististitemHits{}
这样一来,统计学家只能用int和string表示,正如其他人所说,不能用这种方式使用类型限制。您可以做的是根据您的基本类型进行以下专业化:
public class StatisticItemHits <T> { }
public class StringStatisticItemHits : StatisticItemHits<string> { }
public class IntegerStatisticItemHits : StatisticItemHits<int> { }
公共类统计学家{}
公共类StringStatististItemHits:StatististististItemHits{}
公共类IntegerStatisticItemHits:StatisticItemHits{}
另外,由于您的基类是泛型的,所以您将无法使用它来进行多态性分析。如果您需要这样做,请让统计学家实现一个接口,并使用该接口引用实例。比如:
public class StatisticItemHits <T> : IStaticticItemHits { }
public interface IStatisticItemHits { }
公共类统计学家ItemHits:IStaticItemHits{}
公共接口IStatisticityItemHits{}
正如其他人所说,您不能以这种方式使用类型限制。您可以做的是根据您的基本类型进行以下专业化:
public class StatisticItemHits <T> { }
public class StringStatisticItemHits : StatisticItemHits<string> { }
public class IntegerStatisticItemHits : StatisticItemHits<int> { }
公共类统计学家{}
公共类StringStatististItemHits:StatististististItemHits{}
公共类IntegerStatisticItemHits:StatisticItemHits{}
另外,由于您的基类是泛型的,所以您将无法使用它来进行多态性分析。如果您需要这样做,请让统计学家实现一个接口,并使用该接口引用实例。比如:
public class StatisticItemHits <T> : IStaticticItemHits { }
public interface IStatisticItemHits { }
公共类统计学家ItemHits:IStaticItemHits{}
公共接口IStatisticityItemHits{}
鉴于这里只有两种类型,我将在这里采用面向对象的方法,为这两种类型创建两个类
泛型最好用在可以应用的情况下,你知道,泛型。在这种情况下,它们的用处要小得多
您可以仅限于结构或类类型,我确实认为需要有数字或基于运算符的限制(例如,必须支持+=)
Int和string真的很不同,当然比Int和double更不同。如果泛型类支持不可变引用类型string和值类型int,而不支持其他更类似于这两种类型的类型,那就没有多大意义 如果这里只有两种类型,我将采用面向对象的方法,只为这两种类型创建两个类 仿制药是最好的选择