views:

348

answers:

1

Is it possible to generate a property with a public getter and a protected setter with CodeDOM? The goal is to achieve something similar to the following.

[Serializable]
public class Wrapper {
  public Wrapper() { }
  private string _Field;
  public string Field { get; protected set; }
}

I have a large COM based API for which I wish to compile a .Net wrapper so it would be easier to use .Net features such as LINQ, Reflection, inheritance and serialization with it. The only feasible way is to automate large parts of this work with code generation.

These objects contain some read only properties that I wish to expose through serialization which requires a property setter. But so long I haven't found any ways to set the setter protected or similar.

One way might be to mark the property as not serializable and serialize the _Field but since one goal for the serialized output is web I'd need to attribute the private member with all the possible serializer attributes that direct the serializer to use a cleaner name (without the underscore) for serialization. For the same reason I believe custom serialization isn't possible.

It's not that important that I can deserialize it accurately, ie. it isn't critical that the value remains in its original value during/through deserialization. The properties are read only just to reduce confusion by preventing the API consumer from trying to change the read only fields and wondering why they have no effect on anything.

A: 

Just wrapping this up. There is no way to do this in CodeDOM save for raw string output.

And what comes to XMLSerializer the serializer is able to serialize the class but it won't serialize the value with the protected setter.

So not possible. The alternative way I used was to generate a ToSerializableType() method to each of these classes which returns a instance of a similar class which has all properties read/Write.

Mikko Rantanen