【问题标题】:Interface for two almost identical web references in C#C# 中两个几乎相同的 Web 引用的接口
【发布时间】:2012-06-05 15:33:35
【问题描述】:

我有 2 个无法更改的网络参考:

它们几乎相同,但引用时一个只接受 ProperCase 而另一个接受 Uppercamelcase

例子

不仅是道具,还有整个类及其道具和方法

@EDIT:对不起,我意识到它比最初说的要复杂:

不仅是道具,还有整个类及其道具和方法和内部类。虽然只用作结构,但内部类也有同样的问题。

public class Foobar
{
     public string Logmsgno;
     public string Revno;
     public string Reqsox;
     public void Dosomething();
     public Barbaz Mybarbaz;
     public List<quux> Myquuxlist;
}

另一个有类似的名字

public class FooBar
{
     public string LogMsgNo;
     public string RevNo;
     public string ReqSox;
     public void DoSomething();
     public BarBaz MyBarBaz;
     public List<Quux> MyQuuxList;
}

有没有一种简单的方法可以为两者制作界面?

TIA!

【问题讨论】:

  • 我应该明确指出,我猜是在使用或实例化时。我不会实例化接口..IFooBar myElem = new FooBar();IFooBar myElem = new Foobar(); idk,我对新想法持开放态度...也许这不是一个接口,我应该考虑一个抽象类?

标签: c# .net-3.5 interface web-reference


【解决方案1】:

如果没有适当的重构来更新所有内容并更改名称,是的,您可能会遇到一些烟雾和镜子。根据您希望它们成为的新值创建一个接口,然后将它们更改为分别使用 getter/setter 来保留原始值而不破坏它。

为了扩展您的扩展问题。您还必须调整每个级别。为“Barbaz”和“BarBaz”类定义一个接口,以便你的外部类可以有一个

的对象
public interface IYourBarBazInterface
{
     string BarBazProp1 { get; set; }
     string AnotherProp { get; set; }
}

public interface IQuux
{
    int QuuxProp { get; set; }
    string AnotherQuuxProp { get; set; }
}

public interface IYourCommonInterface
{
     string LogMsgNo { get; set; };
     string RevNo { get; set; };
     string ReqSox { get; set; };

     // Similar principle of declarations, but interface typed objects
     IYourBarBazInterface MyBarBaz { get; set; }
     List<IQuux> MyQuuxList;
     void DoSomething();
}



public class Foobar : IYourCommonInterface
{
     public string Logmsgno;
     public string Revno;
     public string Reqsox;
     public void Dosomething();

     // your existing old versions keep same name context
     // but showing each of their respective common "interfaces"
     public IYourBarBazInterface mybarbaz;
     public List<IQuux> myQuuxlist = new List<IQuux>();


     // these are the implementations of the interface...
     public string LogMsgNo
     { get { return Logmsgno; }
       set { Logmsgno = value; }
     }

     public string RevNo
     { get { return Revno; }
       set { Revno = value; }
     }

     public string ReqSox
     { get { return Reqsox; }
       set { Reqsox = value; }
     }

     public void DoSomething()
     { Dosomething(); }

     // Now, the publicly common Interface of the "IYourCommonInterface"
     // that identify the common elements by common naming constructs.
     // similar in your second class.
     public IYourBarBazInterface MyBarBaz 
     { get { return mybarbaz; }
       set { mybarbaz = value; }
     }

     public List<IQuux> MyQuuxList
     { get { return myQuuxlist; }
       set { myQuuxlist = value; }
     }
}


public class FooBar : IYourCommonInterface
{
     // since THIS version has the proper naming constructs you want,
     // change the original properties to lower case start character
     // so the interface required getter/setter will be properly qualified

     public string logMsgNo;
     public string revNo;
     public string reqSox;

     public IYourBarBazInterface MyBarbaz;
     public List<IQuux> Myquuxlist;



     // these are the implementations of the interface...
     public string LogMsgNo
     { get { return logMsgMo; }
       set { logMsgNo = value; }
     }

     public string RevNo
     { get { return revNo; }
       set { revNo = value; }
     }

     public string ReqSox
     { get { return reqSox; }
       set { reqSox = value; }
     }


     // Since your "DoSomething()" method was already proper case-sensitive
     // format, you can just leave THIS version alone
     public void DoSomething()
     { .. do whatever .. }



     public IYourBarBazInterface MyBarBaz 
     { get { return MyBarbaz; }
       set { MyBarbaz = value; }
     }

     public List<IQuux> MyQuuxList
     { get { return myquuxlist; }
       set { myquuxlist = value; }
     }

}

【讨论】:

  • 不错且完整的答案 DRapp。请检查我的问题编辑,我里面有课程。虽然只用作结构,但对我来说知道这如何改变分辨率很重要。
  • @apacay,请参阅修订后的答案。简而言之,您基本上需要应用相同的技术来识别公开的接口的任何常见“嵌套”。让原始不匹配的大小写敏感的代码保持完整,但现在所有其他新代码都可以引用“新”正确的 CamelCase 命名约定。
【解决方案2】:

很遗憾,没有。没有。 C# 区分大小写(包括接口)。为了让它们都符合单个接口,名称大小写必须匹配。如果你这样做了,那么无论如何课程都是一样的。

您唯一的选择是创建一个使用其中一种封装方法的接口,在两个类上实现它,然后将代码添加到一个类(使用您未选择的命名约定)以通过调用:

public interface IFooBar
{
    string LogMsgNo { get; set; }
    string RevNo { get; set; }
    string ReqSox { get; set; }
    void DoSomething();
}

public class Foobar : IFooBar
{
    public string Logmsgno;
    public string Revno;
    public string Reqsox;
    public void Dosomething();

    public string LogMsgNo
    {
        get { return Logmsgno; }
        set { Logmsgno = value; }
    }

    // And so on
}

更新

看到您的编辑后,事情变得更加复杂。您必须对所有内部类执行相同的操作,然后让您的接口引用较低级别的接口。相同的概念,只是更多的工作。

【讨论】:

  • 我知道是这样,但是......有什么方法可以封装两者吗?
  • 您希望完成多少工作?您可以在其中一个上创建一个匹配大小写的接口,然后在另一个上使用显式接口实现。
  • 在VB.NET中不会出现同样的问题吗?
  • @Mehrdad idk,vbnet 不区分大小写吗?
  • 如果明天出现一个新类 fooBar,我应该将遗产添加到那个新类中,就是这样,不是吗?
【解决方案3】:

如果我必须处理这个问题,我可能会编写一个扩展方法来从一种类型转换为另一种类型。一些反思将完成大部分工作。 new Foobar().ToFooBar().ToFoobar() 或者编写一个我会一直与之交互的类,最后你需要访问正确的实现,调用ToFoobar()

【讨论】:

  • 所以你是说任何时候你想使用其中之一,你都必须添加.ToFoobar()?
  • 是的,您可以随时转换为在给定时间需要处理的内容。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 2020-09-16
  • 1970-01-01
  • 2016-09-12
  • 2021-03-07
相关资源
最近更新 更多