实现接口时出错:类没有实现接口成员。

3

我正在尝试实现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
    }

}

}

3个回答

8

这是一个接口,因此您必须实现所有成员 - 不管您想不想要。

除非您完全实现了该接口,否则此错误将不会消失。在您正在实现的方法范围内,您可以按照自己的意愿进行操作,例如引发NotImplementedException异常 - 但这就是您的实现方式,编译器会满意。

我不会容忍无知(您仍然应该学习“如何”和“为什么”),但我会提供一个提示,可能有助于您的学习,并且如果没有其他作用,可以提高您的生产力:

在Visual Studio中,当您打开要实现接口的类代码文件时,您可以让VS替您生成代码...

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

从下拉菜单中,你应该看到选项实现接口'IMyInterface',点击它,然后就大功告成了!它会自动为你生成方法的框架。


好的,如果我只需要一些方法,我可以引发NotImplementedException异常,这样更快,谢谢。 - 404Dreamer_ML
虽然这确实是正确的方法,但这并不是一个好习惯。作为同事,我会一直质疑你为什么要实现一个没有所有方法的接口。除非接口规定允许这样的实现,否则你本质上是在撒谎。 - jdmichal
如果你想快速完成,那么请查看我的自动生成更新。如果你想手动完成,则定义方法体并省略任何异常抛出将会比“添加”throw更快。 - Grant Thomas

1

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


@404Dreamer_ML 是的。除非你实现了 所有 方法,否则它会报错。这就是实现接口的意思,即你要实现它的所有方法。 - jdmichal

1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接