C# C中两个几乎相同的web引用的接口#

C# C中两个几乎相同的web引用的接口#,c#,.net-3.5,interface,web-reference,C#,.net 3.5,Interface,Web Reference,我有两个无法更改的web参考: 它们几乎相同,但引用时,一个只接受ProperCase,另一个只接受大写 范例 不仅仅是道具,还有整个类及其道具和方法 @编辑:对不起,我意识到这比最初说的要复杂: 不仅仅是道具,还有整个类及其道具和方法和内部类。虽然只用作结构,但内部类也有同样的问题 公共类Foobar { 公共字符串Logmsgno; 公共字符串Revno; 公共字符串Reqsox; 公共无效剂量(); 公共巴尔巴兹-米巴尔巴兹; 公共列表Myquuxlist; } 另一个名字是 publi

我有两个无法更改的web参考:

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

范例

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

@编辑:对不起,我意识到这比最初说的要复杂:

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

公共类Foobar
{
公共字符串Logmsgno;
公共字符串Revno;
公共字符串Reqsox;
公共无效剂量();
公共巴尔巴兹-米巴尔巴兹;
公共列表Myquuxlist;
}
另一个名字是

public class FooBar
{
     public string LogMsgNo;
     public string RevNo;
     public string ReqSox;
     public void DoSomething();
     public BarBaz MyBarBaz;
     public List<Quux> MyQuuxList;
}
公共类FooBar
{
公共字符串LogMsgNo;
公共字符串RevNo;
公共字符串ReqSox;
公共无效剂量();
公共巴尔巴兹-米巴尔巴兹;
公共列表MyQuuxList;
}
有没有一种简单的方法来为两者创建一个接口


蒂亚

不幸的是,没有。没有。C#区分大小写(包括接口)。要使它们都符合单个接口,名称大小写必须匹配。如果你这样做了,那么这些类无论如何都是一样的

您唯一的选择是创建一个使用其中一个casing方法的接口,在两个类上实现它,然后向一个类添加代码(使用您没有选择的命名约定)以传递调用:

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
}
更新


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

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

,而无需进行适当的重新分解来更新所有内容和更改名称,是的,您可以使用一些冒烟和镜像。基于您想要的新值创建一个接口,然后将其更改为分别使用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; }
     }

}
公共接口IYourBarBazInterface
{
字符串1{get;set;}
字符串另一个属性{get;set;}
}
公共接口
{
int-quxprop{get;set;}
字符串AnotherQuuxProp{get;set;}
}
公共接口IYourCommonInterface
{
字符串LogMsgNo{get;set;};
字符串RevNo{get;set;};
字符串ReqSox{get;set;};
//声明的类似原理,但接口类型的对象
IYourBarBazInterface MyBarBaz{get;set;}
列出MyQuuxList;
无效剂量();
}
公共类Foobar:IYourCommonInterface
{
公共字符串Logmsgno;
公共字符串Revno;
公共字符串Reqsox;
公共无效剂量();
//现有的旧版本保持相同的名称上下文
//但显示各自的公共“接口”
公共IYourBarBazInterface mybarbaz;
public List myquoxlist=新列表();
//这些是接口的实现。。。
公共字符串LogMsgNo
{get{return Logmsgno;}
设置{Logmsgno=value;}
}
公共字符串RevNo
{get{return Revno;}
设置{Revno=value;}
}
公共字符串请求
{get{return Reqsox;}
设置{Reqsox=value;}
}
公共无效剂量测定法()
{Dosomething();}
//现在,“IYourCommonInterface”的公共接口
//通过公共命名构造标识公共元素的。
//你的第二节课也一样。
公共IYourBarBazInterface MyBarBaz
{get{return mybarbaz;}
设置{mybarbaz=value;}
}
公共列表MyQuuxList
{获取{返回myQuuxlist;}
设置{myquxlist=value;}
}
}
公共类FooBar:IYourCommonInterface
{
//由于此版本具有您想要的正确命名结构,
//将原始属性更改为小写起始字符
//因此,getter/setter所需的接口将被正确地限定
公共字符串logMsgNo;
公共字符串revNo;
公共字符串reqSox;
公共IYourBarBazInterface MyBarbaz;
公共列表Myquuxlist;
//这些是接口的实现。。。
公共字符串LogMsgNo
{get{return logMsgMo;}
设置{logMsgNo=value;}
}
公共字符串RevNo
{get{return revNo;}
设置{revNo=value;}
}
公共字符串请求
{get{return reqSox;}
设置{reqSox=value;}
}
//因为您的“DoSomething()”方法已正确区分大小写
//格式,您可以不使用此版本
公共无效剂量测定法()
{..做任何事..}
公共IYourBarBazInterface MyBarBaz
{get{return MyBarbaz;}
设置{MyBarbaz=value;}
}
公共列表MyQuuxList
{获取{返回myquuxlist;}
设置{myquxlist=value;}
}
}

我知道是这样,但是。。。有没有办法把两者都封装起来?你打算做多少工作?您可以创建一个与其中一个上的大小写匹配的接口,然后在另一个上使用显式接口实现。在VB.NET中不是也会发生同样的问题吗?@Mehrdad idk,vbnet不区分大小写吗?如果明天出现一个新类fooBar,我应该会将遗产添加到该新类中,仅此而已,不是吗?我应该明确指出,当使用时,或者instanc
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; }
     }

}