Question

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!

Was it helpful?

Solution

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

}

OTHER TIPS

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.

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().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top