1

I recently made my own custom formatter, it takes a runtime object and writes down an object graph of it in a string format then takes that string and converts it back to a runtime object. Example: [CustomFormatter.Btype, CustomFormatter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null==[a==5][b==0]]

after messing around with primitive objects (int, bool, float...) I tried to serialize a Bitmap object into string using the ISerializable interface and it worked well but when I try to reconstruct the object back I'm getting an error System.MissingMethodException: Constructor on type 'System.Drawing.Bitmap' not found.

If bitmap doesn't have a constructor which takes SerializationInfo and StreamingContext as parameters how else can an object which inherits the ISerializable interface be constructed?

This part is is the main issue but I can include the whole code:

        private static object SeriObjConstructor(Type objType, ref string dataInfo, ref int location)
        {
            SerializationInfo info = new SerializationInfo(objType, new FormatterConverter());
            StreamingContext context = new StreamingContext(StreamingContextStates.All);
            location += serializerEntry.Length;
            while (!CheckHitOperator(dataInfo, serializerExit, ref location))
            {
                KeyValuePair<string, Type> serializedObj = GetSerialiedName(ref dataInfo, ref location); //<name, type>
                info.AddValue(serializedObj.Key, Construction(serializedObj.Value, ref dataInfo, ref location));
            }
            location += serializerExit.Length;
            location += endClass.Length;

            var instance = Activator.CreateInstance(objType, info, context); //THROWS AN ERROR
            return instance;
        }

1 Answer 1

1

If you are forced to for legacy .NET Framework reasons, and you are taking over the role of the serializer engine: you can construct raw objects using FormatterServices.GetUninitializedObject. This API is explicitly intended for use with things like serialization, but should be treated as an advanced/exotic feature, and needs great care. Bad things can happen in some cases (fields not initialized, etc).

Quite honestly there is no correct way of working with the ISerializable interface in 2020 - that entire API is deprecated and obsolete, and isn't widely supported in .NET Core / .NET 5, and I would not recommend starting new work with it. More details about the problems/reasons are here: https://blog.marcgravell.com/2020/03/why-do-i-rag-on-binaryformatter.html, and many other, non-deprecated, serialization frameworks exist.

Sign up to request clarification or add additional context in comments.

2 Comments

I've heard the BinaryFormatter isn't safe which is why I made my own formatter for education purposes and learn about security issues. for basic objects I'm using FormatterServices.GetUninitializedObject but for other .Net objects like Bitmap such methods aren't effective. I was wondering of there's a different way to initialize objects that inherit from ISerializable. if BinaryFormatter is able to serialize such complex objects I assume there's a special method to construct them, but I couldn't find one. you know any open source formatters so I could learn how they dealt with it?
@Yoav usually a combination of checking for certain constructors (the ones that take certain types), parameterless, falling back to formatterservices - however, honestly IMO it is a mistake to serialize types like Bitmap (which are an implementation detail), rather than serializing the data (a blob, for example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.