如何根据现有属性引入新属性并修改现有属性?

Ant*_*lov 6 c# aop postsharp fody

我试图自动化这个XmlSerializer解决方案模式.请参阅下面的更新

是否可以基于现有属性引入新属性并使用PostSharp(或者其他一些AOP工具)修改现有属性的属性?

最好在构建时进行这种修改.

示例源属性:

public class TestType {
  // Original version
  [XmlAttribute()]
  public DateTime ReqDateTime {
      get { return this.reqDateTimeField; }
      set { this.reqDateTimeField = value; }
  }
}
Run Code Online (Sandbox Code Playgroud)

期望的结果(省略类声明):

// 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()); }
}
Run Code Online (Sandbox Code Playgroud)

我找到了关于介绍成员的PostSharp教程,但没有关于(a)如何引入具有动态名称的成员以及(b)如何将属性([XmlAttribute]在我的情况下)从现有成员移动到新创建的成员的信息.

我不需要一个确切的解决方案 - 只需要一些提示即可.


更新:从进一步研究中我可以得出结论,PostSharp不支持动态方法命名.PostSharpIt也无法从现有方法中删除属性.

那么让我用另一种方法来解决问题:

1)注入10个名为新特性IntroducedProperty0,IntroducedProperty1...这似乎是微不足道的.属性是硬编码的.

2)以某种方式在/之后(1)将属性添加[XmlAttribute("nameOftheOriginalProperty#N")]IntroducedPropertyN其中N = 0..9且M <= N 的前M中.这有点动态.向现有(未注入)成员添加属性时可以执行此操作.但是他们说你不能为注入的成员添加属性.其余注入的方法(从M到N)应标记为[XmlIgnore].

3)用[XmlIgnore]标记类的原始方法.

也许这可以通过Fody实现?