简体   繁体   English

如何处理不允许在c#中定义接口中的类型

[英]How to deal with not being allowed to define types in an interface in c#

I often find the need to do something along these lines: 我经常发现需要按照以下方式做点什么:

public class OperationResult
{
  bool succes;
  SomeOtherObject someobject;
}


public interface ICanDoSomethingWeird
{
  OperationResult DoMyThing();
}

Where the OperationResult is really a class that belongs to the ICanDoSomethingWeird interface. OperationResult实际上是属于ICanDoSomethingWeird接口的类。 I find it very annoying I cannot place it in the namespace of the interface. 我觉得很烦人我不能把它放在界面的命名空间中。 I'm wondering how other people deal with this. 我想知道别人怎么处理这件事。 Do you just stick it in the global namespace? 你只是把它贴在全局命名空间中吗? or just one namespace up from the namespace where the interface sits? 或者从接口所在的命名空间中只有一个命名空间?

My current approach is to do rename the OperationResult class to ICanDoSomethingWeird_OperationResult, but am not very impressed by how pretty that is :) 我目前的方法是将OperationResult类重命名为ICanDoSomethingWeird_OperationResult,但是对于它的漂亮程度并没有给我留下太深刻的印象:)

Anybody have a better solution? 有人有更好的解决方案吗?

If OperationResult is a type used solely by ICanDoSomethingWeird and clients of ICanDoSomethingWeird , then it belongs in the same "context" as ICanDoSomethingWeird . 如果OperationResult是仅由所使用的类型ICanDoSomethingWeird和客户ICanDoSomethingWeird ,然后它属于相同的“上下文”为ICanDoSomethingWeird That is, it belongs in the same namespace as ICanDoSomethingWeird : 也就是说,它与ICanDoSomethingWeird 属于 同一名称空间:

namespace MyNamespace
{
    public class OperationResult {}

    public interface ICanDoSomethingWeird
    {
        OperationResult DoMyThing();
    }
}

Think about the client code. 想想客户端代码。 Would you prefer this: 你更喜欢这个:

using MyNamespace;

ICanDoSomethingWeird myWeirdThing = ...;
ICanDoSomethingWeird.OperationResult result = myWeirdThing.DoMyThing();

or this: 或这个:

using MyNamespace;

ICanDoSomethingWeird myWeirdThing = ...;
OperationResult result = myWeirdThing.DoMyThing();

The latter makes more sense to me, and is the only option where interfaces are concerned. 后者对我来说更有意义,并且是接口所关注的唯一选择。 You cannot declare inner types in interfaces. 您不能在接口中声明内部类型。 And do note the general advice regarding nested types: 并注意有关嵌套类型的一般建议

DO NOT use nested types if the type is likely to be referenced outside of the containing type. 如果类型可能在包含类型之外引用,请不要使用嵌套类型。

An interface does not define any implementation. 接口不定义任何实现。 By including a class your interface is effectively including some implementation. 通过包含一个类,您的接口实际上包括一些实现。 Nested classes have the purpose of assisting the class implementation that defines it. 嵌套类的目的是协助定义它的类实现。

Nested classes should not be used as if the class has merit outside of its use with the containing class. 不应该使用嵌套类,就好像该类在使用包含类之外具有优点。 A class should not be used as a surrogate for a namespace to define other classes. 不应将类用作命名空间的代理来定义其他类。

If an interface defines some behaviour that requires the use of a class that as yet does not exist it would make sense to create that class in the same namespace to which the interface belongs. 如果接口定义了一些需要使用尚不存在的类的行为,那么在接口所属的同一名称空间中创建该类是有意义的。

You could define the interface as an abstract class instead and then define a public inner class. 您可以将接口定义为抽象类,然后定义公共内部类。

 public interface ICanDoSomething

 public abstract class AbstractCanDoSomething
 {
     public class OperationResult
     {
          public bool SomeValue { get; set; }
          public int SomeOherVAlue { get; set; }
     }

     public abstract OperationResult DoMyThing();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM