简体   繁体   中英

Partial Interface in C#

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

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

and in ManagerFactory.cs class, I have:

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.

The simplest way is just to try it :)

But yes, partial interfaces are allowed.

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

  • Classes (10.1.2)
  • Structs (11.1.2)
  • Interfaces (13.1.2)
  • Methods (C# 3.0+) (10.2.7; 10.6.8)

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

Invalid locations:

  • Enums
  • Delegates

Yes, it does.

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

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 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.

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.

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.

是的, 确实如此

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.

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.

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

请参阅说明可以在类,结构或接口上使用的文档

简短:是的!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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