views:

860

answers:

3

I know that a private parameterless constructor works but what about an object with no parameterless constructors?

I would like to expose types from a third party library so I have no control over the type definitions.

If there is a way what is the easiest? E.g. I don't what to have to create a sub type.

Edit:

What I'm looking for is something like the level of customization shown here: http://msdn.microsoft.com/en-us/magazine/cc163902.aspx although I don't want to have to resort to streams to serialize/deserialize.

A: 

I am not a WCF expert but it is unlikely that they support serialization on a constructor with arbitrary types. Namely because what would they pass in for values? You could pass null for reference types and empty values for structs. But what good would a type be that could be constructed with completely empty data?

I think you are stuck with 1 of 2 options

  1. Sub class the type in question and pass appropriate default values to the non-parameterless constructor
  2. Create a type that exists soley for serialization. Once completed it can create an instance of the original type that you are interested in. It is a bridge of sorts.

Personally I would go for #2. Make the class a data only structure and optimize it for serialization and factory purposes.

JaredPar
The problem with this is that there are a lot of types in the library and I would have to map all the types across which is what I'm doing now with a subset of the types in the 3rd party library.
Jonathan Parker
+1  A: 

I just ran a little test, using a WCF Service that returns an basic object that does not have a default constructor.

//[DataContract]
//[Serializable]
public class MyObject
{
    public MyObject(string _name)
    {
        Name = _name;
    }

    //[DataMember]
    public string Name { get; set; }

    //[DataMember]
    public string Address { get; set; }
}

Here is what the service looks like:

public class MyService : IMyService
{
    #region IMyService Members

    public MyObject GetByName(string _name)
    {
        return new MyObject(_name) { Address = "Test Address" };
    }

    #endregion
}

This actually works, as long as MyObject is either a [DataContract] or [Serializable]. Interestingly, it doesn't seem to need the default constructor on the client side. There is a related post here:

http://stackoverflow.com/questions/178645/how-does-wcf-deserialization-instantiate-objects-without-calling-a-constructor

Andy White
Unfortunately the 3rd party framework types are neither marked as [DataContract] or [Serializable].
Jonathan Parker
This means that they were not designed to be serialized, so don't serialize them.
John Saunders
You might be able to write an serializable adapter object that wraps the 3rd party types and only serializes the parts you need.
Andy White
+1  A: 

You can't really make arbitrary types serializable; in some cases (XmlSerializer, for example) the runtime exposes options to spoof the attributes. But DataContractSerializer doesn't allow this. Feasible options:

  • hide the classes behind your own types that are serializable (lots of work)
  • provide binary formatter surrogates (yeuch)
  • write your own serialization core (a lot of work to get right)

Essentially, if something isn't designed for serialization, very little of the framework will let you serialize it.

Marc Gravell
It's a pity, I'd like something like extension methods where the framework looks for an extension method on the object and if it exists it calls it to serialize/deserialize.
Jonathan Parker
The framework cannot really look for extension methods; they are a compiler trick. If multiple existed, which would it pick? What you describe is close to surrogates, but that is still a lot of work.
Marc Gravell
Yeah, I guess you would have to register your implementation somehow. There are just so many hooks into WCF if only there was one for serializing which was simple and powerful (i.e. no need to own the types being serialized).
Jonathan Parker