Extension methods are made for use with instances. I'd like to "misuse" them
as static methods, too. Let me tell you my ambition:
I use an extension method to serialize objects somehow like this:
MyObject obj = new MyObject();
obj.ToXmlFile(" some directory\\some file.xml");
The method "ToXmlFile" is defined elsewhere far apart from MyObject to work
for all objects:
public static void ToXmlFile<T>(th is T obj, string path)
{
//do the right thing
}
And of course there is another method being a "classic" static method that
reads the XML file and deserializes its contents:
public static T FromXmlFile<T>( string path)
{
//do the right thing the other way round
}
That method is called:
MyObject obj = MyXmlSerializer Class.FromXmlFi le<MyObject>("s omewhere");
Now my question is: Can you think of a possibility to define "FromXmlFil e"
in some way that allows me to call it more convenient? i.e.:
MyObject obj = MyObject.FromXm lFile("somewher e");
A solution that does also work for 3rd party objects whose code I can't
manipulate? i.e.:
TheirObject obj = TheirObject.Fro mXmlFile("somew here");
Something one might call "static extension method"?
I'd really appreciate any hints!
Thanks in advance,
Steffen
as static methods, too. Let me tell you my ambition:
I use an extension method to serialize objects somehow like this:
MyObject obj = new MyObject();
obj.ToXmlFile(" some directory\\some file.xml");
The method "ToXmlFile" is defined elsewhere far apart from MyObject to work
for all objects:
public static void ToXmlFile<T>(th is T obj, string path)
{
//do the right thing
}
And of course there is another method being a "classic" static method that
reads the XML file and deserializes its contents:
public static T FromXmlFile<T>( string path)
{
//do the right thing the other way round
}
That method is called:
MyObject obj = MyXmlSerializer Class.FromXmlFi le<MyObject>("s omewhere");
Now my question is: Can you think of a possibility to define "FromXmlFil e"
in some way that allows me to call it more convenient? i.e.:
MyObject obj = MyObject.FromXm lFile("somewher e");
A solution that does also work for 3rd party objects whose code I can't
manipulate? i.e.:
TheirObject obj = TheirObject.Fro mXmlFile("somew here");
Something one might call "static extension method"?
I'd really appreciate any hints!
Thanks in advance,
Steffen
Comment