.NET Class Interface, Inheritance and Library: error does not implement interface member

好久不见. 提交于 2019-12-25 06:25:50

问题


I want to do this (I'm on silverlight but nothing specific so want to do this also on winform and wpf)

namespace MyComponents
{
    public class IMyManager : ILibManager
    {
        void SetModel(ILibModel model);
    }
}

but get this error

Error 2 'MyComponents.IMymanager' does not implement interface member 'lib.manager.ILibManager.SetModel(lib.model.ILibModel)'. 'MyComponents.IMymanager.SetModel(lib.model.ILibModel)' cannot implement an interface member because it is not public. C:...\MyComponents\MyComponents\IMymanager.cs 17 18 MyComponents

Why ? This is the code in Lib

using lib.model;

using System;
using System.Collections.Generic;
using System.Text;

namespace lib.manager
{
    public interface ILibManager
    {
        public void SetModel(ILibModel model);
    }
}

using lib.model;

using System;
using System.Net;
using System.Windows;


namespace lib.manager
{
    public class Manager: IManager
    {
        // Constructor
        public Manager() { 

        }

        public void SetModel(ILibModel model) {

        }

    }
}

namespace lib.model
{
    public interface ILibModel
    {

    }
}


namespace lib.model
{
    public class Model : ILibModel
    {

    }
}

回答1:


I believe you had two errors here, didn't you? there should be an error saying that SetModel should have a body because IMyManager isn't an interface or an abstract class!

So, I believe you should have a body for that method, and then it has to be "public" since it's part of an implementation of an interface. And you should also rename IMyManager to be "MyManager", since it's not an interface. you should have your class like this:

public class MyManager : ILibManager
{
    public void SetModel(ILibModel model)
    {
        // implementation of SetModel
    }
}

Hope this helps :)




回答2:


Try this instead:

namespace MyComponents
{
    public class MyManager : ILibManager
    {
        public void SetModel(ILibModel model)
        {
           // ...
        }
    }
}

A class that conforms to an interface (contract!) must implement it in a public manner.




回答3:


You might also try explicit implementation, such as:

public class MyManager : ILibManager
{
    void ILibManager:SetModel(ILibModel model)
    {
        // ...
    }
}


来源:https://stackoverflow.com/questions/5729153/net-class-interface-inheritance-and-library-error-does-not-implement-interfac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!