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

时间:2012-06-05 15:33:35

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

我有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!

3 个答案:

答案 0 :(得分:1)

不幸的是,没有。没有。 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
}

<强>更新

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

答案 1 :(得分: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; }
     }

}

答案 2 :(得分:0)

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