【问题标题】:PostSharp. How to introduce serializable PropertyPostSharp。如何引入可序列化的属性
【发布时间】:2014-03-25 13:03:01
【问题描述】:

我需要引入字段,该值必须与被切面修饰的类的其他字段序列化。

这是我的课:

[Serializable]
[MyAspect(1)]
public MyClass
{
    public int IntField = 0;
}

这是我的方面:

[Serializable]
public class MyAspect: InstanceLevelAspect
{
    private int _aspectField;

    public MyAspect(int aspectField)
    {
        _aspectField = aspectField;
    }

    [IntroduceMember]
    public int IntroducedProperty { get; set; }
}

反编译 dll 后,我看到 IntroducedProperty 已添加到 MyClass 定义中,但它会将所有调用委托给 MyAspect.IntroducedProperty,从而委托给它的支持字段。

所以,序列化在 MyClass 中看不到任何对应于 IntroducedProperty 的字段。

另外,PostSharp 在 MyClass 中生成 MyAspect 类型的字段,由 NonSerializable 属性标记。

有没有办法引入字段,参与序列化?

【问题讨论】:

  • 我见过的所有序列化程序都只对属性进行操作,而不是对字段进行操作。
  • 我正在使用 BinaryFormatter,它对字段进行操作

标签: c# serialization aop postsharp binaryformatter


【解决方案1】:

在这里解决:http://support.sharpcrafters.com/discussions/questions/742-how-to-introduce-property-for-serialization

我需要在MyClass中实现ISerializable接口,或者分面引入该接口

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    相关资源
    最近更新 更多