我有一个在客户端应用程序和服务器应用程序中使用的类。在服务器应用程序中,我通过扩展方法向类添加了一些功能。效果很好。现在我想要更多:我的类(B)继承自另一个类(A)。我想将一个虚函数附加到A(比方说Execute()),然后在B中实现该函数。但仅限于在服务器中。Execute()方法需要做一些只能在服务器上做的事情,使用只有服务器知道的类型。有很多类型像B一样从A继承,我想为它们中的每一个实现Execute()。我希望我可以为A添加一个虚拟扩展方法,但这个想法似乎行不通。我正在寻找解决这个问题的最优雅的方法,有或没有扩展方法。 最佳答案
最近我发现C#允许Aninterfacecaninheritfromoneormorebaseinterfaces.例如,Caliburn.Micro中的IScreen在http://caliburnmicro.codeplex.com/SourceControl/latest#src/Caliburn.Micro/IScreen.cs中执行此操作namespaceCaliburn.Micro{publicinterfaceIScreen:IHaveDisplayName,IActivate,IDeactivate,IGuardClose,INotifyPropertyChangedE
我希望这里有人可以解释我所做的错误假设。在C#4.0中,我有2个接口(interface)和一个实现它们的类。在一个方法中,我声明了一个具有第一个接口(interface)类型的变量,使用实现这两个接口(interface)的类实例化它,并且可以以某种方式成功地将它转换为第二个接口(interface),如以下代码所示:publicinterfaceIFirstInterface{voidMethod1();}publicinterfaceISecondInterface{voidMethod2();}publicclassInterfaceImplementation:IFirstI
在下面的示例中,我可以在inherited类中创建一个virtual方法Show(),然后override它在继承类中。我想用protected类变量prefix做同样的事情,但我得到了错误:Themodifier'virtual'isnotvalidforthisitem但是因为我不能在我的类中将这个变量定义为virtual/override,所以我得到了编译器警告:TestOverride234355.SecondaryTransaction.prefix'hidesinheritedmember'TestOverride234355.Transaction.prefix'.Use
这是thisquestion的扩展甚至可能是其他一些问题的重复(如果是这样,请原谅我)。我seefromMSDN泛型通常与集合一起使用Themostcommonuseforgenericclassesiswithcollectionslikelinkedlists,hashtables,stacks,queues,treesandsoonwhereoperationssuchasaddingandremovingitemsfromthecollectionareperformedinmuchthesamewayregardlessofthetypeofdatabeingstored.我
我可以在AutoMapper(v2.2)中对源类型相同但目标类型不同的映射使用继承映射吗?我有这样的基本情况(真正的类有更多的属性):publicabstractclassBaseViewModel{publicintCommonProperty{get;set;}}publicclassViewModelA:BaseViewModel{publicintPropertyA{get;set;}}publicclassViewModelB:BaseViewModel{publicintPropertyB{get;set;}}ViewModelA和ViewModelB是同一实体类的不同表示
假设我有一个实现IBaseClass的类BaseClass然后我有一个继承IBaseClass的接口(interface)IClass。然后我有一个名为class的类,它实现了IClass。例如:[ComVisible(true),InterfaceType(ComInterfaceType.IsDual),Guid("XXXXXXX")]publicinterfaceIBaseClass{[PreserveSig]stringGetA()}[ComVisible(true),InterfaceType(ComInterfaceType.IsDual),Guid("XXXXXXX")]
为什么条件运算符(?:)在与从单个基类型继承的两个类型一起使用时不起作用?我的例子是:ActionResultfoo=(someCondition)?RedirectToAction("Foo","Bar"):Redirect(someUrl);长格式工作正常的地方:ActionResultfoo;if(someCondition){foo=RedirectToAction("Foo","Bar");}else{foo=Redirect(someUrl);}RedirectToRouteResult和RedirectResult这两个返回类型都继承自ActionResult。
请注意,我已经阅读了作为WebApi2.2的一部分的新路由功能,以允许路由继承。但是,这似乎并没有解决我的特定问题。它似乎解决了继承Action级别路由属性的问题,但没有解决在类级别定义的路由前缀。http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-22#ARI我想做这样的事情:[RoutePrefix("account")]publicabstractclassAccountControllerBase:ControllerBase{}[RoutePrefix("facebook")]pub
郑重声明,我已经看过这个connectitem但我真的不明白支持这个会有什么问题。假设我有以下代码:publicinterfaceIInterface{voidMethod();}publicclassBase:IInterface{virtualvoidIInterface.Method(){thrownewNotImplementedException();}}虚拟标识符有什么问题?使用virtual修饰符可以override指示基类中有不同的实现。我现在可以通过删除虚拟方法并像这样创建派生类来使其工作:publicclassDerived:IInterface{voidIInte