How could an object get a reference to a class that is in a different (and unreferenced) assembly?
-
06-07-2019 - |
Question
I have two projects: A/B. Project A is the project that contains all of the winforms which are bound to objbects in B (logic items)
A has an object of type A.Form
B has objects of type B.Serializer B.Logic
Now, A has a reference to B (but B does not have a reference to A) and A.Form contains a member variable of type B.Logic. At some point, when all of the data is stored in B.Logic I try to save this object to disk by calling B.Serializer(B.Logic).
At this point I get an error when serializing saying that A.From is not marked as serializable.
But the project B has NO reference to A at ALL and even if it did SOMEHOW have a member referencing A.Form, it shouldn't even compile.
Solution
The usual culprit here is things like events (in B.Logic
), or other back-references to external objects. You can mark fields as not for serialization:
[NonSerialized]
private SomeType foo;
or with field-like events:
[field: NonSerialized]
public event EventHandler Bar;
As an aside - from the description, I assume that you are using BinaryFormatter
; personally, I have reservations about this - it is very brittle. I'd suggest something non-implementation-specific; XmlSerializer
, protobuf-net, Json.NET, etc.