none
Extending System.Convert Class RRS feed

  • Question

  • Hi,

    I try to extend the System.Convert classe to add for exemple a methode to convert Image objet to byte[] (wich is pretty useful to store image in SQL db). So here is my code :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.Drawing;
    using System.IO;
    
    namespace Test
    {
        public static partial  class Convert
        {
            public static byte[] ToBytes (Image i)
            {
                try
                {
                    MemoryStream ms = new MemoryStream();
                    i.Save(ms, i.RawFormat);
                    return ms.ToArray();
                }
                catch (Exception){throw new FormatException();}
            }
        }
    }

    And unfortunately it does not work as I expect because if you want to use usual Convert.toString() you have to specify System.Convert.toString()...

    I try : 

    namespace System
    {
        public static partial  class Convert
        {

    It's worse...

    namespace Test
    {
        public static class AllConvert : System.Convert
        {

    not more... 

    Any idea ?

    Reds, Pascal.


    Wednesday, August 15, 2012 9:22 PM

Answers

  • Thanks for so quick answer...

    But then is there anyway to inherit from from Convert Class to to do somthing like AllConvert ?

    No - The methods in convert are static (and convert is a static class), so there is no way to inherit from it.  You can just make your own conversion class, and use it.  (Note that this is likely better, anyways, as it makes it clear that you're not using the System classes, which makes your API more discoverable and maintainable.)

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 15, 2012 9:38 PM
    Moderator

All replies

  • There is no way to extend a class at runtime.  Partial classes only work within the same assembly.

    The best you could do would be to make your own Conversion class, and use that.  It will need to have a different (fully qualified) name, though, so you wouldnt' be able to use "System.Convert" .


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 15, 2012 9:25 PM
    Moderator
  • Thanks for so quick answer...

    But then is there anyway to inherit from from Convert Class to to do somthing like AllConvert ?

    Wednesday, August 15, 2012 9:30 PM
  • Thanks for so quick answer...

    But then is there anyway to inherit from from Convert Class to to do somthing like AllConvert ?

    No - The methods in convert are static (and convert is a static class), so there is no way to inherit from it.  You can just make your own conversion class, and use it.  (Note that this is likely better, anyways, as it makes it clear that you're not using the System classes, which makes your API more discoverable and maintainable.)

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 15, 2012 9:38 PM
    Moderator