![](/img/trans.png)
[英]“Class does not implement interface member” error on class implementing a generic interface
[英].NET Class Interface, Inheritance and Library: error does not implement interface member
我想這樣做(我使用的是Silverlight,但沒有具體說明,所以也想在Winform和WPF上這樣做)
namespace MyComponents
{
public class IMyManager : ILibManager
{
void SetModel(ILibModel model);
}
}
但是得到這個錯誤
錯誤2'MyComponents.IMymanager'未實現接口成員'lib.manager.ILibManager.SetModel(lib.model.ILibModel)'。 'MyComponents.IMymanager.SetModel(lib.model.ILibModel)'無法實現接口成員,因為它不是公共的。 C:... \\ MyComponents \\ MyComponents \\ IMymanager.cs 17 18 MyComponents
為什么呢 這是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
{
}
}
我相信您在這里有兩個錯誤,不是嗎? 應該有一個錯誤,說SetModel應該有一個主體,因為IMyManager不是接口或抽象類!
因此,我認為您應該為該方法提供一個主體,然后它必須是“公共的”,因為它是接口實現的一部分。 並且您還應該將IMyManager重命名為“ MyManager”,因為它不是接口。 您應該像這樣上課:
public class MyManager : ILibManager
{
public void SetModel(ILibModel model)
{
// implementation of SetModel
}
}
希望這可以幫助 :)
嘗試以下方法:
namespace MyComponents
{
public class MyManager : ILibManager
{
public void SetModel(ILibModel model)
{
// ...
}
}
}
符合接口(合同!)的類必須以公共方式實現。
您也可以嘗試顯式實現,例如:
public class MyManager : ILibManager
{
void ILibManager:SetModel(ILibModel model)
{
// ...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.