用于检查指定类的 C# 程序是否为可序列化类
序列化是一种将对象转换为字节流的方法,该字节流将用于将对象存储在数据库、内存或文件等中,以便我们可以轻松地读回并将其转换回对象。因此,要检查指定的类是否可序列化,我们使用 Type 类的 IsSerializable 属性。如果该类被声明为可序列化,它将返回 true。否则,它将返回 false。
句法:
public bool IsSerializable { get; }
示例 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);
}
}
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");
}
}
}
输出:
Is Geeks1 class is serializable or not?:True
Is Geeks2 class is serializable or not?:False
示例 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");
}
}
}
输出:
The given class is serializable