草庐IT

default-method

全部标签

c# - 犀牛模拟 : How to stub a generic method to catch an anonymous type?

我们需要stub一个通用方法,该方法将使用匿名类型作为类型参数来调用。考虑:interfaceIProgressReporter{TReport(TprogressUpdater);}//Unittestarrange:FuncreturnArg=(x=>x);//wewishtoreturntheargument_reporter.Stub(x=>x.Report(null).IgnoreArguments().Do(returnArg);如果在被测方法中对.Report()的实际调用是使用对象作为类型参数完成的,那么这将起作用,但实际上,调用该方法时使用的T是匿名类型。此类型在被测

c# - 统一: Change default lifetime manager for implicit registrations and/or disable them

Unity容器将自动解析它可以自行识别的任何类型,无需手动注册。这在某些方面很好,但我遇到的问题是它使用TransientLifetimeManager来解决这种类型的问题,而我几乎总是想要一个ContainerControlledLifetimeManager。当然,我仍然可以手动将我的类型注册为单例,但如果我忘记了,应用程序将成功启动,而不是在启动时出现未处理的异常,并且一切似乎都正常工作。但最终会出现错误,可能非常微妙,难以诊断,因为存在一个类型的多个实例,这意味着是一个单例。所以我的问题是:有没有一种方法可以指定不同的默认生命周期管理器或完全禁用默认的自动解析行为并将容器限制为

c# - 通过安装程序设置模拟属性会导致 'Expression is not a method invocation'

我有下面的代码,其中我的Mock接口(interface)有一个Recorder属性,它是一个类。然后我尝试在该类上设置一个属性,但我得到了一个Expressionisnotamethodinvocation错误。你能帮忙吗?错误是在运行时尝试设置枚举属性时出现的。它会抛出一个带有以下堆栈跟踪的ArgumentException:atMoq.ExpressionExtensions.ToMethodCall(LambdaExpressionexpression)atMoq.Mock.c__DisplayClass1c`2.b__1b()atMoq.PexProtector.Invoke

C# 错误 : The call is ambiguous between the following methods or properties. 运算符重载

我在名为Dinero的命名空间中有2个带有重载运算符的类,这些是2个类:第一个:namespaceDinero{classDollar{#regionAtributospublicDoublecant;#endregion#regionConstructorespublicDollar(){this.cant=0;}publicDollar(Doubleamount){this.cant=amount;}#endregion#regionSobrecargadeOperadorespublicstaticDollaroperator+(Euroeu,Dollardol){Dollard

c# - 如何: Use async methods with LINQ custom extension method

我有一个LINQ自定义扩展方法:publicstaticIEnumerableDistinctBy(thisIEnumerableitems,Funcproperty){returnitems.GroupBy(property).Select(x=>x.First());}我是这样使用它的:varspc=context.pcs.DistinctBy(w=>w.province).Select(w=>new{abc=w}).ToList();但问题是我不想要ToList()我想要这样的东西varspc=awaitcontext.pcs.DistinctBy(w=>w.province).

c# - 当 new-able 使用 new T(),否则使用 default(T)

我正在研究C#泛型函数。错误时,如果泛型类型可以是new-able,返回newT(),否则返回default(T)。代码如下:privateTFunc(){try{//trytodosomething...}catch(Exceptionexception){if(Tisnew-able)//我知道对于那些使用newT()的人来说,它需要whereT:new()。这个问题是,如何在运行时判断这个? 最佳答案 您只需要检查该类型是否具有无参数构造函数。您可以通过以空类型作为参数调用Type.GetConstructor方法来实现。va

c# - 编译器错误 "Default parameter specifiers are not permitted"

下面是我的代码。publicclassPItem{publicStringcontent;publicintcount;publicintfee;publicintamount;publicstringdescription;//DefaultvaluespublicPItem(String_content="",int_count=0,int_fee=0,string_description="",int_amount=0){content=_content;count=_count这是在一个类里面。当我尝试运行程序时出现此错误:Defaultparameterspecifiersa

c# - 错误 CS0051(不一致的可访问性 : parameter type 'Job' is less accessible than method 'AddJobs.TotalPay(Job)' )

我通过省略totalFee字段成功编译并运行了下面的源代码。如何将totalFee写入此程序,以便它准确计算每项工作的总费用(费率*时间)?下面,您会看到我尝试使用一种方法;这产生了错误CS0051(可访问性不一致:参数类型“Job”的可访问性低于方法“AddJobs.TotalPay(Job)”)。此源代码是对以下作业的响应:"DesignaJobclassforHarold’sHomeServices.Theclasscontainsfourdatafields—Jobdescription(forexample,“washwindows”),timeinhourstocomple

c# - 可访问性不一致 : return type is less accessible than method C#

好吧,这真的很奇怪。我有一个私有(private)成员,我想将它用于Form2。我创建了一个公共(public)静态方法,以便我可以将该成员放入Form2。这是我的代码:privatestaticAppControllerappController;privateBreadRepositorybreadRep;privateCakeRepositorycakeRep;privateSandwichRepositorysandwichRep;publicForm1(){InitializeComponent();breadRep=newBreadRepository();cakeRep=

c# - 如何关闭 resharper 中的 "Convert Extension Method to Plain Static"自动重构?

在使用Resharper时,由于某些原因,当我调用扩展方法时,它会自动将其转换为静态方法调用。这就是所谓的ConvertExtensionMethodtoPlainStatic重构。foo.Bar()成为MyStaticExtensions.Bar(foo);具有讽刺意味的是,它随后将此标记为代码异味。我该如何关闭它? 最佳答案 Resharper的IntelliSense中嵌入了此功能。我经常使用Linq-to-sql扩展,所以我禁用了这个功能:Resharper-选项-IntelliSense-启用RadiobuttonVisu