2012-08-24 2 views
1

안녕하세요 제가 시도이옵션 보이드

public interface IPluginInterface :IEquatable<IPluginInterface> 
{ 
    string Maker { get; } 
    string Version { get; } 
    void Do(); 
    void Do_two(); 
} 

처럼 보이는 플러그인 인터페이스를 만들었습니다, 그러나 havent 한 문자열 메이커 및 버전은 선택 할 수있는 방법을 발견은, 나는 내가 설정해야합니다 생각 boolean 같음, 그러나 방법을 모른다. 도움을 주셔서 감사합니다.

답변

5

인터페이스에서 선언 한 경우 이어야합니다.

인터페이스에 선언 된 선택적 멤버를 가질 수 없습니다.

당신을 위해 몇 가지 옵션이 있습니다 :

    두 가지로 인터페이스를 브레이크
  • . 필요한 것을 구현하십시오.
  • "선택적"구성원이 비어 있고 추상이 아닌 추상 클래스를 구현합니다.
1

인터페이스 메소드를 선택 사항으로 표시 할 수 없습니다. 전체 인터페이스를 구현하거나 전혀 구현하지 않아도됩니다.

대신이 인터페이스를 두 개의 다른 인터페이스로 분리 할 수 ​​있습니다.

0

이 방법을 선택적으로 사용하려면 인터페이스가 잘못된 방법입니다. 하지만 추상적 인 기본 클래스에 넣을 수 있습니다.

1

분할 여러 가지로 인터페이스 :

public interface IPluginInterface : IEquatable<IPluginInterface> 
{ 
    string Maker { get; } 
    string Version { get; } 
} 

public interface IPluginWithOptionA : IPluginInterface 
{ 
    void Do(); 
} 

public interface IPluginWithOptionB : IPluginInterface 
{ 
    void Do_two(); 
} 

하나 이상의 인터페이스를 구현할 수 있습니다

public class MyPlugin : IPluginWithOptionA, IPluginWithOptionB 
{ 
    public bool Equals(IPluginInterface other) 
    { 
     throw new NotImplementedException(); 
    } 

    public string Maker 
    { 
     get { throw new NotImplementedException(); } 
    } 

    public string Version 
    { 
     get { throw new NotImplementedException(); } 
    } 

    public void Do_two() 
    { 
     throw new NotImplementedException(); 
    } 

    public void Do() 
    { 
     throw new NotImplementedException(); 
    } 
}