Extend Types like instances?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    Extend Types like instances?

    Can one extend the type itself and not just an instance of the type?

    So I have something, say, like

    public static DateTime RawRead(this DateTime i, Stream s)
    {
    BinaryReader b = new BinaryReader(s) ;
    i = DateTime.FromBi nary(b.ReadInt6 4());
    return i;
    }


    But how I have to use it is

    DateTime d = new DateTime();
    d = d.RawRead(s);

    I'd like to be able to do

    DateTime d = DateTime.RawRea d(s);

    or even

    DateTime d = new DateTime();
    d.RawRead(s);


    In a sense I need something like

    public static DateTime RawRead(this ref DateTime i, Stream s)
    {
    BinaryReader b = new BinaryReader(s) ;
    i = DateTime.FromBi nary(b.ReadInt6 4());
    }

    But of course that doesn't work. (Or even a way to extend the type itself)

    Ultimately its not that big a deal but its a little messy to have to use the
    instance itself to call the method. (and for the fact that it creates two
    objects since I first have to instantiate it then return a new one)

    Thanks,
    Jon


  • Jon Slaughter

    #2
    Re: Extend Types like instances?

    Also, is it possible to override an extension? (I doubt it but I'd like to
    be able to override any extensions later on that I've created without having
    the source)


    Comment

    Working...