Home > Software design >  how to override System.Convert.Tostring Method in c#
how to override System.Convert.Tostring Method in c#

Time:10-01

Need to override a System.Convert.ToString. i have achieved for string.Tostring() using stringextensions.is there a similar way to working for convert. please help to find a way to achieve this.

CodePudding user response:

Just implement IConvertible in your class. This is what is used by Convert internally (see source)

class MyClass : IConvertible
{
    string IConvertible.ToString(IFormatProvider provider)
    {
        return "whatever";
    }
    //   all the other IConvertible methods
}
  • Related