5

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).

Community
  • 1
  • 1
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • Assuming that everything descends from `Object`, it already has a default constructor. Perhaps I'm missing something here? – 3Dave Jun 20 '12 at 18:53
  • @DavidLively constructors aren't inherited; `class Foo { public Foo(int i) {...}` does **not** have a parameterless constructor – Marc Gravell Jun 20 '12 at 18:53
  • I'd like to suggest reading this: http://social.msdn.microsoft.com/forums/en-US/csharplanguage/thread/f6364918-fc7f-4153-b020-6ce14b34820c Although they provide something like a solution, that uses generics. – nxu Jun 20 '12 at 18:56
  • already saw that one, but there he wants some parameters, i especially want >no< parameters. – Mare Infinitus Jun 20 '12 at 18:57
  • @MarcGravell constructors for base classes *are* called (inherited) when you instantiate a child class. You only need to declare a constructor `Foo() {}' when you've added a constructor that requires parameters. – 3Dave Jun 20 '12 at 19:06
  • The problem is, that i cannot construct a Foo if it doesn't have a default constructor in serialisation. the base default constructor will not help me out. – Mare Infinitus Jun 20 '12 at 19:08
  • @MareInfinitus - ah - I didn't get that from your question. "Default constructor" is a bit ambiguous. – 3Dave Jun 20 '12 at 19:11

2 Answers2

7

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()
    
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Writing a wrapper is a partial solution, but maybe the fastest. Thank you. – Mare Infinitus Jun 20 '12 at 18:58
  • +1 for integration tests, I made a similar use of them at several times. Last time was a few days ago, to ensure that a given set of classes was decorated with a consistent combination of custom attributes. – s.m. Jun 20 '12 at 18:59
  • Yes, the more I read on those tests, the more I like the idea. We already have a somewhat big set of tests, but this one is missing. – Mare Infinitus Jun 20 '12 at 19:01
  • The constraints will get me out of this i believe. Should be only minor changes to existing code. Thank you so much! – Mare Infinitus Jun 20 '12 at 19:15
0

There most probably are better solutions, but you could write an application that uses reflection to inspect the assembly during the post-build event.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72