Tag: 接口

为什么接口不起作用,而抽象类却使用generics类约束?

下面的代码显示了一个带有类型约束的通用类( Pub )。 该类有一个可以引发的事件,允许我们向订阅者传递消息。 约束是消息必须实现IMsg (或者当它是抽象类时从IMsginheritance)。 Pub还提供了一个Subscribe方法,当且仅当对象实现了IHandler ,才允许对象订阅notify事件。 使用.NET 4,下面的代码显示baseImplementer.NotifyEventHandler上的错误,指出: “No overload for ‘IHandler.NotifyEventHandler(IMsg)’ matches delegate ‘System.Action'” 问题:(使用更新的订阅方法) 一旦我将`IMsg`更改为抽象类而不是接口,为什么错误会消失? public interface IMsg { } // Doesn’t work //public abstract class IMsg { } // Does work public class Msg : IMsg { } public class Pub where T : IMsg { public event Action notify; […]

检查控件类型

我可以在页面打印时显示页面的所有控件的ID以及它们的类型 myPhoneExtTxt Type:System.Web.UI.HtmlControls.HtmlInputText 这是基于此代码生成的 foreach (Control c in page) { if (c.ID != null) { controlList.Add(c.ID +” Type:”+ c.GetType()); } } 但是现在我需要检查它的类型并访问其中的文本,如果它的类型为HtmlInput,我不太清楚如何做到这一点。 喜欢 if(c.GetType() == (some htmlInput)) { some htmlInput.Text = “This should be the new text”; } 我怎么能这样做,我想你明白了吗?

你认为“自动接口实现”在.NET / C#中会很有用吗

考虑一下: public class interface Person : IPerson { int ID { get; protected set; } string FirstName { get; set; } string LastName { get; set; } string FullName { get { return FirstName + ” ” + LastName; } } } 还有这个: public class StubPerson : IPerson { int ID { get { return […]

接口事件的实际使用

接口事件的强大function(在接口内声明事件)的一个很好的例子是什么? 大多数时候我只看到界面内部的公共抽象方法。

entity framework多态关联

我将很快使用Entity Framework作为预订系统(从头开始制作)。 我一直在做一些原型,试图弄清楚在项目启动之前我想做什么(我还在与客户讨论要求等)。 考虑这种情况: 我有预订,预订可以有相关的资源,可以预订,但这些资源可以不同,并有不同的字段等。 我之前从未真正使用EF,所以我不知道如何实现这种多态性,我在其他项目中使用它(我们一直在使用原始SQL)。 C#模型看起来像这样: public class Booking { public int BookingID { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public IRessource Ressources { get; set; } … } public interface IRessource { public int RessourceTypeId { get; set; } } public class […]

为什么接口类型列表不能接受inheritance接口的实例?

给出以下类型: public interface IPrimary{ void doBattle(); } // an ISecondary “is” an IPrimary public interface ISecondary : IPrimary { } // An implementation of ISecondary is also an IPrimary: internal class SecondaryImpl : ISecondary { // Required, since this is an IPrimary public void doBattle(){ } } 为什么我不这样做? List list = new List(); 这会导致以下编译错误: 参数类型’System.Collections.Generic.List’不能赋值参数类型’System.Collections.Generic.List’ […]

如何使用接口克服C#中的多重inheritance问题?

我知道C#不支持多重inheritance,而且解决方案是使用接口。 但我不明白的是为什么接口不像多inheritance那样创建钻石问题。 如何使用接口避免多重inheritance的陷阱?

可变类型的不可变视图

我有一个项目,我需要在执行流程之前构建大量的配置数据。 在配置阶段,将数据变为可变非常方便。 但是,一旦配置完成,我想将该数据的不可变视图传递给function过程,因为该过程将依赖于其许多计算的配置不变性(例如,基于预先计算事物的能力)初步配置。)我想出了一个可能的解决方案,使用接口来公开一个只读视图,但我想知道是否有人遇到过这种方法的问题,或者是否有其他建议如何解决这个问题。 我正在使用的模式的一个例子: public interface IConfiguration { string Version { get; } string VersionTag { get; } IEnumerable Devices { get; } IEnumerable Commands { get; } } [DataContract] public sealed class Configuration : IConfiguration { [DataMember] public string Version { get; set; } [DataMember] public string VersionTag { get; set; } [DataMember] public […]

Web服务无法序列化接口

我有这样的界面: public interface IDocument : ISerializable { Boolean HasBeenUploaded { get; set; } void ISerializable.GetObjectData(SerializationInfo, StreamingContext) { } } 有三个文件inheritance自此,所有这些文件序列化都很好。 但是当创建一个简单的Web服务时,它什么都不做,可以将它们上传到… public class DCService : System.Web.Services.WebService { [WebMethod] public Boolean ReceiveDocument(IDocument document) { DBIO io = new DBIO(); return io.InsertIntoDB(document); // does nothing; just returns true } } 我试图运行它时得到这个:“无法序列化接口IDocument” 我不太清楚为什么这会是一个问题。 我知道有些人遇到了麻烦,因为他们不想强制子类来实现自定义序列化,但我确实如此,到目前为止它已经成功了。 编辑>如果我创建接受实现接口的对象的单个web方法,它工作正常,但这削弱了客户端/服务器之间的合同(并破坏了首先拥有接口的目的)

使用接口inheritance时,为什么动态绑定失败?

在C#中,有谁知道为什么我不能做以下事情? (特别是下面标有“不好!”的行) interface A { void Add(dynamic entity); } interface B : A {} class C : B { public void Add(dynamic entity) { System.Console.WriteLine(entity); } } class Program { static void Main(string[] args) { B b = new C(); dynamic x = 23; b.Add(23); // fine b.Add((int)x); // fine (b as A).Add(x); // fine […]