Tag: 接口

IList 和List 与接口的转换

我一般都了解接口,inheritance和多态,但有一件事令我困惑。 在这个例子中, Cat实现了IAnimal ,当然List实现了IList : IList cats = new List(); 但它会生成编译错误(无法隐式转换类型…) 。 如果我使用Catinheritance的asbtract超类[Animal],它也将无法工作。 但是,如果我用Cat替换IAnimal : IList cats = new List(); 它编译得很好。 在我看来,因为Cat实现了IAnimal ,所以第一个例子应该是可接受的,允许我们返回列表和包含类型的接口。 谁能解释为什么它无效? 我确信这是一个合乎逻辑的解释。

.NET工具:提取接口并实现包装类

是否有可以为现有类生成提取和生成接口的工具? 我知道Visual Studio将为现有类提取接口。 但是,我还想生成一个实现该function的包装类。 我相信这对unit testing非常有帮助。 示例现有类: public class ThirdPartyClass { public void Method1(){} public void Method2(){} } 这可以由Visual Studio(Extract Interface)生成: public interface IThirdPartyClass { void Method1(); void Method2(); } 我想更进一步: public class ThirdPartyClassWrapper : IThirdPartyClass { private tpc = new ThirdPartyClass(); public void Method1() { tpc.Method1(); } public void Method2() { tpc.Method2(); } } […]

嘲笑’System.Console’的行为

是否有一种标准方法可以通过对接口而不是System.Console进行编程来使C#控制台应用程序单元可测试? 例如,使用IConsole界面? 你做过这个,你用过什么样的方法? 您是否公开了应用程序需要写入标准输出的事件?

在Ruby中,C#中的接口相当于什么?

我目前正在尝试学习Ruby,并且我正在尝试更多地了解它在封装和合同方面提供的内容。 在C#中,可以使用接口定义合同。 实现接口的类必须通过为每个定义的方法和属性(以及可能的其他内容)提供实现来满足合同中的条款。 实现接口的单个​​类可以在合同定义的方法范围内执行所需的任何操作,只要它接受相同类型的参数并返回相同类型的结果即可。 有没有办法在Ruby中强制执行此类操作? 谢谢 我在C#中的一个简单例子: interface IConsole { int MaxControllers {get;} void PlayGame(IGame game); } class Xbox360 : IConsole { public int MaxControllers { get { return 4; } } public void PlayGame(IGame game) { InsertDisc(game); NavigateToMenuItem(); Click(); } } class NES : IConsole { public int MaxControllers { get { return 2; […]

C#接口inheritance

鉴于: public interface IA { void TestMethod(); } public interface IB : IA { } 为什么: typeof(IB).GetMethods().Count() == 0; ? 要明确一点: public class A { public void TestMethod() { } } public class B : A { } typeof(B).GetMethods().Count(); 确实有效(它返回5); 作为奖励: typeof(IB).BaseType == null

为什么C#编译器不允许接口中的私有属性设置器?

在某些情况下,比如MVVM视图模型,我有时需要私有setter,因为视图模型公开了一个只能在内部修改的状态。 那么在接口上需要私有的setter是错误的吗? (我的意思并不是特别在所描述的场景中)如果没有,为什么C#编译器不允许它? 谢谢。

C#:接口inheritancegetters / setters

我有一组接口,它们与特定的可变对象紧密结合使用。 该对象的许多用户只需要能够从对象读取值,然后只需要几个属性。 为了避免命名空间污染(更容易的智能感知)和跨越使用意图,我希望有一个小的基本接口,它只以只读方式暴露一些“关键”属性。 但是,几乎所有实现都将支持完整的界面,其中包括可修改性。 不幸的是,我遇到了在C#中表达这个概念的障碍: interface IBasicProps { public int Priority { get; } public string Name {get;} //… whatever } interface IBasicPropsWriteable:IBasicProps { public int Priority { set; } //warning CS0108: […] hides inherited member […] public string Name { set; } //… whatever } 我当然不打算隐藏任何成员,所以不好! 当然,我可以使用方法解决这个问题,但是什么是正确的选择? 我想保持“核心”接口尽可能小,即使分割接口除了传达意图之外没有任何其他目的。 使用拆分接口,很明显哪些方法不会进行任何更新,这使得编写代码更加清晰(更不用说还允许使用简单的静态单例存根,这对于很多简单的情况就足够了) 。 我想避免任何抽象类等; 他们重新实现或快速单一用途垫片更复杂和难以理解。 那么,想法?

无法实现接口成员,因为它没有匹配的返回类型List

我有接口IChild和IParent 。 IParent的成员是List 。 我希望有一些实现IParent类,其中每个类都有一个实现IChild的成员: public interface IChild { } public interface IParent { List a { get; set; } } public class ChildA : IChild { } public class ChildB : IChild { } public class ParentA : IParent { public List a { get; set; } } public class ParentB : IParent { […]

接口和类之间的区别

正如标题所述,我想知道使用这样的类之间的区别 public class Account { public string Username { get; set; } public string Password { get; set; } } 并使用和接口这样 public class Account : IAccount { public string Username { get; set; } public string Password { get; set; } } public interface IAccount { string Username { get; set; } string Password { […]

编程到接口。 如何确定需要的地方?

我知道对接口进行编程有助于松散耦合。 但是,是否有一个解释何时最有效的指南? 例如,我有一个简单的Web应用程序,它收集员工的数据,他们的培训计划,费用和计算他们一年的费用等。这是一个相当简单的应用程序,我当然可以使用一个界面,但想知道是否会有任何使用。 我将使用它为了使用它。 总是可以说,随着应用程序的复杂性增加而我传递对象,传递类型(接口)比实现更有意义。 那么,我应该等待应用程序变得复杂或立即使用它吗? 我想知道最好的做法可能会变成“这家伙过度做事”。