Tag: inheritance

为什么我不能从扩展类型的基类调用扩展方法?

我正在尝试通过覆盖索引器来添加查找List<KeyValuePair>的元素的function。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { public class MyList : List<KeyValuePair> { public int this[string key] { get { return base.Single(item => item.Key == key).Value; } } } } 由于某种原因,编译器抛出此错误: ‘ System.Collections.Generic.List<System.Collections.Generic.KeyValuePair> ‘不包含’ Single ‘的定义。 虽然List确实没有该方法,但它应该是可见的,因为它是System.Linq命名空间(包含在内)中的扩展方法。 显然使用this.Single解决了这个问题,但为什么通过base访问是一个错误? C#规范第7.6.8节说 当base.I出现在类或结构中时, I必须表示该类或结构的基类的成员。 这似乎阻止了通过base访问扩展方法。 不过它也说 在绑定时, base.I和base[E]forms的基本访问表达式的计算方式与它们的编写完全相同((B)this).I和((B)this)[E] ,其中B是构造发生的类或结构的基类。 因此, base.I和base[E]对应于this.I和this[E] […]

从C#中的inheritance类转换数据类型

我试图理解我的团结项目的inheritance,但似乎已经找到了我的设置的限制。 我在写作时感到困惑,因为我仍在学习正确理解C#。 我有一组inheritance的类,它们基于两种不同的行为进行拆分,这样我就有了正确的引用。 然后我需要转换它们,以便我可以访问其中一个类中的方法。 所以我的结构看起来像这样: public class Behaviour : Position { public Handler reference; public Behaviour(int tx, int ty, Handler refer) : base (tx,ty){ reference = refer; } // overload public Behaviour(int tx, int ty) : base (tx,ty){} } public class Behaviour2 : Position { public SettingsHandler reference; public Behaviour2(int tx, int ty, SettingsHandler refer) […]

ASP.NET MVC 3:具有inheritance/多态的DefaultModelBinder

首先,对于这个大post(我先尝试做一些研究)和同一问题的混合技术(ASP.NET MVC 3,Ninject和MvcContrib)感到抱歉。 我正在使用ASP.NET MVC 3开发一个项目来处理一些客户端订单。 简而言之:我有一些inheritance自的对象和抽象类Order ,当我向控制器发出POST请求时,我需要解析它们。 我该如何解决正确的类型? 我是否需要覆盖DefaultModelBinder类,还是有其他方法可以执行此操作? 有人可以提供一些代码或其他链接,如何做到这一点? 任何帮助都会很棒! 如果post令人困惑,我可以做任何改变,以明确! 所以,对于我需要处理的订单,我有以下inheritance树: public abstract partial class Order { public Int32 OrderTypeId {get; set; } /* rest of the implementation ommited */ } public class OrderBottling : Order { /* implementation ommited */ } public class OrderFinishing : Order { /* implementation ommited */ […]

关于子类返回类型的C#协方差

有谁知道为什么C#不支持协变返回类型? 即使在尝试使用接口时,编译器也会抱怨它是不允许的。 请参阅以下示例。 class Order { private Guid? _id; private String _productName; private double _price; protected Order(Guid? id, String productName, double price) { _id = id; _productName = productName; _price = price; } protected class Builder : IBuilder { public Guid? Id { get; set; } public String ProductName { get; set; } public double […]

如何从另一个类访问Winform文本框控件?

我有一个名为Form1的winform和一个名为textBox1的textbox 在Form1中,我可以通过键入以下内容来设置文本: textBox1.text = “change text”; 现在我创建了另一个类。 如何在此课程中调用textBox1 ? 所以我想在这个类中更改textBox1的文本。 如何从这个新类访问Form1 ?