I am trying to automate this workaround XmlSerializer template . See update below.
Is it possible to introduce a new property based on an existing property and change the attributes of an existing one using PostSharp (or perhaps another AOP tool)?
It would be preferable to make this modification during assembly.
Property of the original sample:
public class TestType {
Desired result (class declaration omitted):
// Modified version // <original property> = "ReqDateTime" // <original property> marked as XmlIgnore // New property with name "<original property>ForXml" is introduced with code as per below // XmlAttribute moved to the newly introduced <original property>ForXml property with parameter "<original property>" [XmlIgnore()] public DateTime ReqDateTime { get { return this.reqDateTimeField;} set { this.reqDateTimeField = value;} } [XmlAttribute("ReqDateTime")] [EditorBrowsable(EditorBrowsableState.Never)] public string ReqDateTimeForXml { get { return Common.GetAndFormatDate(this, Common.GetCaller()); } set { Common.ParseAndSetDate(this, value, Common.GetCaller()); } }
I found a PostSharp member introduction tutorial, but I didn’t get information on (a) how to enter members with dynamic names and (b) how to move attributes ( [XmlAttribute] in my case) from an existing member to a newly created one.
I do not need an exact solution - just some advice is enough.
Update: From further research, I can conclude that PostSharp does not support a dynamic method name. Also, PostSharpIt cannot remove an attribute from an existing method.
So let me reformulate the problem in yet another approach to solving it:
1) Inject 10 new properties named IntroducedProperty0 , IntroducedProperty1 , ... It seems to be trivial . Properties are hard-coded.
2) Somehow after / s (1) add the attribute [XmlAttribute("nameOftheOriginalProperty#N")] to the first M of IntroducedPropertyN , where N = 0..9 and M = N. This is a kind of dynamics. This is possible when adding attributes to existing (not entered) members. However, they say that you cannot add attributes to the entered members . The remaining methods entered (from M to N) should be marked as [XmlIgnore].
3) Mark the original methods of the class using [XmlIgnore].
Maybe this is possible with Fody?