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

7
  • Assuming that everything descends from Object, it already has a default constructor. Perhaps I'm missing something here? Commented Jun 20, 2012 at 18:53
  • @DavidLively constructors aren't inherited; class Foo { public Foo(int i) {...} does not have a parameterless constructor Commented Jun 20, 2012 at 18:53
  • I'd like to suggest reading this: social.msdn.microsoft.com/forums/en-US/csharplanguage/thread/… Although they provide something like a solution, that uses generics. Commented Jun 20, 2012 at 18:56
  • already saw that one, but there he wants some parameters, i especially want >no< parameters. Commented Jun 20, 2012 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. Commented Jun 20, 2012 at 19:06

2 Answers 2

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()
    
Sign up to request clarification or add additional context in comments.

4 Comments

Writing a wrapper is a partial solution, but maybe the fastest. Thank you.
+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.
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.
The constraints will get me out of this i believe. Should be only minor changes to existing code. Thank you so much!
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.