草庐IT

inheritance

全部标签

c# - 在派生类中重载基方法

所以我正在使用C#来查看它是否与这篇文章中的C++行为相匹配:http://herbsutter.com/2013/05/22/gotw-5-solution-overriding-virtual-functions/当我遇到这种非常奇怪的行为时:publicclassBaseClass{publicvirtualvoidFoo(inti){Console.WriteLine("CalledFoo(int):"+i);}publicvoidFoo(stringi){Console.WriteLine("CalledFoo(string):"+i);}}publicclassDerive

c# - 将接口(interface)转换为它不继承的另一个接口(interface)

我希望这里有人可以解释我所做的错误假设。在C#4.0中,我有2个接口(interface)和一个实现它们的类。在一个方法中,我声明了一个具有第一个接口(interface)类型的变量,使用实现这两个接口(interface)的类实例化它,并且可以以某种方式成功地将它转换为第二个接口(interface),如以下代码所示:publicinterfaceIFirstInterface{voidMethod1();}publicinterfaceISecondInterface{voidMethod2();}publicclassInterfaceImplementation:IFirstI

c# - 泛型与继承(当不涉及集合类时)

这是thisquestion的扩展甚至可能是其他一些问题的重复(如果是这样,请原谅我)。我seefromMSDN泛型通常与集合一起使用Themostcommonuseforgenericclassesiswithcollectionslikelinkedlists,hashtables,stacks,queues,treesandsoonwhereoperationssuchasaddingandremovingitemsfromthecollectionareperformedinmuchthesamewayregardlessofthetypeofdatabeingstored.我

c# - AutoMapper——继承映射不​​工作,相同的源,多个目的地

我可以在AutoMapper(v2.2)中对源类型相同但目标类型不同的映射使用继承映射吗?我有这样的基本情况(真正的类有更多的属性):publicabstractclassBaseViewModel{publicintCommonProperty{get;set;}}publicclassViewModelA:BaseViewModel{publicintPropertyA{get;set;}}publicclassViewModelB:BaseViewModel{publicintPropertyB{get;set;}}ViewModelA和ViewModelB是同一实体类的不同表示

c# - 如何使用反射在泛型类型中动态确定属性属于基类还是子类?

我有以下两个类(模型),一个是基类,另一个是子类:publicclassBaseClass{publicstringBaseProperty{get;set;}}publicclassChildClass:BaseClass{publicstringChildProperty{get;set;}}在应用程序中,我使用泛型动态调用ChildClassListpropertyNames=newList();foreach(PropertyInfoinfointypeof(T).GetProperties()){propertyNames.Add(info.Name);}在这里,在prope

C# 向 COM 公开 - 接口(interface)继承

假设我有一个实现IBaseClass的类BaseClass然后我有一个继承IBaseClass的接口(interface)IClass。然后我有一个名为class的类,它实现了IClass。例如:[ComVisible(true),InterfaceType(ComInterfaceType.IsDual),Guid("XXXXXXX")]publicinterfaceIBaseClass{[PreserveSig]stringGetA()}[ComVisible(true),InterfaceType(ComInterfaceType.IsDual),Guid("XXXXXXX")]

c# - 在 WebApi 中如何在 Controller 类级别继承路由前缀?

请注意,我已经阅读了作为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

c# - XmlRootAttribute 是否可继承?

我有一个正在使用C#的XmlSerializer序列化的类.它标有XmlRoot属性,我想在派生类中继承这个属性。查看文档,它没有说XmlRoot使用AttributeUsageAttribute将Inherit设置为false(Inherit应该默认为true),但是当我尝试反序列化没有XmlRoot属性的继承类时出现错误(“不是预期的。").目前有效:[Serializable()][XmlRoot("rootNode")]publicclassBaseClass{[XmlAttribute("attributeA")]publicintA{get;set;}}[Serializa

c# - 如何正确反射(reflect)基本接口(interface)方法

我通过反射调查了2个接口(interface)和2个类:家长IChild-派生自IParent家长子级-派生自父级对我来说奇怪的是,当我通过对IChild类型的反射进行查看时,我没有找到IParent方法。应用于Child类型的相同代码按预期工作-反射显示Parent方法。interfaceIParent{voidParentMethod();}interfaceIChild:IParent{voidChildMethod();}classParent{publicvoidParentMethod(){}}classChild:Parent{publicvoidChildMethod(

c# - 为什么 C# 允许通过接口(interface)扩展方法而不是类进行多重继承?

关闭。这个问题是off-topic.它目前不接受答案。想改进这个问题吗?Updatethequestion所以它是on-topic用于堆栈溢出。关闭10年前。Improvethisquestion我已经检查过其他问题,令人惊讶的是这个问题似乎并没有被问到。使用扩展方法,接口(interface)提供有限但真实的实现多重继承。这带来了Diamond问题,与基于类的多重继承相同。为什么这比许多人认为如此可怕的基于类的多重继承更好或更容易接受?这实际上似乎是一种更糟糕的实现多重继承的方式,因为扩展方法不能进入接口(interface)本身,甚至不能进入实现该接口(interface)的类,但