C# Program to Check a Specified class is a Serializable class or not
Last Updated :
07 Mar, 2023
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#
using System;
using System.Reflection;
[Serializable] class Geeks1
{
public static void Display()
{
Console.WriteLine( "Hello! Geeks1" );
}
}
class Geeks2
{
public static void Show()
{
Console.WriteLine( "Hello! Geeks2" );
}
}
class GFG{
static void Main()
{
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#
using System;
using System.Reflection;
[Serializable] class Geeks
{
public static void Display()
{
Console.WriteLine( "Hello" );
}
}
class GFG{
static void Main()
{
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...