110,534
社区成员
发帖
与我相关
我的任务
分享
[Serializable]
class A
{
}
class B
{
}
private void button1_Click(object sender, EventArgs e)
{
A a = new A();
B b = new B();
bool r1 = MyIsSerializable(a.GetType()); //true
bool r2 = MyIsSerializable(b.GetType()); //false
}
bool MyIsSerializable(Type t)
{
foreach (object o in t.GetCustomAttributes(false)) //get all attributes of the type
{
if( o is System.SerializableAttribute ) return true; //check whether there is a SerializableAttribute
}
return false;
}