草庐IT

Generics

全部标签

c# - 非泛型类上的 C# 泛型方法归结为什么?

如果我有这样的类(class):-staticclassFoo{publicstaticvoidBar(Titem){Console.WriteLine(item.ToString();}}我知道在这个例子中没有必要使用T因为所有类型都有ToString()等等——这只是一个人为的例子。我更感兴趣的是引擎盖下发生的事情,具体如下:-Foo.Bar("Hello");Foo.Bar(123);Foo.Bar(newEmployee("Isaac"));我广泛地(思考!)我理解具体化,即如果你创建不同类型的泛型类,例如ListListList等然后在编译时(或运行时?)我们最终得到三种实际

c# - 编译器在传递方法时无法确定泛型类型

我在使用C#和泛型类型推断时遇到了问题。我想编写一个方法来传递具有任何类型的方法,但编译器无法推断我传入的方法的类型。编译器总是提示消息Expectedamethodwith'???TestFunc(???,???)'signature这是一个测试用例。usingSystem;publicclassExample{privateinterfaceITest{intTestFunc(stringstr,inti);}privateclassTest:ITest{publicintTestFunc(stringstr,inti){return0;}}publicstaticvoidMain

c# - 为什么 C# 编译器不自动推断此代码中的类型?

为什么C#编译器不推断FooExt.Multiply()的事实满足Functions.Apply()的签名?我必须指定一个类型为Func的单独委托(delegate)变量让代码工作......但似乎类型推断应该处理这个问题。我错了吗?如果是这样,为什么?编辑:收到的编译错误是:ThetypeargumentsformethodFirstClassFunctions.Functions.Apply(T1,System.Func,T2)'cannotbeinferredfromtheusage.Tryspecifyingthetypeargumentsexplicitlynamespace

c# - 犀牛模拟 : How to stub a generic method to catch an anonymous type?

我们需要stub一个通用方法,该方法将使用匿名类型作为类型参数来调用。考虑:interfaceIProgressReporter{TReport(TprogressUpdater);}//Unittestarrange:FuncreturnArg=(x=>x);//wewishtoreturntheargument_reporter.Stub(x=>x.Report(null).IgnoreArguments().Do(returnArg);如果在被测方法中对.Report()的实际调用是使用对象作为类型参数完成的,那么这将起作用,但实际上,调用该方法时使用的T是匿名类型。此类型在被测

c# - 我如何声明派生的 "shell"类,它们除了重命名外什么都不做?

我在代码中传递和使用了两种不同类型的字符串,这两种字符串密切相关,但不应相互混淆。我想我可以通过拥有两个只是字符串但具有不同名称的类来帮助自己避免错误,这样方法签名(以及一般的类型不兼容)将强制执行两种不同类型字符串的语义。同时,我不想从something="foo";重构。至something.Value="foo";在一百个地方。第一个想法:privateclassFirstKind:string{}privateclassSecondKind:string{}想法是如果我有voidMyMethod(FirstKindvarOne,SecondKindvarTwo){...},然后

C# Generic Constraint - 如何引用当前类类型?

我有以下类/接口(interface):publicabstractclassAbstractBasePresenter:IPresenterwhereT:class,IView{}publicinterfaceIPresenter{}publicinterfaceIView:IViewwhereTV:IViewModelwhereTE:IEditModel//whereTP:AbstractBasePresenter{}publicinterfaceIView{}有什么方法可以将IView上的TP限制为继承自AbstractBasePresenter的类?或者我唯一的选择是创建一个非

c# - C#泛型是如何实现的?

我曾认为C#中的泛型的实现方式是在运行时或编译时生成新的类/方法/你有什么,当使用新的泛型类型时,类似于C++模板(我从未真正研究过,我很可能是错的,对此我很乐意接受更正)。但是在我的编码中我想出了一个确切的反例:staticclassProgram{staticvoidMain(){TesttestVar=newTest();GenericTestgenericTest=newGenericTest();intgen=genericTest.Get(testVar);RegularTestregTest=newRegularTest();intreg=regTest.Get(test

c# - 为什么不在用作泛型类型参数的类上调用静态构造函数?

给定以下类:publicclassFoo{staticFoo(){Console.WriteLine("Fooisbeingconstructed");}}publicclassBar{publicvoidReferenceFooAsGenericTypeParameter(){Console.WriteLine("Fooisbeingreferencedasagenerictypeparameter");}}publicclassSampleClass{publicstaticvoidMain(){newBar().ReferenceFooAsGenericTypeParameter

c# - 重写继承方法时避免显式类型转换

我有一个基础抽象类,它也实现了一个特定的接口(interface)。publicinterfaceIMovablewhereTEntity:classwhereT:struct{TEntityMove(IMovermoverProvider);}publicabstractclassAnimal:IMovable{...publicvirtualAnimalMove(IMovermoverProvider){//performsmovementusingprovidedmover}}然后我继承了一些类,其中一些类必须重写基类的接口(interface)实现方法。publicclassS

c# - XML反序列化泛型方法

我有下一个XML文件:d639a54f-baca-11e1-8067-001fd09b1dfd-24145e3b3b4cd-bb8e-11e1-8067-001fd09b1dfd0.28我将它反序列化为这个类:[XmlRoot("Root",IsNullable=false)]publicclassDocBalanceCollection{[XmlElement("Document")]publicListDocsBalanceItems=newList();}DocBalanceItem是:publicclassDocBalanceItem{[XmlElement("Id")]pub