草庐IT

Inheritance

全部标签

c# - 如何在 C# 中使用继承类实现 Xml 序列化

我有两个类:基类名称Component和继承类名称DBComponent[Serializable]publicclassComponent{privatestringname=string.Empty;privatestringdescription=string.Empty;}[Serializable]publicclassDBComponent:Component{privateListspFiles=newList();//StorageProcedureFiles[XmlArrayItem("SPFile",typeof(string))][XmlArray("SPFile

c# - 获取类型的继承树

PossibleDuplicate:TogetparentclassusingReflectiononC#我试图找到一种在C#中使用反射获取特定类型的继承树的简单方法。假设我有以下类(class);publicclassA{}publicclassB:A{}publicclassC:B{}我如何使用类型“C”的反射来确定它的父类(superclass)是“B”,而后者又来自“A”等等?我知道我可以使用“IsSubclassOf()”,但假设我不知道我正在寻找的父类(superclass)。 最佳答案 要获取类型的直接父代,您可以使用

c# - 从所属类外部调用 C# 事件?

是否有可能在任何情况下都能够做到这一点?我目前的情况是这样的:publicclassCustomForm:Form{publicclassCustomGUIElement{...publiceventMouseEventHandlerClick;//etc,andsoforth....}privateList_elements;...publicvoidCustomForm_Click(objectsender,MouseEventArgse){//wemightwanttocalloneofthe_elements[n].Clickinhere//butwecan'tbecausew

C# 反射 - 派生类型中的基类静态字段

在C#中,当我反射(reflect)派生类型时,为什么看不到基类的静态字段?我已经尝试了type.GetFields(BindingFlags.Static)和type.GetFields()。 最佳答案 这就是它的工作原理。static成员实际上是非面向对象的东西。它们不是多态的,它们确实属于它们的声明类型,并且与继承层次结构中的其他类型无关。例如,在访问派生类中的静态成员之前,不需要运行基类的静态初始化程序。除非指定了BindingFlags.FlattenHierarchy,否则不会返回基类的static成员:type.Get

c# - 如何在 C# 中调用 protected 构造函数?

如何调用protected构造函数?publicclassFoo{publicFoo(alotofarguments){}protectedFoo(){}}varfoo=???这显然没有通过测试:publicclassFooMock:Foo{}varfoo=newFooMock();Assert(typeof(Foo),foo.GetType()); 最佳答案 调用无参数保护/私有(private)构造函数:Foofoo=(Foo)Activator.CreateInstance(typeof(Foo),true);调用带参数的非公

c# - ASP.NET EF 从未映射的类中删除鉴别器列

我有一个我的内容模型:classBaseModel{publicvirtualstringContent{get;set;}//...}要仅显示数据,上面的模型就可以了。但我想添加编辑内容的功能。所以我需要向成员content添加一个属性-但这应该只发生在autor按下编辑按钮时,而不是在内容的常规View中。所以我创建了第二个继承自BaseModel的模型,这样我就可以用我的属性覆盖成员:classEditableBaseModel:BaseModel{[UIHint("MyEditor"),AllowHtml]publicoverridestringContent{get;set;

c# - 派生的 C# 接口(interface)属性是否可以覆盖同名的基本接口(interface)属性?

我正在尝试创建一个接口(interface)继承系统,该系统使用相同的属性但始终具有进一步派生的类型。因此,基本属性应该以某种方式被派生接口(interface)覆盖或隐藏。例如,派生到Husband和Wife的两个接口(interface)Man和Woman也是接口(interface)。Man和Husband接口(interface)都有一个“sweetheart”属性,而Woman和Wife有一个“darling”属性。现在,Man的“sweetheart”属性是Woman类型,而Husband的同一个“sweetheart”属性应该是Wife(派生自Woman)。女人和妻子的“

c# - 内部抽象方法。为什么会有人拥有它们?

我今天正在做一些代码审查,发现了一些开发人员编写的旧代码。事情是这样的publicabstractclassBaseControl{internalabstractvoidDoSomething();}如果在同一个程序集中有一个派生类,它就可以工作publicclassDerivedControl:BaseControl{internaloverridevoidDoSomething(){}}但是在不同的程序集中派生基类会产生编译时错误DerivedControldoesnotimplementinheritedabstractmember'BaseControl.DoSomethin

c# - C# 中方法重载的不同行为

我在浏览C#脑筋急转弯(http://www.yoda.arachsys.com/csharp/teasers.html)时遇到了一个问题:这段代码的输出应该是什么?classBase{publicvirtualvoidFoo(intx){Console.WriteLine("Base.Foo(int)");}}classDerived:Base{publicoverridevoidFoo(intx){Console.WriteLine("Derived.Foo(int)");}publicvoidFoo(objecto){Console.WriteLine("Derived.Foo(

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