Mega Code Archive

 
Categories / C# / Reflection
 

FieldInfo IsNotSerialized Property indicates whether this field has the NotSerialized attribute

using System; 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]);     } }