Check if a field has the NotSerialized attribute in CSharp
Description
The following code shows how to check if a field has the NotSerialized attribute.
Example
using System;//from w w w . j a v a2 s .com
using System.Reflection;
using System.Runtime.Serialization;
public class MyClass
{
public short myShort;
[NonSerialized()]
public int myInt;
}
public class Type_IsNotSerializable
{
public static void Main()
{
Type myType = typeof(MyClass);
FieldInfo[] myFields = myType.GetFields(BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static);
for(int i = 0; i < myFields.Length; i++)
if(myFields[i].IsNotSerialized)
Console.WriteLine("The {0} field is not serializable.", myFields[i]);
else
Console.WriteLine("The {0} field is not serializable.", myFields[i]);
}
}
The code above generates the following result.