草庐IT

泛型库

全部标签

c# - 如果声明是接口(interface),编译器无法识别泛型中的属性

查看以下演示我的VisualStudio2017编译器问题的内容publicinterfaceIFoo{stringKey{get;set;}}publicclassFoo:IFoo{publicstringKey{get;set;}}classProgram{staticvoidMain(string[]args){PrintFoo(newFoo(){Key="HelloWorld"});Console.ReadLine();}privatestaticvoidPrintFoo(Tfoo)whereT:IFoo{//setbreakpointhereandtrytolookatfoo

C# 获取泛型类的所有运行时构造类的列表

我正在尝试列出由泛型类创建的所有运行时构造类。换句话说,如果我有一个类:publicGenericCls{publicvoidReset(){...}...}我在很多地方都有这样的代码:GenericClsgci=newGenericCls();GenericClsgcs=newGenericCls();GenericClsgcf=newGenericCls();...我能得到这样的东西吗?:Type[]allconstructed=GetAllConstructed(typeof(GenericCls));返回{GenericCls,GenericCls,GenericCls,...

c# - 如何使用反射获取泛型类型的扩展方法

从互联网上的各种来源,我收集了以下功能:publicstaticNullableTryParseNullable(thisNullablet,stringinput)whereT:struct{if(string.IsNullOrEmpty(input))returndefault(T);Nullableresult=newNullable();try{IConvertibleconvertibleString=(IConvertible)input;result=newNullable((T)convertibleString.ToType(typeof(T),CultureInfo

c# - 为什么泛型类型参数和其他成员之间存在名称冲突

有时像这样的东西很有用:classX{...}classY{XX{get{...}set{...}}}因为X描述了类型是什么(作为类名),以及被访问/改变的值(作为属性名)。到目前为止,一切都很好。假设你想做同样的事情,但以一种通用的方式:classZ{TT{get{...}set{...}}}对于这个例子,编译器提示:Thetype'Z'alreadycontainsadefinitionfor'T'.这发生在属性、变量和方法上,我不太明白为什么-编译器肯定知道T是一个类型,因此可以像第一个例子一样计算出来?简短版本:为什么第一个示例有效,但第二个示例无效?编辑:我刚刚发现,如果我“

c# - 使用泛型实现类型安全的枚举模式

如何在泛型类上实现类型安全的枚举模式?让我们假设它是按照这些思路实现的publicclassKnownSetting{publicreadonlystaticKnownSettingName=newKnownSetting("name","DefaultName",t=>t);publicreadonlystaticKnownSettingSize=newKnownSetting("size","25",t=>Converter.ToInt32);publicStringKey{get;set;}publicTDefaultValue{get;set;}publicFuncConver

c# - 泛型方法以不同于泛型类型的方式处理 IEnumerable

请检查以下代码段:publicinterfaceICountable{}publicclassCounterwhereT:ICountable{publicintCount(IEnumerableitems){return0;}publicintCount(TItem){return0;}}publicclassCounter{publicintCount(IEnumerableitems)whereT:ICountable{return0;}publicintCount(TItem)whereT:ICountable{return0;}}Counter的两个版本仅在泛型参数的规范上有

c# - 你能在 Controller 中使用泛型方法吗?

是否可以在Controller中使用泛型方法?我在谈论这样的事情:[HttpPost]publicvoidDoSomething([FromBody]SomeGenericClasssomeGenericObject){SomePrivateMethod(someGenericObject);}我实际上已经尝试了上面的方法(尽管所有的名称都不同)并发布到Api//DoSomething使用someGenericObject的实例在请求正文中,它没有工作(即它没有到达Controller)。我猜测WebAPI路由无法解析泛型方法,因为它们可能会导致下面不同类型的不同方法。但这正是我的想法

c# - 在这种情况下,C# 泛型会阻止结构的自动装箱吗?

通常,将结构S视为接口(interface)I会触发结构的自动装箱,如果经常这样做会对性能产生影响。但是,如果我编写一个采用类型参数T:I的泛型方法并使用S调用它,那么编译器是否会省略装箱,因为它知道类型S并没有使用接口(interface)?这段代码表明了我的观点:interfaceI{voidfoo();}structS:I{publicvoidfoo(){/*dosomething*/}}classY{voiddoFoo(Ii){i.foo();}voiddoFooGeneric(Tt)whereT:I{t.foo();//doFoo方法在I类型的对象上调用foo(),所以一旦我

c# - 关于泛型和继承(原谅我不好的标题)

因为我不知道我的问题是如何命名的,所以我不能保证最近或根本没有人问过同样的问题。我确实注意到了,但是有很多主题具有相似的标题,但它们似乎与我的问题无关。我有一个自定义列表类,它实现了泛型。classMyList{publicvoidadd(Titem)//addsanitemtothelist{/*code*/}publicvoidadd(MyListlist)//attachesanexistinglisttotheendofthecurrentone{/*code*/}}我也有类(class):classApple:Fruit和classBanana:Fruit现在,是相关代码:M

c# - C# 中空泛型参数 <,> 的术语是什么?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:C#Language:generics,open/closed,bound/unbound,constructed在C#中使用反射做一些事情时,我注意到某些类型具有类型定义,例如Foo这种表示法的官方术语是什么?