Tag: 接口

用于标记类的接口或属性?

我有几个类,我希望用特定的属性标记。 我有两种方法。 一个涉及使用属性扩展类。 另一个使用空接口: 属性 public class FoodAttribute : Attribute { } [Food] public class Pizza { /* … */ } [Food] public class Pancake { /* … */ } if (obj.IsDefined(typeof(FoodAttribute), false)) { /* … */ } 接口 public interface IFoodTag { } public class Pizza : IFoodTag { /* … */ } public […]

使用接口分离读写问题的最佳方法?

最近我一直在意识到(一些人会过度使用)不可变对象的好处,以大大减少我的对象模型中的读写依赖性问题及其产生的条件和副作用,最终使代码更易于管理(一种function性编程式的。 这种做法使我创建了只读对象,这些对象在创建/构建时提供了值,然后只为外部调用者提供公共getter来访问属性。 受保护的内部和私有设置器允许在写入对象模型时保持内部控制。 在通过我的对象模型创建API时创建接口时,我已经开始考虑有关不变性的相同问题。 例如,通过在我的接口上仅提供公共getter,并将其留给实现者来决定setter以及如何处理该方面。 我正在讨论的用于实现的“只读”接口的一个例子是这个有价值的项目(仅用于演示): public interface IValuableItem { decimal Amount {get;} string Currency {get;} } 但是我想知道我应该如何提供一个允许写入的伴随接口(如果我应该) ,而不是在同一个接口中组合这些操作,以免“污染”它的不变性。 我想到了以下几个想法,就在我的脑海中。 如果没有提供我认为对每个人的利弊, 你认为最好的方法是什么? 有没有一种行业中常用的编码方法来管理这个概念? // companion writer public interface IValuableModifier { decimal Amount {set;} string Currency {set;} } 要么 // explicit methods to enforce importance of or deviance in the programming public interface IValuableModifier { void […]

switch语句是否适用于工厂方法? C#

我想返回一个接口,并在switch语句中我想设置它。 这是一个糟糕的设计吗? private IResultEntity GetEntity(char? someType) { IResultEntity entity = null; switch (someType) { case ‘L’: //life entity = new LifeEntity(); break; case ‘P’: //property entity = new PropertyEntity(); break; case ‘D’: //disability entity = new DisabilityEntity(); break; case ‘C’: //credit card entity = new CreditCardEntity(); break; } return entity; }

接口变量是否具有值类型或引用类型语义?

接口变量是否具有值类型或引用类型语义? 接口按类型实现,这些类型是值类型或引用类型。 显然, int和string实现IComparable , int是值类型, string是引用类型。 但是这个怎么样: IComparable x = 42; IComparable y = “Hello, World!”; (我试图回答的问题可能被删除了,因为它询问接口是存储在堆栈还是堆上,而且,正如我们都应该知道的那样,考虑到它们之间的值和引用类型之间的差异更具建设性。语义而不是它们的实现。有关讨论,请参阅Eric Lippert的堆栈是一个实现细节 。)

C#接口的方法的默认实现?

是否可以在C#中定义具有默认实现的接口? (这样我们就可以定义一个实现该接口的类,而无需实现该特定的默认方法)。 我知道扩展方法 (例如在此链接中解释)。 但这不是我的答案,因为有一个方法扩展如下,编译器仍然抱怨在MyClass中实现MyMethod: public interface IMyInterface { string MyMethod(); } public static class IMyInterfaceExtens { public static string MyMethod(this IMyInterface someObj) { return “Default method!”; } } public class MyClass: IMyInterface { // I want to have a default implementation of “MyMethod” // so that I can skip implementing it here } 我问这个是因为(至少就我所理解的而言)可以在Java中这样做(见这里 […]

我该如何inheritanceIDisposable?

class级名称已被更改以保护无辜者 。 如果我有一个名为ISomeInterface的接口。 我还有inheritance接口的类,FirstClass和SecondClass。 FirstClass使用必须处理的资源。 SecondClass没有。 所以问题是,我应该从IDisposableinheritance哪里? 以下两个选项似乎都不太理想: 1) 使FirstClassinheritanceIDisposable 。 然后,任何处理ISomeInterfaces的代码都必须知道是否要处理它们。 这闻起来像是与我紧密耦合。 2) 使ISomeInterfaceinheritanceIDisposable 。 然后,任何从它inheritance的类都必须实现IDisposable,即使没有任何东西可以处理。 除了注释之外,Dispose方法基本上是空白的。 #2对我来说似乎是正确的选择,但我想知道是否有其他选择。

为什么在值类型上调用显式接口实现会导致它被装箱?

我的问题与此问题有些相关: generics约束如何阻止使用隐式实现的接口装入值类型? ,但不同,因为它不需要约束来执行此操作,因为它根本不是通用的。 我有代码 interface I { void F(); } struct C : I { void IF() {} } static class P { static void Main() { C x; ((I)x).F(); } } 主要方法编译如下: IL_0000: ldloc.0 IL_0001: box C IL_0006: callvirt instance void I::F() IL_000b: ret 为什么不编译到这个? IL_0000: ldloca.s V_0 IL_0002: call instance void C::IF() IL_0007: […]

为什么C#中的基类允许实现接口契约而不inheritance它?

我偶然发现了C#的这个“特性” – 实现接口方法的基类不必从中派生出来 。 例: public interface IContract { void Func(); } // Note that Base does **not** derive from IContract public abstract class Base { public void Func() { Console.WriteLine(“Base.Func”); } } // Note that Derived does *not* provide implementation for IContract public class Derived : Base, IContract { } 会发生什么是Derived神奇地选择一个公共方法Base.Func ,并决定它将实现IContract.Func 。 这种魔力背后的原因是什么? […]

IList 和IReadOnlyList

如果我有一个需要参数的方法, 拥有Count属性 有一个整数索引器(get-only) 这个参数的类型应该是什么? 我会在.NET 4.5之前选择IList ,因为没有其他可索引的集合接口,并且数组实现它,这是一个很大的优点。 但.NET 4.5引入了新的IReadOnlyList接口,我也希望我的方法能够支持它。 如何在不违反DRY等基本原则的情况下编写此方法以支持IList和IReadOnlyList ? 编辑 :丹尼尔的回答给了我一些想法: public void Foo(IList list) => Foo(list, list.Count, (c, i) => c[i]); public void Foo(IReadOnlyList list) => Foo(list, list.Count, (c, i) => c[i]); private void Foo( TList list, int count, Func indexer) where TList : IEnumerable { // Stuff } 编辑2:或者我可以接受一个IReadOnlyList并提供这样的帮助: public static […]

为什么我不能在一个接口中放置一个委托?

为什么我不能在我的界面中添加委托?