Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force implementation of default constructor [duplicate]

Possible Duplicate:
Interface defining a constructor signature?

I have a mixed hierarchy of classes and interfaces.

For using serialisation I need a default constructor present in each class. I would really aprreciate if the compiler could tell me that a default constructor is missing somewhere in the hierarchy. (seeing the problem at compile time, not in the later tests)

What I would like to have could be some markup or attribute, but I could not find anything.

Something like:

[ForceDefaultConstructor]
interface IVeryQuickSerializable
{   
    Serialize();
    Deserialize();
}

would be great!

But anything like that is very appreciated.

There is a limitation: I cannot change the Serialisation. Making it generic would solve the problem, but I do not have the source. Writing a wrapper might do the job, but it will have a loophole for objects deriving from the toplevel Serialisation interface (which may not be altered).

like image 631
Mare Infinitus Avatar asked Jun 20 '12 18:06

Mare Infinitus


1 Answers

You can't do that in an interface or attribute.

Two thoughts:

  • integration test: use reflection to find all relevant classes, and check them in a test
  • expose your serialization code in a generic API that uses the T : new() clause, i.e.

    void Serialize<T>(T obj, ...) where T : IVeryQuickSerializable, new()
    
like image 157
Marc Gravell Avatar answered Nov 14 '22 22:11

Marc Gravell