Custom class serialization without default constructor does not work
See original GitHub issueDescribe the bug Custom class serialization without default constructor does not work
To Reproduce Steps to reproduce the behavior:
[ServerRpc]
public void TestServerRCP(Foo foo)
{
// foo is null
}
public class Foo : INetworkSerializable
{
public string Boo;
public string Poo;
public Foo(string boo, string poo)
{
Boo = boo;
Poo = poo;
}
/// <inheritdoc />
public void NetworkSerialize(NetworkSerializer serializer)
{
serializer.Serialize(ref Boo);
serializer.Serialize(ref Poo);
}
}
Actual outcome Foo is null
Expected outcome
- Provided constructor should be used instead, (if possible) or exception should be returned
- Exception should be returned that the default constructor is missing.
- Allow for custom constructors in serializer API
Environment (please complete the following information):
- OS: Windows 10
- Unity Version: 2020.3.5f1
- MLAPI Version: 1.0.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Custom class serialization without default constructor does ...
Describe the bug Custom class serialization without default constructor does not work To Reproduce Steps to reproduce the behavior: ...
Read more >c# - class can not do serialization without empty constructor
ClassName cannot be serialized because it does not have a parameterless constructor. The code goes like this: public void DoSerialize(string ...
Read more >Cannot serialize without a default public constructor...
I'm having trouble (xml) serializing a class that I've placed a constructor in: [System.Serializable] public class Stat { public...
Read more >Should custom deserialization happen in a constructor or ...
No, it should not. Serialization is orthogonal to the object and thus should be kept outside. Custom serialization belongs in a ...
Read more >CA2229: Implement serialization constructors (code analysis)
The serialization constructor is required to deserialize, or recreate, objects that have been serialized using the ISerializable. GetObjectData ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In case its useful to anyone else, in this thread (and the current documentation) the term default constructor is being used to refer to a parameterless constructor (this messed me up when googling a bit). To elaborate unnecessarily, this constructor only exists if you have explicitly defined it or you have defined no constructors (and so the default constructor given to you is a parameterless one)
tldr; In short, your receiver will receive
null
instead of the value you expect if no parameterless constructor is provided in the class you are trying to serialize.Gratuitous examples for those who only read code and are allergic to text:
✔️ The following will serialize properly (C# gives you a default constructor since you didn’t declare any constructors at all and this default constructor is parameterless)
❌ The following will NOT serialize properly, the receiver will just get
null
because you have defined a constructor with parameters, so C# will not give you a default one (which would have been parameterless)✔️ The following will serialize properly because a parameterless constructor was explicitly declared on the class
@ShadauxCat, not urgent and also not triaged yet but I think it’d worth checking if we implemented RPC params
new()
(default parameterless constructor) constraint check on the ILPP side and closing this one off. There might be a minor update on the docs side (as we mentioned before above) but I actually want to just leave all this on your judgement.