【问题标题】:Error when Implementing Interface: class doesn't implement interface members实现接口时出错:类没有实现接口成员
【发布时间】:2011-03-18 14:39:34
【问题描述】:

我正在尝试实现 IUpdatable。

错误 1 ​​'WebRole1.InfoManager' 没有实现接口成员 'System.Data.Services.IUpdatable.ClearChanges()' 我得到的所有错误都是说我没有实现所有接口成员,但我当然实现了一些不是全部。洞口代码我没放,希望大家能看懂。

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Services;
  using Microsoft.WindowsAzure;
  using Microsoft.WindowsAzure.ServiceRuntime;
  using Microsoft.WindowsAzure.StorageClient;

  namespace WebRole1
  {
     public class InfoManager : IUpdatable
     {
      private TableServiceContext context;

    // To Generate DataConnectionString and svcClient
    private TableServiceContext GetContext()
    {
    //Implemented code
    }

    public CommentManager()
    {
        context = GetContext();
    }


    // To get my table infos
    public IQueryable<Info> Infos
    {
        get
        {
            return context.CreateQuery<Info>("Infos").AsTableServiceQuery();
        }
    }
   // Creating the resource and cheking the compatibility of the type and do an add Object 

    public Object CreateResource(string containerName, string fullTypeName)
    {
        //Implemented Code
    }

    // Return the instance of the resource represented by the object 
    public Object ResolveResource(Object resource)
    {
        return resource;
    }

    public void SaveChanges()
    {
        context.SaveChangesWithRetries();
    }

    public void setValue(Object targetResource, string propertyName, Object propertyValue)
    {
    //Implemented Code
    }

}

}

【问题讨论】:

    标签: c# visual-studio visual-studio-2010 azure azure-web-roles


    【解决方案1】:

    这是一个接口,因此您必须实现所有成员 - 无论您是否愿意。

    在您完全实现接口之前,此错误不会消失。你可以在你正在实现的方法的范围内做你想做的事,比如说甚至提出一个NotImplementedException ——但是你的实现,所以编译器很高兴。

    我不会容忍无知(您仍然应该了解如何为什么),但我会提供一个提示,可能有助于您的学习,如果没有别的,未来的生产力:

    在 Visual Studio 中,当您打开用于实现接口的类代码文件时,您可以让 VS 为您吐出代码...

    class MyClass : IMyInterface // <- hover mouse and click the drop down that appears
    

    从下拉列表中您应该会看到选项Implement Interface 'IMyInterface',单击它并瞧!它会自动为你生成骨架方法体。

    【讨论】:

    • 好的,如果我只需要一些方法,我可以引发 NotImplementedException,这样会更快,谢谢。
    • 虽然这当然是正确的方法,但它并不是一个好习惯。作为一名同事,我总是会质疑为什么你要实现一个没有所有方法的接口。你基本上是在撒谎,除非接口规定这样的实现是允许的。
    • 如果你想快点看我的自动生成更新。如果您想手动完成,那么定义方法主体并省略任何异常引发将比 添加 throw 更快。
    【解决方案2】:

    我不明白问题是什么。如果实现一个接口,则必须实现该接口中的所有方法。否则编译器会报错。

    【讨论】:

    • @404Dreamer_ML 是的。除非您实现 all 方法,否则它会报错。这就是实现接口的意思,即实现它的所有方法。
    【解决方案3】:

    我认为错误很明显:您没有实现所有接口成员,这当然是必需的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多