1

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.

Dandruff
  • 21
  • 4
  • What you're asking for is strange. It will be better for us to answer if you can say what is the actual problem you're trying to solve? and What's wrong with instances? – Sriram Sakthivel Apr 17 '14 at 19:47
  • take a look at [this post](http://stackoverflow.com/questions/259026/why-doesnt-c-sharp-allow-static-methods-to-implement-an-interface) – tophallen Apr 17 '14 at 19:48
  • That post is perfect. It has extremely extensive thought into why Interfaces don't allow static methods. I did some research, but that post trumps it, so thank you !! – Dandruff Apr 17 '14 at 20:03
  • i had similar question http://stackoverflow.com/questions/12865049/how-to-make-pluggable-static-classes – pm100 Apr 17 '14 at 20:53

2 Answers2

0

The only way I'd think to accomplish this would be inheriting a base class instead of the interface option. Something like:

public class BaseClass
{
    public static BaseClass NewSelf()
    {
        return new BaseClass();
    }
}

public class TestClass : BaseClass
{

}

And then use it:

TestClass newItem = (TestClass)BaseClass.NewSelf();
Bill
  • 1,431
  • 10
  • 13
0

for the sake of science and the edification of society as a whole, is there a better way to do this?

Yes. Separation of concerns would indicate that you should use a different class, which can be instantiated, to load your other classes from a stream, rather than using the same class for multiple purposes.

interface ISaveObjects<T>
{
    Stream Save(T obj);
}

interface ILoadObjects<T>
{
    T Load(Stream stream);
}

public class MyClassStreamer : ISaveObjects<MyClass>, ILoadObjects<MyClass>
{
    public MyClass Load(Stream stream)
    {
        // Deserialize the stream and load data into new instance
    }

    public Stream Save(MyClass obj)
    {
        Stream stream;

        // Serialize "obj" and write to stream

        return stream;
    }
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Yes, I like your thinking. I hate the idea of separating it out and creating a class that will implement Load methods for all of "MyClass"es. But I would rather finish this project then get stuck on a rather mundane issue. Thank you +edit: I like that factory idea – Dandruff Apr 17 '14 at 19:59
  • @Dandruff: As much as you hate the idea, it's actually a good design principle. It increases the likelihood that you'll find commonality between your "stream" types and be able to reuse code better. It decreases the likelihood that a change in how you load/save objects will end up making you re-write half your code-base. – StriplingWarrior Apr 17 '14 at 20:01
  • 1
    Actually when you place it in that light, it does make a lot of sense... I've NEVER had to rewrite large of amounts of code due to my ignorance.... _cringe_ Thank you again! – Dandruff Apr 17 '14 at 20:07