C#中的部分接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/734112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:05:48  来源:igfitidea点击:

Partial Interface in C#

c#

提问by Graviton

Does C# allows partial interface? i.e., in ManagerFactory1.cs class, I have

C# 是否允许部分接口?即,在 ManagerFactory1.cs 类中,我有

public partial interface IManagerFactory
{
    // Get Methods
    ITescoManager GetTescoManager();
    ITescoManager GetTescoManager(INHibernateSession session);
}

and in ManagerFactory.cs class, I have:

在 ManagerFactory.cs 类中,我有:

public partial interface IManagerFactory
{
    // Get Methods
    IEmployeeManager GetEmployeeManager();
    IEmployeeManager GetEmployeeManager(INHibernateSession session);
    IProductManager GetProductManager();
    IProductManager GetProductManager(INHibernateSession session);
    IStoreManager GetStoreManager();
    IStoreManager GetStoreManager(INHibernateSession session);
}

Both ManagerFactory and ManagerFactory1 are located in the same assembly.

ManagerFactory 和 ManagerFactory1 都位于同一个程序集中。

采纳答案by Jon Skeet

The simplest way is just to try it :)

最简单的方法就是尝试一下:)

But yes, partial interfaces are allowed.

但是,是的,允许部分接口。

Valid locations for the partialmodifier (with C# 3.0 spec references):

partial修饰符的有效位置(使用 C# 3.0 规范参考):

  • Classes (10.1.2)
  • Structs (11.1.2)
  • Interfaces (13.1.2)
  • Methods (C# 3.0+) (10.2.7; 10.6.8)
  • 类 (10.1.2)
  • 结构 (11.1.2)
  • 接口 (13.1.2)
  • 方法 (C# 3.0+) (10.2.7; 10.6.8)

Section 10.2 of the spec contains most of the general details for partial types.

规范的 10.2 节包含了部分类型的大部分一般细节。

Invalid locations:

无效位置:

  • Enums
  • Delegates
  • 枚举
  • 代表

回答by Konstantin Tarkus

Yes, it does.

是的,它确实。

Partial Classes and Methods (C# Programming Guide)at MSDN

MSDN 上的部分类和方法(C# 编程指南)

Restrictions:

限制:

  • All partial-type interface definitions meant to be parts of the same type must be modified with partial
  • The partial modifier can only appear immediately before the keyword interface.
  • All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file).
  • 所有旨在成为同一类型部分的部分类型接口定义必须修改为 partial
  • 部分修饰符只能出现在关键字之前interface
  • 所有旨在成为同一类型部分的部分类型定义必须在同一程序集和同一模块(.exe 或 .dll 文件)中定义。

Partial intefaces are primary used when code generation is involved. For example when one part of an interface is generated and the other one is user-written.

当涉及代码生成时,主要使用部分接口。例如,当界面的一部分是生成的,而另一部分是用户编写的。

回答by Anton Gogolev

Yes, it does.

是的,确实如此

回答by Kent Boogaart

Yes, it does. Are both partial interfaces defined in the same namespace?

是的,它确实。两个部分接口是否定义在同一个命名空间中?

回答by ShuggyCoUk

See the docswhich state that it can be used on classes, structs or interfaces

查看说明它可以用于类、结构或接口的文档

回答by Michael Piendl

Short and simple: YES!

简短而简单:是的!

回答by Keith

It does, but an important question would be why?

确实如此,但一个重要的问题是为什么?

Partial classes are there so that you can extend auto-generated code. VS can generate a form file, or code behind, or Linq to SQL accessor and you can extend it using a partial.

存在部分类,以便您可以扩展自动生成的代码。VS 可以生成表单文件、代码隐藏或 Linq to SQL 访问器,您可以使用部分来扩展它。

I'd avoid using partials just to split up classes (or in this case interfaces) as generally that generates more confusion than it's worth.

我会避免使用部分来拆分类(或在这种情况下是接口),因为通常会产生比其价值更多的混乱。

In this case I'd investigate why this needs to be across multiple files - factory pattern interfaces can make tracking back through you code more complex, but here you'd be tracking back through multiple files.

在这种情况下,我会调查为什么这需要跨多个文件 - 工厂模式接口可以使回溯您的代码更加复杂,但在这里您将回溯多个文件。

回答by Krzysztof Kozmic

Think twice before making your interface partial. Maybe it's better to split it into two itnerfaces?

在使您的界面部分化之前要三思。也许最好将它分成两个内部面?

Keep your interfaces small and focused. partial is a code smell.

保持你的界面小而专注。部分是一种代码气味。

回答by Kim

Nice.

好的。

I agree this could be a smell, but I can still think of one reason to do it.

我同意这可能是一种气味,但我仍然可以想到一个这样做的理由。

I'm currently working on an application MVVM framework for WPF and Silverlight. What I've encountered is that WPF and Silverlight are so different that rather than using defines all over the code, the partial interface/class can actually separate the differences between the two frameworks and still keep the code clean and nearly single sourced.

我目前正在为 WPF 和 Silverlight 开发应用程序 MVVM 框架。我遇到的是 WPF 和 Silverlight 是如此不同以至于不是在整个代码中使用定义,部分接口/类实际上可以分离两个框架之间的差异,并且仍然保持代码干净和几乎单一来源。