How to tell if a class is Serialized

Hello Experts,

I am maintaining some C# and VB.net code. I know that by default classes and members are non-serializable; that they can be made serializeable by adding the SerializableAttribute.

I also know that if class B inherits from a serialized class, "class A", that class B is not serialized by default. So here is my question. Is there a way in C# or VB.net to prove that a class is currently not serialized?
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
 
it_saigeDeveloperCommented:
The Type class has an IsSerializable property which is set if the object in question sets the SerializableAttribute, implementing ISerializable does not set the Serializable flag.  Proof of concept -
using System;
using System.Runtime.Serialization;

namespace EE_Q29096497
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"{nameof(NonSerializableClass)} is serializable? {typeof(NonSerializableClass).IsSerializable}");
            Console.WriteLine($"{nameof(OneSerializableClass)} is serializable? {typeof(OneSerializableClass).IsSerializable}");
            Console.WriteLine($"{nameof(AnotherSerializableClass)} is serializable? {typeof(AnotherSerializableClass).IsSerializable}");
            Console.ReadLine();
        }
    }

    class NonSerializableClass { }

    class OneSerializableClass : ISerializable
    {
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            throw new NotImplementedException();
        }
    }

    [Serializable]
    class AnotherSerializableClass { }
}

Open in new window

Which produces the following output -Capture.PNGBut do heed what kaufmed and Miguel have stated.

-saige-
0
 
käµfm³d 👽Commented:
What kind of serialization are you talking about? The term "serialization" just means taking the in-memory representation of an object, and transforming that data into a representation that can be stored or transmitted. Being serialized means the data has already undergone that conversion.

Marking a class as "serializable" does nothing to the class itself.
0
 
Miguel OzSoftware EngineerCommented:
By definition all public classes are serializable unless you add attributes to the class specifying not to do so. For example to serialize to XML the class must be public and only public properties that do not have attribute XmlIgnored are present in the final result.
0
 
brgdotnetcontractorAuthor Commented:
Migquel read this article. By default they are not serialized, you have to place an attribute on them to declare them as serializedL

https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc30772
0
 
käµfm³d 👽Commented:
Well again, it depends on what kind of serialization you are talking about. The attributes in that post are specific to binary serialization. JSON and XML serializers--at least the common ones--don't rely on those attributes at all, and as Miguel mentions, public members of classes are serialized by default with those serializers unless you instruct otherwise.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.