Ricky
Ricky

Reputation: 35913

C#: How to check whether a instance is serializable

How can check whether a instance is marked as serializable?

Thanks

Upvotes: 1

Views: 232

Answers (4)

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60714

Since you are asking for instance, and not class, the correct answer is actually:

o.GetType().IsSerializable;

Upvotes: 3

Saeed Amiri
Saeed Amiri

Reputation: 22565

        Type t = typeof(x) 
        for fields:
        t.GetFields().Where(p=> !p.Attributes.HasFlag(FieldAttributes.NotSerialized));
        for type
        t.Attributes.HasFlag(TypeAttributes.Serializable);

Upvotes: 1

Cheng Chen
Cheng Chen

Reputation: 43523

typeof(MyClass).IsSerializable;

Upvotes: 6

jebberwocky
jebberwocky

Reputation: 1089

have you tried?

o.GetType().FindInterfaces().Any(x => x == typeof(ISerializable));

Upvotes: 1

Related Questions