C# Program to Check a Specified class is a Serializable class or not
Serialization is a method of converting an object into a stream of bytes that will be used to store the object in the database, or memory, or file, etc so that we can easily read back and convert it back to an object. So to check whether a specified class is serializable or not we use the IsSerializable property of the Type class. It will return true if the class is declared as serializable. Otherwise, it will return false.
Syntax:
public bool IsSerializable { get; }
Example 1:
C#
// C# program to check whether the given // class is serializable class or not using System; using System.Reflection; // Declare a serializable class [Serializable] class Geeks1 { public static void Display() { Console.WriteLine( "Hello! Geeks1" ); } } // Declare a normal class class Geeks2 { public static void Show() { Console.WriteLine( "Hello! Geeks2" ); } } class GFG{ // Driver code static void Main() { // Check the specific class is Serializable or not Console.WriteLine( "Is Geeks1 class is serializable or not?:" + typeof (Geeks1).IsSerializable); Console.WriteLine( "Is Geeks2 class is serializable or not?:" + typeof (Geeks2).IsSerializable); } } |
Output:
Is Geeks1 class is serializable or not?:True Is Geeks2 class is serializable or not?:False
Example 2:
C#
// C# program to check whether the given // class is serializable class or not using System; using System.Reflection; // Declare a serializable class [Serializable] class Geeks { public static void Display() { Console.WriteLine( "Hello" ); } } class GFG{ // Driver code static void Main() { // Checking the class is serializable or not // Using IsSerializable property if ( typeof (Geeks).IsSerializable == true ) { Console.WriteLine( "The given class is serializable" ); } else { Console.WriteLine( "The given class is not serializable" ); } } } |
Output:
The given class is serializable
Please Login to comment...