jjzjj

generic-method

全部标签

c# - 简单 QueryOver : Unrecognised method call

我有一个简单的QueryOvervarq=SessionInstance.QueryOver().Where(p=>p.Number.Equals(number));Number字段类型为int。此查询因此消息而出现运行时错误:Unrecognisedmethodcall:System.Int32:BooleanEquals(Int32) 最佳答案 ==运算符生成一个BinaryExpression可以将其转换为SQL并且.Equals()方法生成MethodCallExpression这显然没有转换为SQL。通常二元运算符在Que

C# Generic List of Multiple Types 泛型列表

这是我的问题的抽象和简化:我有一套玩具和这些玩具对应的盒子。我希望用户能够指定盒子可以容纳的最大类型的玩具:publicclassBox{}然后在Box类中我想要一个通用的玩具列表,但是盒子中包含的每个玩具都有一个通用类型:publicclassBox{publicList=newList();publicboolWhatever;[memberfunctions,constructors...][ThememberfunctionswilldependonT]}Toys类将如下所示:publicclassToywhereT:struct//Tisanytype{publicList=

c# - 如何测试一个空的 generic.dictionary 集合?

如何测试通用字典对象以查看它是否为空?我想按如下方式运行一些代码:while(reportGraphs.MoveNext()){reportGraph=(ReportGraph)reportGraphs.Current.Value;report.ContainsGraphs=true;break;}reportGraph对象的类型是System.Collections.Generic.Dictionary运行此代码时,reportGraphs字典为空,MoveNext()立即抛出NullReferenceException。如果有更高效的方法来处理空集合,我不想在block周围放置tr

c# - System.Collections.Generic.List 中的某项在什么情况下不会被删除成功?

在什么情况下System.Collections.Generic.List中的item不会被成功移除?来自http://msdn.microsoft.com/en-us/library/cd666k3e.aspx:trueifitemissuccessfullyremoved;otherwise,false.ThismethodalsoreturnsfalseifitemwasnotfoundintheList(OfT).他们表达它的方式让我认为对List(OfT)中找到的项目的删除操作实际上可能会失败,因此这个问题。 最佳答案 查

C# 设计 : Why is new/override required on abstract methods but not on virtual methods?

为什么抽象方法需要new/override而虚方法不​​需要?示例1:abstractclassShapesClass{abstractpublicintArea();//abstract!}classSquare:ShapesClass{intx,y;publicintArea()//Error:missing'override'or'new'{returnx*y;}}编译器会显示这个错误:要使当前成员覆盖该实现,请添加override关键字。否则添加新关键字示例2:classShapesClass{virtualpublicintArea(){return0;}//itisvirt

c# - 通用方法和可选参数

是否可以写出类似的结构?我想以某种方式为类型T的参数设置默认值。privateTGetNumericVal(stringsColName,TdefVal=0){stringsVal=GetStrVal(sColName);TnRes;if(!T.TryParse(sVal,outnRes))returndefVal;returnnRes;}此外,我还找到了以下链接:GenerictypeconversionFROMstring我想,这段代码一定可以工作privateTGetNumericVal(stringsColName,TdefVal=default(T))whereT:IConv

c# - 低级差异 : non-static class with static method vs. 静态类与静态方法

我想知道使用具有静态方法的非静态类与具有相同静态方法的静态类的一般好处(或缺点)是什么,除了我不能使用非静态类中的静态方法作为扩展方法。例如:classNonStaticClass{publicstaticstringGetData(){return"Thiswasinvokedfromanon-staticclass.";}}与此相比:staticclassStaticClass{publicstaticstringGetData(){return"Thiswasinvokedfromastaticclass.";}}使用一种方法优于另一种方法对性能/内存有何影响?注意:假设我不需要

c# - Resharper 中的警告 "Return value of pure method is not used"

我有一个快速的问题,关于我正在工作的c#项目中从VisualStudio中的Resharper收到的警告。警告是:"ReturnValueofpuremethodisnotused"发生这种情况的方法如下:privatestaticboolFilePathHasInvalidChars(stringuserInputPath){try{//thisiswherethewarningoccurs:Path.GetFullPath(userInputPath);}catch(Exceptione){Log.Error(String.Format("TheProgramfailedtorun

c# - 多线程,Task.Run 错误 'The call is ambiguous between the following methods or properties'

当我尝试构建项目时,显示以下错误消息。Thecallisambiguousbetweenthefollowingmethodsorproperties:'System.Threading.Tasks.Task.Run(System.Action)'and'System.Threading.Tasks.Task.Run(System.Func)'我该如何解决这个问题?publicstaticclassMaintananceManager{privatestaticThreadSafeSocialMediaListPostList=newThreadSafeSocialMediaList(

c# - 如何初始化 ConcurrentDictionary?错误 : "Cannot access private method ' Add' here"

我有一个静态类,我在其中使用字典作为查找表以在.NET类型和SQL类型之间进行映射。这是一个这样的字典的例子:privatestaticreadonlyDictionarySqlServerMap=newDictionary{{typeof(Boolean),"bit"},{typeof(Byte[]),"varbinary(max)"},{typeof(Double),"float"},{typeof(Byte),"tinyint"},{typeof(Int16),"smallint"},{typeof(Int32),"int"},{typeof(Int64),"bigint"},{