积极答复者
关于这几个关键字:class method event field 在Attribute声明中的用法

问题
-
我是在一篇介绍序列化的博客中看到的这种用法,希望能得到系统的讲解,但我在 http://msdn.microsoft.com/zh-cn/library/x53a06bb.aspx 中没有找到这几个关键字(关于这种用法的)介绍,求助于各位
原文是这样使用的:利用field关键字来破坏被Attribute修饰的目标的类型的限制(使本仅用于field的NonSerialized特性用于event)
[field: NonSerialized]
//事件必须用field进行修饰
public
event
EventHandler Event;
但当我模仿时却失败(想让编译器把“get”识别成struct却失败)
protected PlanOrderProcedure NextProcedure
{
[struct: DebuggerStepThrough]
get
{...当然这个struct标记是不需要的,我只是想试出这几个关键字的用法。。。现在实验陷入死胡同了,求助于各位。谢谢!
答案
-
你的原文我已经看过了,它主要是说明事件默认也是要被序列化的,但是事件绑定的那个类不一定是加了Serializable标签的,这就导致无法序列化了,因此必须人为忽略这个属性;而NonSerialized必须加在Field前(注意反射代码粗体下划线部分限制了此属性的用途范围)。
由于事件会被自动序列化,而事件并不是“字段”,因此要忽略的话必须让你那个事件被认为是“字段”才可以忽略。
不支持struct:Nonserialized。如果一个struct不需要序列化,直接不要加上“Serialized”属性即可。
namespace System { /// <summary> /// Indicates that a field of a serializable class should not be serialized. This class cannot be inherited. /// </summary> /// <filterpriority>1</filterpriority> [ComVisible(true)] [AttributeUsage(AttributeTargets.Field, Inherited = false)] public sealed class NonSerializedAttribute : Attribute { /// <summary> /// Initializes a new instance of the <see cref="T:System.NonSerializedAttribute"/> class. /// </summary> [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public NonSerializedAttribute() { base.\u002Ector(); } internal static Attribute GetCustomAttribute(RuntimeFieldInfo field) { if ((field.Attributes & FieldAttributes.NotSerialized) == FieldAttributes.PrivateScope) return (Attribute) null; else return (Attribute) new NonSerializedAttribute(); } internal static bool IsDefined(RuntimeFieldInfo field) { return (field.Attributes & FieldAttributes.NotSerialized) != FieldAttributes.PrivateScope; } } }
- 已标记为答案 phommy 2012年12月20日 10:23
全部回复
-
可以把博客发出一个Url我们看看吗?谢谢。
-
你的原文我已经看过了,它主要是说明事件默认也是要被序列化的,但是事件绑定的那个类不一定是加了Serializable标签的,这就导致无法序列化了,因此必须人为忽略这个属性;而NonSerialized必须加在Field前(注意反射代码粗体下划线部分限制了此属性的用途范围)。
由于事件会被自动序列化,而事件并不是“字段”,因此要忽略的话必须让你那个事件被认为是“字段”才可以忽略。
不支持struct:Nonserialized。如果一个struct不需要序列化,直接不要加上“Serialized”属性即可。
namespace System { /// <summary> /// Indicates that a field of a serializable class should not be serialized. This class cannot be inherited. /// </summary> /// <filterpriority>1</filterpriority> [ComVisible(true)] [AttributeUsage(AttributeTargets.Field, Inherited = false)] public sealed class NonSerializedAttribute : Attribute { /// <summary> /// Initializes a new instance of the <see cref="T:System.NonSerializedAttribute"/> class. /// </summary> [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public NonSerializedAttribute() { base.\u002Ector(); } internal static Attribute GetCustomAttribute(RuntimeFieldInfo field) { if ((field.Attributes & FieldAttributes.NotSerialized) == FieldAttributes.PrivateScope) return (Attribute) null; else return (Attribute) new NonSerializedAttribute(); } internal static bool IsDefined(RuntimeFieldInfo field) { return (field.Attributes & FieldAttributes.NotSerialized) != FieldAttributes.PrivateScope; } } }
- 已标记为答案 phommy 2012年12月20日 10:23