简体   繁体   English

.NET类接口,继承和库:错误未实现接口成员

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

I want to do this (I'm on silverlight but nothing specific so want to do this also on winform and wpf) 我想这样做(我使用的是Silverlight,但没有具体说明,所以也想在Winform和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)'. 错误2'MyComponents.IMymanager'未实现接口成员'lib.manager.ILibManager.SetModel(lib.model.ILibModel)'。 'MyComponents.IMymanager.SetModel(lib.model.ILibModel)' cannot implement an interface member because it is not public. 'MyComponents.IMymanager.SetModel(lib.model.ILibModel)'无法实现接口成员,因为它不是公共的。 C:...\\MyComponents\\MyComponents\\IMymanager.cs 17 18 MyComponents C:... \\ MyComponents \\ MyComponents \\ IMymanager.cs 17 18 MyComponents

Why ? 为什么呢 This is the code in Lib 这是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
    {

    }
}

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! 应该有一个错误,说SetModel应该有一个主体,因为IMyManager不是接口或抽象类!

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. 并且您还应该将IMyManager重命名为“ MyManager”,因为它不是接口。 you should have your class like this: 您应该像这样上课:

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

Hope this helps :) 希望这可以帮助 :)

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. 符合接口(合同!)的类必须以公共方式实现。

You might also try explicit implementation, such as: 您也可以尝试显式实现,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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