So the title sounds pretty odd, but there is (at least I think there is) a reason behind my madness. I want to call an method of an Interface from the class without having to create an instance of the class; exactly like a static method, but I want to add in some sort of generics I think.
interface ISaveMyself
{
Stream Save( );
// If I could force this to be static, it would fix my problem
Object Load( MyClass instance );
}
class MyClass
{
#region Implementing ISaveMyself
public Stream Save( )
{
Stream stream;
// Serialize "this" and write to stream
return stream;
}
// Implements my interface by calling my static method below
Object ISaveMyself.Load( Stream stream )
{
return MyClass.Load( stream );
}
#endregion Implementing ISaveMyself
// Static method in the class because interfaces don't allow static
public static Object Load( Stream )
{
Object currentClass = new MyClass( );
// Deserialize the stream and load data into "currentClass"
return currentClass;
}
}
And then I would have wanted to do something like this to call it:
Type myClassType = typeof( MyClass )
// This would never work, but is essentially what I want to accomplish
MyClass loadedClass = ( myClassType as ISaveMyself ).Load( stream );
I understand how stupid this question sounds, and that it is impossible to have static methods in Interfaces. But for the sake of science and the edification of society as a whole, is there a better way to do this? Thank you for your time and any suggestions.