2

I have 2 web refs which I can't change:

They are almost identical but when referenced one only accepts ProperCase and the other Uppercamelcase.

Example

Not only props is the thing but entire classes with its props and methods

@EDIT: Sorry, I've realized it's more complicated than initially stated:

Not only props is the thing but entire classes with its props and methods and inner classes. Although only used as structures, inner classes have the same issue.

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

And the other has names like

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

Is there an easy way to make an interface for both?

TIA!

apacay
  • 1,702
  • 5
  • 19
  • 41
  • I should explicit that, when used, or instanced I guess. I won't instance the interface.. `IFooBar myElem = new FooBar();` or `IFooBar myElem = new Foobar();` idk, I'm open to new ideas... maybe this isn't an *interface* and I should be thinking on an *abstract class*? – apacay Jun 05 '12 at 15:45

3 Answers3

1

Unfortunately, no. There's not. C# is case sensitive (including interfaces). To have them both conform to a single interface, the name case would have to match. If you did that, the classes would be the same anyway.

Your only option would be to create an interface that used one of the casing methods, implement it on both classes, and then add code to one class (with the naming convention you didn't chose) to pass through the calls:

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
}

UPDATE

After seeing your edit, things become much more complex. You'll have to do the same thing to all of the inner classes and then have your interfaces reference the lower level interfaces. Same concept, just more work.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • I know it is but... any idea of a way to encapsulate both? – apacay Jun 05 '12 at 15:38
  • How much work are you looking to do? You can create an interface that matches the case on one of them and then use explicit interface implementation on the other. – antijon Jun 05 '12 at 15:41
  • Doesn't the same problem happen in VB.NET? – user541686 Jun 05 '12 at 15:45
  • @Mehrdad idk, isn't vbnet case insensitive? – apacay Jun 05 '12 at 15:49
  • If tomorrow appears a new class fooBar, I would supposedly add the heritage to that new class and that's it, isn't it? – apacay Jun 05 '12 at 15:55
  • @apacay: That's why I asked. I don't have it handy, but I don't think it will work... in which case, I'm not sure it's a C# issue. – user541686 Jun 05 '12 at 15:56
  • I'll go with this, and see where it leads me. I'll be notifying you soon – apacay Jun 05 '12 at 16:08
  • Please check my question edit, I have classes inside them. Although only used as structures, it's important for me to know how does this change the resolution – apacay Jun 05 '12 at 16:40
1

Without a proper re-factoring to update everything and changing names, yes, you COULD with a little bit of smoke and mirrors. Create an interface based on the NEW values you WANT them to be, then change them to respectively use getter/setter to retain original and not break it.

To expand from your expanded question. You would have to adjust each of those levels too.. Define an interface for the "Barbaz" and "BarBaz" class so your outer class can have an object of

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
  • 47,638
  • 12
  • 72
  • 142
  • Nice and complete answer DRapp. Please check my question edit, I have classes inside them. Although only used as structures, it's important for me to know how does this change the resolution. – apacay Jun 05 '12 at 16:23
  • @apacay, see revised answer. In short, you basically need to apply the same technique to identify whatever common "nesting" of interfaces being exposed. Have the original mismatched case-sensitive to leave any ORIGINAL code unbroken, but now all other new code can reference the "New" proper CamelCase naming convensions. – DRapp Jun 05 '12 at 18:22
0

If I had to handle this, I would likely write an extension method to convert from one type to another. Some reflection would do most of the work. new Foobar().ToFooBar().ToFoobar() Or write a class I would always interact with and at the last point you need to access the right implementation, call the ToFoobar().

Travis
  • 10,444
  • 2
  • 28
  • 48