草庐IT

【JavaSE】初识泛型

全部标签

c# - 使用泛型类型参数代替 System.Type 类型的参数。是气味吗?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭3年前。Improvethisquestion我经常看到(例如在许多模拟库中)使用泛型类型参数代替System.Type类型参数的方法。我特别讨论的是泛型类型仅在typeof(T)操作中使用的情况(即,方法中的任何地方都没有使用类型T的实例,并且T没有被用于返回值类型或其他参数)。例如考虑以下方法:publicstringGetTypeName(System.Typetype){returntype.FullName;}这个方法通常伴

c# - 将扩展方法组转换为具有泛型类型的委托(delegate)

我在IDataReader上有两个具有以下签名的扩展方法:internalstaticListGetList(thisIDataReaderreader,Funcdel)internalstaticdouble?GetDoubleOrNull(thisIDataReaderreader,stringcolumnName)GetDoubleOrNull没有任何重载。在其他地方,我可以做Funcdel=reader.GetDoubleOrNull;varx=reader.GetList(del);或varx=reader.GetList(reader.GetDoubleOrNull);或者

c# - 无效的转换异常泛型

我遇到了这个问题,我正在使用反射从类中提取属性,但问题是反射将它们作为对象返回,而我无法将其放入我的实际类型中。举个例子,如果这是类:publicclassRow{publicstaticexplicitoperatorRow(Rowo){returnnewRow{Name=o.Name,Value=o.Value};}publicstringName{get;set;}publicTValue{get;set;}}类型转换自一说Row至Row作品:vara=newRow{Name="Foo",Value=true};varb=(Row)a;//Works但是当我尝试从object开始

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

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

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# - 如何使用通过依赖注入(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();