草庐IT

继承权

全部标签

c# - Entity Framework DB-First,实现继承

我正在尝试使用EntityFramework6.0和数据库优先方法实现继承。好的,假设我有一个Person和一个Organization实体,如下所示://asimplifiedversionoforganizationentitypublicclassOrganization{publicGuidID{get;set;}publicstringNickname{get;set;}publicstringEmail{get;set;}publicstringPhoneNumber{get;set;}publicstringOfficialName{get;set;}publicGuid

c# - 检查对象类型是否继承了抽象类型

假设我有一个对象,someDrink。它可以是CocaCola或Pepsi类型,它们都继承了抽象的Cola(它继承了Drink)或任何类型喝那件事。我有一个方法可以返回一串最喜欢的饮料。publicstringPreferredDrink(DrinksomeDrink){varorderOfPreference=newList{typeof(Cola),typeof(PurpleDrank),typeof(LemonLimeBitters)...}foreach(drinkTypeinorderOfPreference){if(someDrink.GetType()==drinkTyp

c# - 无法从 ASP.NET Core 2.0 中的 IdentityUser 和 IdentityRole 继承

我正在尝试完成对.NETCore2.0的更新,但弹出了几个错误:问题:我有两个类,ApplicationRole和ApplicationUser,它们继承了IdentityRole和IdentityUser的一些属性。随着Core2.0的更新,我收到以下错误,声称找不到IdentityRole和IdentityUser。但是,包Microsoft.AspNetCore.Identity.EntityFrameworkCore2.0安装在新版本的.NETCore中关于IdentityRole:消息1:Thetypeornamespacename'IdentityRole'couldnot

c# - 构造函数和继承

让我们以C#为例publicclassFoo{publicFoo(){}publicFoo(intj){}}publicclassBar:Foo{}现在,除了构造函数之外,Foo的所有公共(public)成员都可以在Bar中访问。我不能做类似的事情Barbb=newBar(1);为什么构造函数不可继承?更新我知道我们可以链接构造函数,但我想知道为什么上述构造无效。我相信应该有一个正当的理由。 最佳答案 构造函数不可继承,因为它可能会导致奇怪和意外的行为。更具体地说,如果您向基类添加了一个新的构造函数,则所有派生类都会获得该构造函数的

c# - 我可以防止继承的虚方法在子类中被覆盖吗?

我有一些类是这样布局的classA{publicvirtualvoidRender(){}}classB:A{publicoverridevoidRender(){//PreparetheobjectforrenderingSpecialRender();//Dosomecleanup}protectedvirtualvoidSpecialRender(){}}classC:B{protectedoverridevoidSpecialRender(){//Dosomecoolstuff}}是否可以在不破坏以下代码的情况下防止C类覆盖Render方法?Aobj=newC();obj.Re

c# - 如何定义一个必须继承的类

如何定义一个必须继承的类?在C#中 最佳答案 您将类标记为abstract(这是VB.NET必须继承的C#模拟)。这将确保它不能被直接实例化。来自链接的MSDN文章:Theabstractmodifierindicatesthatthethingbeingmodifiedhasamissingorincompleteimplementation.Theabstractmodifiercanbeusedwithclasses,methods,properties,indexers,andevents.Usetheabstractmod

C# 泛型通配符或如何保存对未知泛型继承的引用

好的,情况是这样的。我有一个FlexCollection类,其目的是保存FlexItem的一些专业列表,因此:publicclassFlexCollectionwhereT:FlexItem,new(){publicvoidAdd(Titem){...}...}FlexItem不是泛型类本身。我想要实现的是能够保持FlexItem的字段是对包含该对象的集合的引用。不幸的是,在C#中,不可能保留对模板类的“任何”特化的引用(如在Java中)。起初我尝试使用非通用接口(interface)IFlexCollection但它实际上迫使我对每个方法执行两次,即:publicclassFlexC

c# - 继承的通用类型统一

对于这样的场景:publicinterfaceIAnimal{}publicinterfaceIGiraffe:IAnimal{}publicinterfaceIQuestionableCollection:IEnumerable{voidSomeAction();}publicinterfaceIQuestionableCollection:IQuestionableCollection,IEnumerablewhereT:IAnimal{}publicclassQuestionableCollection:IQuestionableCollectionwhereT:IAnimal{

c# - 尝试反序列化从 Exception 继承的类时 Json.net 失败

我有一个继承自Exception的类SearchError,每当我尝试从有效的json中反序列化它时,我都会得到以下异常:ISerializabletype'SearchError'doesnothaveavalidconstructor.TocorrectlyimplementISerializableaconstructorthattakesSerializationInfoandStreamingContextparametersshouldbepresent.Path'',line1,position81.我尝试实现建议的缺失构造函数,但没有帮助。这是实现建议的构造函数后的类:

c# - C# 8 默认接口(interface)实现是否允许多重继承

根据https://blogs.msdn.microsoft.com/dotnet/2018/11/12/building-c-8-0/,C#8中的一项新功能是接口(interface)的默认实现。这个新特性是否也隐含地允许多重继承?如果不是,如果我尝试以下操作,究竟会发生什么:publicinterfaceA{intFoo()=>1;}publicinterfaceB{intFoo()=>2;}publicclassC:A,B{} 最佳答案 MadsTorgersen在您链接到的博客文章中回答了您的问题:Actuallyinter