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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.