草庐IT

Generics

全部标签

c# - 获取通用接口(interface)的类型?

我有一个像这样的通用接口(interface):publicinterfaceIResourceDataType{voidSetResourceValue(TresValue);}然后我得到了实现我的接口(interface)的类:publicclassMyFont:IResourceDataType{//Ctor+SetResourceValue+...}最后我得到了一个:varMyType=typeof(MyFont);我现在想从MyType获取System.Drawing.Font类型!此刻,我得到了这段代码:if(typeof(IResourceDataType).IsAssi

c# - 为什么不接受 IEnumerable(of T) 作为扩展方法接收器

在代码前完成问题:为什么是IEnumerablewhereT:ITest不被接受为期望thisIEnumerable的扩展方法的接收者?现在是代码:我有三种类型:publicinterfaceITest{}publicclassElement:ITest{}publicclassElementInfo:ITest{}还有两种扩展方法:publicstaticclassExtensions{publicstaticIEnumerableMethod(thisIEnumerablecollection)whereT:ITest{→returncollection.ToInfoObjects

c# - 变体和开放泛型 IReadOnlyList

我试图理解为什么C#中有关变体和泛型的特定行为无法编译。classMatrixwhereTLine:ILine{TLine[]_lines;IReadOnlyListLines{get{return_lines;}}//doesnotcompileIReadOnlyListLines{get{return_lines;}}//compile}我不明白为什么这不起作用:_lines,属于TLine[]类型,工具IReadOnlyListIReadOnlyList是一个变体通用接口(interface),这意味着,据我所知,任何实现IReadOnlyList的东西可以用作IReadOnly

c# - .NET:静态方法的推断泛型类型

假设我有publicstaticListMap(Listinputs,Funcf){returninputs.ConvertAll((x)=>f(x));}privateintSquare(intx){returnx*x;}publicvoidRun(){varinputs=newList(newint[]{2,4,8,16,32,64,128,256,512,1024,2048});//thisdoesnotcompilevaroutputs=Map(inputs,Square);//thisisfinevaroutputs2=Map(inputs,Square);//thisisa

当 T 可以是数组时的 C# 泛型

我正在为第3方库编写一个C#包装器,它从硬件设备读取单个值和数组,但即使是一个值也总是返回一个对象[]数组。当我希望最终用户能够使用泛型接收数组或单个值时,这需要重复调​​用object[0]。我想使用泛型,以便被调用者可以通过以下方式使用包装器:MyWrappermw=newMyWrapper(...);floatvalue=mw.Value;//shouldreturnfloat;MyWrappermw=newMyWrapper(...);float[]values=mw.Value;//shouldreturnfloat[];在MyWrapper中,我目前的Value属性如下:p

c# - 从 BlockingCollection 中取出项目的正确方法是什么?

调用BlockingCollection.Take()时,集合的IsCompleted状态可能会在检查IsCompleted和调用Take()之间发生变化。MSDNDocumentation显示的示例只是捕获无效操作异常,但似乎必须有一种正确的方法来执行此类调用而不捕获异常(因为这会产生相当大的开销并且在代码中看起来不干净)。调用.Take()并避免无效操作异常的正确方法是什么?我的特定问题的简化版本:If(!blockingCollection.IsCompleted){//dosomestuffvalue=blockingCollection.Take();//ThrowsExce

c# - 为什么我不能对两个泛型类型参数使用协变?

考虑以下示例:classBase{}classDerived:Base{}classTest1{privateListm_X;publicIEnumerableGetEnumerable(){returnm_X;}}这编译得很好,因为IEnumerable在T中协变.但是,如果我做完全相同的事情,但现在使用泛型:classTest2whereTDerived:TBase{privateListm_X;publicIEnumerableGetEnumerable(){returnm_X;}}编译错误Cannotconvertexpressiontype'System.Collection

c# - Lambda 表达式以及如何组合它们?

如何使用OR将两个lambda表达式合并为一个?我尝试了以下方法,但合并它们需要我将参数传递到Expression.Invoke调用中,但是我希望将传递到新lambda中的值传递到每个子lambda中。Expression>func1=(x)=>x>5;Expression>func2=(x)=>x>lambda=Expression.Lambda>(Expression.Or(Expression.Invoke(func1),Expression.Invoke(func2)));//The9shouldbepassedintothenewlambdaandintobothchildl

c# - 为什么 C# 中的空结构会消耗内存

我一直理解结构(值类型)包含结构字段中定义的字节数...但是,我做了一些测试,空结构似乎有一个异常(exception):publicclassEmptyStructTest{staticvoidMain(string[]args){FindMemoryLoad((id)=>newFooStruct());FindMemoryLoad>((id)=>newBar(id));FindMemoryLoad>((id)=>newBar(id));FindMemoryLoad((id)=>id);Console.ReadLine();}privatestaticvoidFindMemoryLo

c# - 保留一个 Dictionary<Type, MyClass<T>> ,其中元素可以按类型引用

我有一个名为EntityTypeTransform的抽象类,它有一个抽象方法,用于保存将IDataRecord转换为T实例的Func委托(delegate)。publicabstractclassEntityTypeTransformwhereTEntityType:class{publicabstractFuncGetDataTransform();}该类的实现可能看起来像(看起来像):publicclassTaskParameterEntityTypeTransform:EntityTypeTransform{publicoverrideFuncGetDataTransform()