S
Steffen Bobek
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>(this 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 = MyXmlSerializerClass.FromXmlFile<MyObject>("somewhere");
Now my question is: Can you think of a possibility to define "FromXmlFile"
in some way that allows me to call it more convenient? i.e.:
MyObject obj = MyObject.FromXmlFile("somewhere");
A solution that does also work for 3rd party objects whose code I can't
manipulate? i.e.:
TheirObject obj = TheirObject.FromXmlFile("somewhere");
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>(this 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 = MyXmlSerializerClass.FromXmlFile<MyObject>("somewhere");
Now my question is: Can you think of a possibility to define "FromXmlFile"
in some way that allows me to call it more convenient? i.e.:
MyObject obj = MyObject.FromXmlFile("somewhere");
A solution that does also work for 3rd party objects whose code I can't
manipulate? i.e.:
TheirObject obj = TheirObject.FromXmlFile("somewhere");
Something one might call "static extension method"?
I'd really appreciate any hints!
Thanks in advance,
Steffen