草庐IT

factory-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# - 使用 Task.Factory.StartNew().ContinueWith() 的单元测试代码

所以我有一些代码Task.Factory.StartNew(()=>this.listener.Start()).ContinueWith((task)=>{if(task.IsCompleted){this.status=WorkerStatus.Started;this.RaiseStatusChanged();this.LogInformationMessage("WorkerStarted.");}});当我测试时,我正在模拟所有依赖对象(namleythis.listener.Start())。问题是测试在调用ContinueWith之前完成执行。当我调试时,由于我单步执行代

c# - Task.Run 和 Task.Factory.StartNew 之间不同的异常处理

我在使用Task.Factory.StartNew并trycatch抛出的exception时遇到问题。在我的应用程序中,我有一个长时间运行的任务,我想将其封装在Task.Factory.StartNew(..,TaskCreationOptions.LongRunning);但是,当我使用Task.Factory.StartNew时,异常没有被捕获。然而,当我使用Task.Run时,它的工作方式与我预期的一样,我认为它只是Task.Factory.StartNew的包装器(根据例如thisMSDNarticle).此处提供了一个工作示例,不同之处在于使用Task.Run时将异常写入控

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# - 带有 CancellationTokenSource 的 Task.Factory.FromAsync

我有以下代码行用于从NetworkStream异步读取:intbytesRead=awaitTask.Factory.FromAsync(this.stream.BeginRead,this.stream.EndRead,buffer,0,buffer.Length,null);我想让它支持取消。我知道我可以canceltasksusingaCancellationTokenSource,但是我看不出有什么方法可以将它传递给TaskFactory.FromAsync().是否可以使FromAsync()构造的任务支持取消?编辑:我想取消一个已经在运行的任务。

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

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

c# - 如何强制 Task.Factory.StartNew 进入后台线程?

我已经看到许多其他类似的问题,但没有在那里找到我的答案。我的问题是我正在使用以下流程创建线程:privatevoidbtn_Click(objectsender,EventArgse){service.GetCount((count,ex)=>{if(ex!=null)return;for(inti=0;icallback){varcallingThread=TaskScheduler.FromCurrentSynchronizationContext();Funcaction=()=>{returnclient.GetCount();//Synchronousmethod,could

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# - switch 语句是否适用于工厂方法? C#

我想返回一个接口(interface),在switch语句中我想设置它。这是一个糟糕的设计吗?privateIResultEntityGetEntity(char?someType){IResultEntityentity=null;switch(someType){case'L'://lifeentity=newLifeEntity();break;case'P'://propertyentity=newPropertyEntity();break;case'D'://disabilityentity=newDisabilityEntity();break;case'C'://cre