Tag: inheritance

运算符重载和inheritancec#

假设我有一个父类和子类,如下所示 家长class: class Parent { public string _First; public string _Last; public override bool Equals(object obj) { if (ReferenceEquals(obj, null)) return false; else if (ReferenceEquals(obj, this)) return true; else if (obj is Parent == false) return false; else return this.Equals(obj as Parent) & base.Equals(obj); } public override int GetHashCode() { unchecked { return this._First.GetHashCode() ^ […]

如何检查一个对象是否*正好*一个类,而不是一个派生的类?

有什么方法可以确定一个对象是否完全是一个类而不是派生的一个类? 例如: class A : X { } class B : A { } 我可以这样做: bool isExactlyA(X obj) { return (obj is A) && !(obj is B); } 当然,如果有更多的派生类A我必须添加和条件。

接口和标头

今天我遇到了C#接口的概念,我有一个希望很简单的问题,看看我是否理解它们……它们是否与C ++头文件非常相似? 我的意思是,从我得到的,你定义一个类的主干,而不是实际定义它的作用,这类似于一个标题,对吗? 我阅读了整个MSDN定义,它并没有真正让我100%清楚。 我相信我有这个想法(编写并附上了一个非常基本的程序,看看我是否理解)但是至少在明天晚上至少完全理解它们的基础知识非常重要。 例: namespace InterfaceTest { class Program { static void Main(string[] args) { KitchenStaff newKitchen = new KitchenStaff(); newKitchen.getBakers(); newKitchen.getBaristas(); newKitchen.getCooks(); Console.ReadLine(); KitchenDuties newKitchen1 = new KitchenDuties(); newKitchen1.getBakers(); newKitchen1.getBaristas(); newKitchen1.getCooks(); Console.ReadLine(); } } interface Bakers { void getBakers(); } interface Cooks { void getCooks(); } interface Baristas { void getBaristas(); } […]

使用基类作为IEnumerable 的通用

我对OOP一般,inheritance和多态,接口等有很好的理解。我遇到了一个奇怪的情况,我不明白为什么它根本不起作用… 编辑:好的,我发现协方差(或逆变?)可以解决这个问题,但至关重要 我们还在使用.NET 2.0 如何在不转向C#4.0的情况下解决这个问题? 这是情况。 鉴于这两个类: public class CustomCollectionType : IEnumerable { /* Implementation here, not really important */ } public class Entity : EntityBase { /* Implentation here, not important */ } 当我尝试使用这种通用方法时,编译器会抱怨 public void LoopThrough(IEnumerable entityList) { foreach(EntityBase entity in entityList) { DoSomething(entity); } } 并尝试以这种方式使用它: CustomCollectionType entityList; /* Add items to […]

XML序列化类具有隐藏inheritance成员的新属性

我有以下抽象类结构: public abstract class Template { // Some properties and methods defined } public abstract class Template : Template where TTemplate : Template { // No new properties defined, but methods overriden } 然后我将这些模板类用作模型的一部分: public abstract class Model { public Template Template {get;set;} public Model(Template t) {Template = t;} // More properties and methods } […]

无法inheritanceShapes

为什么我不能使用inheritanceShapes类的类 ? 我需要用一些方法扩展Rectangle类,但是我想以与使用Shape相同的方式使用这个类,我该怎么办?

协方差胜过具体类型?

说实话 – 我问过(这个问题的一部分) ,但现在我有一个不同的相关问题。 public class Base { public void Foo(IEnumerable strings) { } } public class Child : Base { public void Foo(IEnumerable objects) { } } List lst = new List(); lst.Add(“aaa”); Child c = new Child(); c.Foo(lst); (在C#3中它将调用:C#4中的Child.Foo将调用: Child.Foo ) 我在FW4! , 让我们来谈谈它 所有对协方差的尊重:当我写c.Foo(lst); (我是STRING的IEnumerable !) – 它看到两个签名! 但仍然 – 它选择IEnumerable ?? […]

c#静态构造函数未从派生类调用

class Bus { static Bus() { foreach(FieldInfo fi in typeof(T).GetFields()) { if(fi.FieldType == typeof(Argument)) { fi.SetValue(typeof(T), new Argument(“busyname”, “busyvalue”)); } } } } class Buss : Bus { public static Argument field; } 任何想法如何使这个工作,以便在Buss中引用静态字段的引用触发总线中的静态构造函数?

无法以inheritance的forms直观地更改DataGridView

我有一个带有DataGridView的WinForms表单。 DataGridView设置为protected 。 当我inheritance该表单时,无法在Visual Studio设计器中更改DataGridView 。 如果我使用Button执行整个操作,它会按预期工作。 有没有办法来解决这个问题? 一些(剪切的)代码(来自DatagridForm.Designer.cs): partial class DatagridForm { protected DataGridView dgData; protected Button button; } 从Inherited.cs: partial class Inherited : DatagridForm { }

MongoDB C#驱动程序类型鉴别器,generics类inheritance自非generics基类

我正在尝试使用官方C#驱动程序存储从mongodb中的非generics基类inheritance的generics类的对象列表。 我的代码如下所示: abstract class MyAbstractClass {} class MyGenericClass: MyAbstractClass { [BsonRequired] public List Values = new List(); public MyGenericClass(IEnumerable values) { Values.AddRange(values); } } class MyContainerClass { [BsonId] public string Id; [BsonRequired] public List ValueCollections = new List(); public MyContainerClass() { Id = Guid.NewGuid().ToString(); } } 在测试时,我创建了一个容器对象并用generics类的实例填充它,如下所示: var container = new MyContainerClass(); container.ValueCollections.Add(new MyGenericClass(new[]{“foo”,”bar”,”baz”})); 当我将其保存到数据库时,添加的文档如下所示: […]