是否可以使接口方法接受不同的类C#?

时间:2016-06-29 16:53:40

标签: c# generics inheritance interface

嗨,我想知道是否有一个很好的方法没有过度杀戮有一个接口方法,可以采取例如3个不同的模型?我正在寻找这样的事情:

 public interface IOperations 
    {
        void AddTranslation(TranslationLocationEnum Location, T model)               
    }

 public class ApplicationOne : IOperations
   {
        public void AddTranslation(TranslationLocationEnum Location, ClassType1 model)     
   }

 public class ApplicationTwo : IOperations
   {
        public void AddTranslation(TranslationLocationEnum Location, ClassType2 model)     
   }

谢谢

1 个答案:

答案 0 :(得分:3)

public interface IOperations<T> where T : class
{
    void AddTranslation(TranslationLocationEnum Location, T model);
}

public class ApplicationOne : IOperations<ApplicationOne>
{
    public void AddTranslation(TranslationLocationEnum Location, ApplicationOne model)
    {
        throw new NotImplementedException();
    }
}

public class ApplicationTwo : IOperations<ApplicationTwo>
{
    public void AddTranslation(TranslationLocationEnum Location, ApplicationTwo model)
    {
        throw new NotImplementedException();
    }
}

在继承接口时使用通用接口和传递类型。希望这会有所帮助。