草庐IT

Generics

全部标签

c# - 使用 LINQ for Entity Framework 将实体转换为通用方法中的已实现接口(interface)

我有一个通用方法来查询EF中TEntity类型的对象。如果TEntity实现特定接口(interface),我想将条件添加为where子句。我的方法是:publicTEntityGetByUserID(GuiduserID){varquery=this.DbSet;if(typeof(TEntity).IsImplementationOf()){query=query.Where((x=>!((IDeletableEntity)x).IsDeleted);}returnquery.FirstOrDefault(x=>x.UserID==userID);}IsImplementation

c# - 接口(interface)作为类型约束和接口(interface)作为参数之间的区别?

如果我想创建一个方法,将IList的实例作为参数(或任何其他接口(interface),但我们以IList为例),我可以创建具有类型约束的泛型方法,例如:publicstaticvoidFoo1(Tlist)whereT:IList{}或者,我可以创建一个直接采用IList参数的方法:publicstaticvoidFoo2(IListlist){}就所有意图和目的而言,这些方法的行为似乎完全相同:ListmyList=newList();Foo1(myList);Foo2(myList);那么我的问题是——这两种方法有什么区别?似乎第二种方法更具可读性;还有其他我应该注意的区别(生成

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

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

c# - 协变/逆变 : how to make the following code compile

更新:以下代码仅在C#4.0(VisualStudio2010)中有意义看来我对协变/逆变有一些误解。谁能告诉我为什么以下代码无法编译?publicclassTestOne{publicIEnumerableMethod(IEnumerablevalues)whereTDerived:TBase{returnvalues;}}编译时:(!!!)publicinterfaceIBase{}publicinterfaceIDerived:IBase{}publicclassTestTwo{publicIEnumerableMethod(IEnumerablevalues){returnva

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# - 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[