Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

Extend Types like instances?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon Slaughter - 03 Apr 2008 19:12 GMT
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.FromBinary(b.ReadInt64());
   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.RawRead(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.FromBinary(b.ReadInt64());
}

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 - 03 Apr 2008 19:16 GMT
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)
Ben Voigt [C++ MVP] - 03 Apr 2008 19:24 GMT
> 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)

Kinda.  There's a particular order in which namespaces are searched to find
extension methods.
Jon Slaughter - 03 Apr 2008 20:46 GMT
>> 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)
>
> Kinda.  There's a particular order in which namespaces are searched to
> find extension methods.

I guess my idea is bad though cause one cannot derive from a static class ;/
I was hoping there would be some way to modify some extension I previous did
if I lost the source code, for example. Say I extended DateTime but later
found a better way but lost the source code. It seems theres no way to
modify that extension? (assuming that I have to use the same name which I
would in my application)
Jon Skeet [C# MVP] - 03 Apr 2008 19:54 GMT
> I guess my idea is bad though cause one cannot derive from a static class ;/
> I was hoping there would be some way to modify some extension I previous did
> if I lost the source code, for example. Say I extended DateTime but later
> found a better way but lost the source code. It seems theres no way to
> modify that extension? (assuming that I have to use the same name which I
> would in my application)

Well, the way to do it is have a "using" directive for the namespace
containing the new set of extensions, and get rid of the one for the
old extensions.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jon Slaughter - 03 Apr 2008 21:08 GMT
>> I guess my idea is bad though cause one cannot derive from a static class
>> ;/
[quoted text clipped - 8 lines]
> containing the new set of extensions, and get rid of the one for the
> old extensions.

But this will throw out all the old extensions and I don't want to do that
since it will require rewriting them? I just want to have the ability to
modify a few if necessary, say, for specific applications.

Lets say I override 30 types and one of those is DateTime. But for a
specific application I want to change what the extension does(say instead of
binary output it does text).. I don't want to have to reimplement all the
other types or change the name(because it might break something elsewhere).

Basically I have RawRead and RawWrite. They save a type to disk. I have
extended all the value types and working on some other types. Right now I
use ToBinary on DateTime to save it and read it to a file. Now later on I
might want to use ToString and Parse for whatever reason. Or maybe there is
a bug when I write the bits to file. Since I have no way of modifying the
extension(or removing it) I'm stuck with that unless I have the source code.
(which means there is no easy way to extend it... but maybe reflection can
help?)

Seems like a bad thing to make extensions unextendable ;/ (not sure if the
is a programmatic reason or what)
Jon Skeet [C# MVP] - 03 Apr 2008 22:40 GMT
> > Well, the way to do it is have a "using" directive for the namespace
> > containing the new set of extensions, and get rid of the one for the
[quoted text clipped - 3 lines]
> since it will require rewriting them? I just want to have the ability to
> modify a few if necessary, say, for specific applications.

Then you'd be well advised to keep different extensions in different
namespaces.

Personally I don't like the way extension method discovery worked - I
wish you had to specify the exact *class* to get stuff from instead of
a namespace. Basically at the moment you're well advised to keep to a
few extended types per namespace, if not just one.

> Lets say I override 30 types and one of those is DateTime. But for a
> specific application I want to change what the extension does(say instead of
[quoted text clipped - 9 lines]
> (which means there is no easy way to extend it... but maybe reflection can
> help?)

One lesson there is to keep source code!

> Seems like a bad thing to make extensions unextendable ;/ (not sure if the
> is a programmatic reason or what)

I can't easily think of an *elegant* way to do it, to be honest -
beyond what I suggested earlier.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jon Slaughter - 04 Apr 2008 03:23 GMT
>> > Well, the way to do it is have a "using" directive for the namespace
>> > containing the new set of extensions, and get rid of the one for the
[quoted text clipped - 40 lines]
> I can't easily think of an *elegant* way to do it, to be honest -
> beyond what I suggested earlier.

But I might want to distribute the code or use it in a different program
without worrying about the source code. I don't think namespaces can help
here because ultimately it still requires me to redefine all extensions.

What I mean is, suppose I have some extension to Int32 and I write an app
that uses that extension. Then I want to modify the extensions internal
workings which does not effect the apps behavior in the least bit... but
this can't be done!?!?! Why? Because I can't use the same name or I'll have
to recode all the extensions. (since you get all or nothing... unless I had
one extension per namespace ;/)

Another example:

suppose I have 20 extensions to the basic types all in the same namespace
and an app that uses them all. I want to modify the internals of just one
but still have it work with the app. Can't be done unless I recode them all
or use a different name.

Now I suppose I could replace method names in all the files but seems like
a.s backwards way to do it.

I suppose it would be better to have a way to tell the compiler which
extension to use when there is a collision. This would solve the problem and
probably could be done quite easily using an attribute or something.
Jon Skeet [C# MVP] - 04 Apr 2008 08:18 GMT
> > I can't easily think of an *elegant* way to do it, to be honest -
> > beyond what I suggested earlier.
[quoted text clipped - 9 lines]
> to recode all the extensions. (since you get all or nothing... unless I had
> one extension per namespace ;/)

It sounds like really you just want to rebuild the class library with
the replaced internals. This is no different to any other peice o code
versioning - how would you do it if you wanted to replace the
implementation of a normal instance method?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.