草庐IT

泛型约束

全部标签

c# - 关于 C# 4.0 泛型协变的问题

定义了这个接口(interface):publicinterfaceIInputBoxService{boolShowDialog();TResult{get;}}为什么下面的代码有效:publicclassStringInputBoxService:IInputBoxService{...}...IInputBoxServiceservice=newStringInputBoxService();这不是吗?publicclassIntegerInputBoxService:IInputBoxService{...}...IInputBoxServiceservice=newInteg

c# - 为什么在通用序列创建器上使用 Func<> 比使用 new() 约束快得多

考虑以下代码...在我对Windows7x64PC(Inteli73GHz)上的RELEASE(不是调试!)x86构建的测试中,我获得了以下结果:CreateSequence()withnew()took00:00:00.9158071CreateSequence()withcreator()took00:00:00.1383482CreateSequence()withnew()took00:00:00.9198317CreateSequence()withcreator()took00:00:00.1372920CreateSequence()withnew()took00:00:

c# - 空接口(interface) vs 属性,通用约束怎么样?

我有一个类,它使用一个空接口(interface)作为“标记接口(interface)”,像这样:namespaceMyNameSpace{publicinterfaceIMessage{//nothingincommonhere...}publicclassMyMessage:IMessage{publicvoidSendMyMessage(){//Dosomethinghere}}}我在其他一些帖子中以及在MSDN(http://msdn.microsoft.com/en-us/library/ms182128.aspx)上读到,应该避免这种情况,您应该使用自定义属性而不是这个空接

c# - 如何抑制 StyleCop 错误 SA0102 : CSharp. CsParser:使用泛型类型参数属性时在文件中发现语法错误

具有以下具有泛型类型参数属性的C#代码:[System.AttributeUsage(System.AttributeTargets.GenericParameter)]publicclassGenericParameterAttribute:System.Attribute{}publicclassGenericClass{}打开StyleCop集成(在.csproj文件中导入StyleCop.targets)StyleCop返回错误且编译失败:Error1SA0102:CSharp.CsParser:Asyntaxerrorhasbeendiscoveredinfile...没有在

c# - 通用约束和接口(interface)实现/继承

不太确定如何表达这个问题,因为它是“为什么这行不通?”查询类型。我已将我的特定问题简化为以下代码:publicinterfaceIFoo{}publicclassFoo:IFoo{}publicclassBarwhereT:IFoo{publicBar(Tt){}publicBar():this(newFoo())//cannotconvertfrom'Foo'to'T'{}}现在,泛型类型T在Bar类必须实现IFoo。那么为什么编译器会给我注释中的错误呢?Foo的实例肯定是IFoo,因此可以作为通用类型的代表传递T?这是编译器限制还是我遗漏了什么? 最佳答

c# - 如何使用通过依赖注入(inject)使用泛型的存储库接口(interface)?

我正在尝试使用以下通用存储库接口(interface)进行DI和构造函数注入(inject):publicinterfaceIRepository:IDisposablewhereTEntity:class问题是为了定义接口(interface)的实例,我必须像这样提供类类型:privateIRepository_personRepository;这个问题是如果我使用DI(并且我使用Unity作为IoC框架),那么我必须在我的构造函数中定义多个实例来获取我需要使用的所有存储库接口(interface),如下所示:publicMyClass(IRepositorypersonReposi

c# - Dart,对泛型的限制?

是否有与c#功能等效的Dart语法来指定泛型类型的类型约束,例如在类似C#的语法中其中TBase是SomeType:classStackPanelextendsPanelwhereTBase:SomeType{} 最佳答案 你可以像这样指定类型约束:classStackPanelextendsPanel{}languagespecification说:AtypeparameterTmaybesuffixedwithanextendsclausethatspecifiestheupperboundforT.Ifnoextendscla

c# - C# 中的泛型和访问 T 的静态成员

我的问题是关于C#以及如何访问静态成员的……好吧,我真的不知道如何解释它(哪种问题对问题不好,不是吗?)我只会给你一些示例代码:Classtest{intmethod1(ObjParameter1){//inhereIwanttodosomethingwhichIwouldexplainasT.TryParse(Parameter1);//myproblemisthatitdoesnotwork...Igetanerror.//justtoexplain:ifIdeclaretest(withtypeInteger)//Iwantmysamplecodetocallint.TryPar

c# - 引用所需的重载泛型方法

给定publicClassExample{publicstaticvoidFoo(intID){}publicstaticvoidFoo(intID){}}问题:将其称为“重载泛型方法”是否正确?如何在创建MethodInfo对象时指定任一方法?TypeexampleType=Type.GetType("fullyqualifiednameOfExample,namespaceOfExample");MethodInfomi=exampleType.GetMethod("Foo",BindingFlags.Public|BindingFlags.Static,null,newType[

C# 泛型约束

这个问题在这里已经有了答案:Isthereaconstraintthatrestrictsmygenericmethodtonumerictypes?(24个答案)关闭8年前。是否可以枚举在泛型约束中“可用”的类型?TMyMethod()whereT:int,double,string我为什么要这样做是因为我有一个小型评估引擎,并且想像这样编写代码:boolexpression.Evaluate();或intexpression.Evaluate();但我想禁止MyCustomClassexpression.Evalaute();