草庐IT

INTERFACE

全部标签

c# - 接口(interface)的调度程序实现的 Ninject 绑定(bind)

我有一个界面:publicinterfaceIService{voidDoStuff(intparm1,stringparm2,GuidgimmeABreakItsAnExampleK);}我想配置Ninject(v3)绑定(bind),这样我就可以有一个“调度程序”shuffle方法调用多个IService实例,如下所示:publicsealedclassDispatcherService:IService{privateIEnumerable_children;publicDispatcherService(IEnumerablechildren){this._children=c

c# - 向 IList<T> 添加动态失败

在下面的代码示例中调用l.Add(s)和c.Add(s)是成功的,但是对于通用的IList失败了.varl=newList();dynamics="s";l.Add(s);varc=(ICollection)l;c.Add(s);vari=(IList)l;i.Add("s");//worksi.Add(s);//failshttps://dotnetfiddle.net/Xll2IfUnhandledException:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:Nooverloadformethod'Add'takes

c# - C#中的接口(interface)继承

我正在尝试解决我在编写应用程序时遇到的相当大的(对我来说)问题。请看这个(为了简单起见,我会尽量缩短代码):我有一个名为IRepository的根接口(interface).接下来,IBookRepository:IRepository接下来,实现它的具体类:BookRepository:IBookRepository在RepositoryManager类中我声明了privateIRepositorycurrentRepo;IRepoItem是Book类实现的接口(interface)。现在,当我尝试做这样的事情时:currentRepo=newBookRepository();Vis

c# - 具有多重实现的协变接口(interface)的泛型类型推断,如何解决它?

考虑这个什么都不做的愚蠢程序:interfaceI{}classA1:I{}classA2:A1,I{}classB1{}classB2:B1,I{}classC1:I{}classC2:C1,I{}staticclassProgram{staticvoidf(Iobj){}staticvoidMain(){f(newA2());f(newA2());f(newB2());f(newB2());f(newC2());f(newC2());}}这表明A2和C2同时实现I和I,那B2同时实现I和I.但是,将其修改为staticvoidMain(){f(newA2());f(newB2())

c# - 你能模拟一个实现接口(interface)和抽象类的对象吗?

是否可以使用Moq模拟一个实现接口(interface)和抽象类的对象?即:publicclassMyClass:SomeAbstractClass,IMyClass你能mock这个吗? 最佳答案 您可以模拟任何接口(interface)以及任何抽象或虚拟成员。基本上就是这样。这意味着以下情况是绝对可能的:varimock=newMock();varaMock=newMock();如果继承自SomeAbstractClass的成员没有被密封,你也可以mockMyClass:varmcMock=newMock();这是否有意义取决于M

c# - 使用代码契约来定义不可变接口(interface)?

我可以使用代码契约在接口(interface)上定义只读、不变的属性吗?IE。实例化后始终产生相同值的属性? 最佳答案 首先注意.NET中的术语:只读:您碰巧拥有的接口(interface)不能用于改变对象或集合不可变的:没有什么可以改变对象或集合现在回到你的问题。在.NET代码约定中,所有属性getter都隐式标记为“纯”。这意味着从getter中读取永远不会有明显的副作用。从严格意义上讲,如果您有一个只有只读属性的抽象接口(interface),那么整个接口(interface)都被认为是只读的。但是,听起来您真正想要的是一种将

c# - 实现通用工厂方法

我实现了一个车辆服务,负责为汽车和卡车等车辆提供服务:publicinterfaceIVehicleService{voidServiceVehicle(Vehiclevehicle);}publicclassCarService:IVehicleService{voidServiceVehicle(Vehiclevehicle){if(!(vehicleisCar))thrownewException("Thisserviceonlyservicescars")//logictoservicethecargoeshere}}我还有一个车辆服务工厂,负责根据传入工厂方法的车辆类型创建车

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# - 开放实现的开放通用接口(interface)类型不等于接口(interface)类型?

在我看来,这是一个应该通过但没有通过的测试。[TestMethod]publicvoidcan_get_open_generic_interface_off_of_implementor(){typeof(OpenGenericWithOpenService).GetInterfaces().First().ShouldEqual(typeof(IGenericService));}publicinterfaceIGenericService{}publicclassOpenGenericWithOpenService:IGenericService{}为什么没有通过?给定Typet=

c# - System.Data.IDbCommand 和异步执行?

系统.数据.SqlClient.SqlCommand有方法BeginExecuteNonQueryBeginExecuteReaderBeginExecuteXmlReader和EndExecuteNonQueryEndExecuteReaderEndExecuteXmlReader用于异步执行。System.Data.IDb命令只有ExecuteNonQueryExecuteReaderExecuteXmlReader仅用于同步操作。有异步操作的接口(interface)吗?另外,为什么没有BeginExecuteScalar? 最佳答案