草庐IT

c# - 当我们创建其继承类的新对象时,是否创建了基类的对象?

在C#中,当我们创建一个继承类对象时,它是否也创建了一个基类对象?因为它从子类构造函数调用基类构造函数而感到困惑。Willcallingabaseclassconstructorfromchildclassconstructor,createabaseclassobject? 最佳答案 只创建了一个对象,但它有两个“层”——基类属性和行为以及继承类属性和行为。所以在某种意义上答案是“是的,创建了一个基类对象”(该对象与任何其他基类对象具有相同的属性和行为)但它与继承类是同一对象所以说“否”也是正确的,基础对象也未创建。”。关键区别在

c# - 为什么 GetProperties 两次列出 protected 属性(在通用基类中声明)?

当我声明以下简单类时:classClass1{protectedvirtualTProp1{get;set;}protectedvirtualstringProp2{get;set;}}classClass2:Class1{protectedoverridestringProp1{get;set;}protectedoverridestringProp2{get;set;}}现在我使用反射来获取Class2的属性,如下所示:varhProperties=typeof(Class2).GetProperties(BindingFlags.NonPublic|BindingFlags.In

c# - new() 的通用类型约束和抽象基类

这里我们有一个简单的类层次结构,并且将泛型与typeconstraint一起使用new()的publicabstractclassBase{}publicclassDerived:Base{}publicclassTestClass{privatevoidDoSomething(Targ)whereT:new(){}publicvoidTestMethod(){Derivedd1=newDerived();DoSomething(d1);//compilesBased2=newDerived();DoSomething(d2);//compileerror}}代码在指示的行编译失败,错

c# - 在数据库中存储具有公共(public)基类的对象

假设我有一个共同的基类/接口(interface)interfaceICommand{voidExecute();}然后有一些命令继承自这个接口(interface)。classCommandA:ICommand{intx;inty;publicCommandA(intx,inty){...}publicvoidExecute(){...}}classCommandB:ICommand{stringname;publicCommandB(stringname){...}publicvoidExecute(){...}}现在我想用一种通用方法将这些命令存储在数据库中,然后将它们全部从数据

c# - 转换为基类,并调用其虚方法导致计算器异常

考虑这段代码:publicclassPerson{publicvirtualvoidUpdateLastLogin(){//businessoflastloginhere}}publicclassStudent:Person{publicoverridevoidUpdateLastLogin(){//loggingsomethingspecifictoperson((Person)this).UpdatelastLogin();}}为什么上面的代码会抛出STACKOVERFLOW异常?但这不是:publicclassPerson{publicvirtualvoidUpdateLastL

c# - 基类中的构造函数依赖注入(inject)

我正在使用EntityFramework构建一个存储库基类,其中所有实体存储库都将继承。我想使用Ninject使用依赖注入(inject)将DatabaseContext注入(inject)基类。我认为构造函数注入(inject)是正确的方法,但是在派生类中使用构造函数注入(inject)来执行此操作我将必须将参数传递给基类中的构造函数,但我不希望这样做。因此,Setter注入(inject)更合适?这是我的代码:publicabstractclassBaseRepository:IDisposablewhereTEntity:class{privatereadonlyDatabase

c# - 在 Json.NET 序列化中忽略基类属性

我有以下类结构:[JsonObject]publicclassPolygon:IEnumerable{publicListVertices{get;set;}publicAxisAlignedRectangleEnvelope{get;set;}}publicclassAxisAlignedRectangle:Polygon{publicdoubleLeft{get;set;}...}我正在序列化Polygon类,但是当我这样做时,我得到一个JsonSerializationException,以及消息Selfreferencingloopdetectedforproperty'Env

c# - 从派生类内部调用 C# 基类扩展方法?

这是一个人为的例子:publicstaticclassMyExtensions{publicstaticvoidMyMethod(thisMyInterfaceobj,stringtxt){}}interfaceMyInterface{}publicMyDerived:MyInterface{voidDoStuff(){MyMethod("test");//fails;compilercan'tfindMyMethod?}}在我上面的示例中,我试图从我的派生类调用分配给接口(interface)的扩展方法。编译器在这里失败并说MyMethod在当前上下文中不存在。我的CS文件中有所有适

C#:解决继承类与其基类之间的无效转换异常

我有两个类,名为Post和Question。问题定义为:publicclassQuestion:Post{//...}我的Question类没有覆盖Post的任何成员,它只是表达了一些其他的。我想完成的事情我有一个Post类型的对象,其成员已填充。现在,我想将其转换为一个问题,以便我可以为其他几个成员添加值。这是我当前的代码,使用显式转换:PostpostToQuestion=newPost();//PopulatethePost...Questionques=(Question)postToQuestion;//-->thisistheerror!//Filltheotherpart

c# - 如何为其派生类型的每种可能组合实现基类的方法

我有以下由多个其他类实现的Shape接口(interface),例如Rectangle、Circle、Triangle...interfaceIShape{boolIsColliding(IShapeother);}IsColliding方法应该检查一个Shape是否与另一个Shape发生碰撞,而不管它们的具体类型。然而,每一对形状(矩形/矩形、矩形/圆形、圆形/三角形等)都有自己的碰撞检查实现。我正在努力为这个问题找到一个好的设计解决方案。天真的方法是切换“其他”形状的类型以调用正确的实现:classRectangle:IShape{boolIsColliding(IShapeoth