c# - C# 中两个几乎相同的 Web 引用的接口(interface)

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

我有 2 个无法更改的网络引用:

它们几乎相同,但在引用时,一个仅接受 ProperCase,另一个则接受 Uppercamelcase

示例

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

@编辑:抱歉,我意识到它比最初所说的更复杂:

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

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!

最佳答案

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

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

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; }
     }

}

关于c# - C# 中两个几乎相同的 Web 引用的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900301/

相关文章:

c# - 访客模式 "does not implement interface"错误

c# - 执行后处置ReactiveCommand

excel - 有没有一种方法可以仅使用 C# 而无需使用 VBA 来捕获 Excel VSTO 中的热键/快捷方式?

android - 在 Activity 中获取 Fragment 实例

haskell - 如何在 Haskell 中模拟继承?

python - 使用 ctypes 将 python IplImage 对象作为简单结构传递给共享 C 库

c# - 在 C# 中转义字符串文字中的双引号

c# - 解决构造函数中的虚拟成员调用

c# - 逗号分隔数字或数字的正则表达式

.net - .Net Framework 4.0 安装程序是否包含 .Net Framework 3.5?