簡體   English   中英

C#錯誤:成員名稱不能與其封閉類型相同,並且接口聲明類型

[英]C# errors: member names cannot be the same as their enclosing type and interfaces declare types

我正在嘗試為Windows Azure的appFabric服務。 我要實現EchoService,我需要通過IEchoContract接口來實現,所有這些都在服務器端。 所以我繼續這樣。

在IEchoContract.cs上

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;


namespace Service
{
[ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
interface IEchoContract
{
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }
}}

並在EchoSErvice.cs上

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
class EchoService
{
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    {
        public string Echo(string text)
        {
            Console.WriteLine("Echoing: {0}", text);
            return text;
        }
    }}}

我有兩個錯誤,我不是C#的專家,所以第一個錯誤:當我放入EchoService:IEchoContract時,我得到了

'EchoService': member names cannot be the same as their enclosing type

第二,當我將公共接口IEchoContract放入

'IEchoContract' : interfaces declare types

所以請幫忙。 謝謝。

您已經兩次聲明了接口和類-一次聲明了一次。

IEchoContract.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceContract(Name = "EchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public interface IEchoContract
    {
        [OperationContract]
        string Echo(string text);
    }
}

EchoService.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Service
{
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    {
        public string Echo(string text)
        {
            Console.WriteLine("Echoing: {0}", text);
            return text;
        }
    }
}

如果在代碼中看到,則在名為EchoService的類中有一個名為EchoSevice的類

namespace Service
{
  class EchoService
  {
    [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    public class EchoService : IEchoContract
    ...

嘗試刪除外部類,因為這里沒有任何意義

namespace Service
{
  [ServiceBehavior(Name = "EchoService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
  public class EchoService : IEchoContract
  ...

您還必須刪除外部接口,因為它們也被定義了兩次(可能是您的類最終也被定義了兩次的原因)

在EchoService.cs中,您不能調用內部類EchoService,因為它上面已經有一個類EchoService。 您需要重命名其中之一。

您需要在EchoService類中定義一個EchoService類-這是不可能的。 只需刪除外部的“ EchoService類”,就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM