有可能在继承接口时覆盖接口中的属性吗?

时间:2014-09-16 18:14:55

标签: c# asp.net-mvc-4

环境: MVC4 C# VS2010

问题: 我有一个界面,我正在考虑我的" Base"接口定义如下:

public interface ITestModel
{
    TestViewModel model { get; set; }

    void PopulateNewReferralRequestModel(Int32 ReferralTypeID, Int32 profileid, string UniqueKeyValues);
    void Save();
}

[MetadataType(typeof(MetaData_TestViewModel))]
public partial class TestViewModel
{
    public Int32 id { get; set; }
    public string name { get; set; }
    public string comments { get; set; }
}

public class MetaData_TestViewModel
{
    [Required(ErrorMessage = "ID is required")]
    [DisplayName("Primary Key")]
    public Int32 id { get; set; }
    [Required(ErrorMessage = "Name is required")]
    [DisplayName("New Name")]
    public string name { get; set; }
    [DisplayName("Comments")]
    public string comments { get; set; }
}

使用界面:

如果我做了以下所有事情,我可以按照自己的意愿行事:

public class BaseViewModel : ITestModel
{
    private TestViewModel _model;
    public TestViewModel model
    {
        get { return _model; }
        set { _model = value; }
    }

    public void PopulateNewReferralRequestModel(Int32 ReferralTypeID, Int32 profileid, string UniqueKeyValues)
    {
        model = new TestViewModel();
        model.id = ReferralTypeID;
        model.name = "TEst";
        model.comments = "This is a comment";
    }

    public void Save()
    {
        int i = 1;
        return;
    }
}

现在我想继承" Base"接口但是使用从TestViewModel继承的定义更改model属性的定义,如下所示:

public interface ITestModelB : ITestModel
{
    new TestViewModelB model { get; set; }

}

public class TestViewModelB : TestViewModel
{
    public int anotherfield { get; set; }
    public string anotherstringfield { get; set; }
}

使用界面如下:

public class AViewModel : ITestModelB
{

    private TestViewModelB _model;
    public TestViewModelB model
    {
        get
        {
            return _model;
        }
        set
        {
            _model = value;
        }
    }

    public void PopulateNewReferralRequestModel(Int32 ReferralTypeID, Int32 profileid, string UniqueKeyValues)
    {
        model = new TestViewModelB();
        model.comments = "New model created";
        model.id = 1;
        model.name = "Referral Type 1";
        model.anotherfield = profileid;
        model.anotherstringfield = UniqueKeyValues;
    }
    public void Save()
    {
        int i = 1;
        return;
    }
}    

问题是当我构建时出现以下错误:

Error   1   'InterfaceTest.Models.AViewModel' does not implement interface member 'InterfaceTest.Models.ITestModel.model'. 'InterfaceTest.Models.AViewModel.model' cannot implement 'InterfaceTest.Models.ITestModel.model' because it does not have the matching return type of 'InterfaceTest.Models.TestViewModel'.

为什么我要这样做:

我有一个推荐系统,我试图建立所有推荐具有相同基本属性和操作的推荐系统。每个引用类型还具有特定于引用类型的属性和操作。我希望引用流经相同的控制器,以便在添加新引用时,我只需创建一个新接口来处理引用。

以下是我希望能够在我的控制器中执行的操作:

ITestModel model = businessLogic.GetModel(referraltypeid);
model.PopulateNewReferralRequestModel(1, 1234, "DRC Policy, 12345678, 2/14/2014");

我会在我的businessLogic类中使用一个方法来根据引用类型确定要返回的模型类型。所有模型都将实现ITestModel接口。这样做可以让我通过同一个控制器处理所有引用,而不是每个引用类型都有多个控制器。

我需要什么 任何人都可以告诉我如何克服我在构建或建议我想要完成的其他解决方案时遇到的错误吗?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用显式接口实现来消除编译错误

    public class AViewModel : ITestModelB
    {

        private TestViewModelB _model;
        public TestViewModelB model
        {
            get
            {
                return _model;
            }
            set
            {
                _model = value;
            }
        }

        private TestViewModel newmodel;
        TestViewModel ITestModel.model
        {
            get { return newmodel; }
            set { newmodel = value; }
        }

        public void PopulateNewReferralRequestModel(Int32 ReferralTypeID, Int32 profileid, string UniqueKeyValues)
        {
            model = new TestViewModelB();
            model.comments = "New model created";
            model.id = 1;
            model.name = "Referral Type 1";
            model.anotherfield = profileid;
            model.anotherstringfield = UniqueKeyValues;
        }
        public void Save()
        {
            int i = 1;
            return;
        }
    } 

或者也许使用通用界面

public interface ITestModel<Tmodel>
{
    Tmodel model { get; set; }

    void PopulateNewReferralRequestModel(Int32 ReferralTypeID, Int32 profileid, string UniqueKeyValues);
    void Save();
}