OdinSerialize does not work on property that is implemented by interface

Issue #125 wontfix
Shane Padgett created an issue

If a property is implemented as part of an interface implementation, the OdinSerialize attribute will not serialize the property. Simple example attached. I looked through the manual and didn't see anything mentioning this, so I figured it must be a bug.

Using: Unity 5.6.2 Odin 1.0.3.0 Windows 10

Comments (3)

  1. Tor Esa Vestergaard

    Thanks for the report!

    Declaring a property which is defined on an interface actually causes that property to be marked as virtual, meaning Odin won't (and shouldn't) serialize it, as it could be updated with custom method logic (if not in your class, then in a deriving class further down) and Odin has no way of telling whether it has - and we currently don't serialize properties where there is a chance that custom property get/set logic may run during serialization. That's a can of worms we don't want to open.

    In your example case, the MyFloat property getter is marked as virtual because BreakingInterface is defined on the class.

    I've updated the manual's serialization section to explicitly explain this case.

  2. Jérémy Quentin

    Declaring a property which is defined on an interface actually causes that property to be marked as virtual

    @TorVestergaard, I am looking for evidence of that but that doesn’t seem to be true.

    You can actually add the virtual keyword to that property if you want it to be virtual but it is not virtual automatically.

    Why would it?

    Such a property cannot be overridden in deriving classes (it does raise a CS0506 compiling error: cannot override inherited member <Name> because it is not marked virtual, abstract, or override).

    So there should be no reason for not serializing it.

  3. Tor Esa Vestergaard

    This limitation has in fact been removed from our serializer, so Odin will now happily serialize any property you give it whether it is auto or non-auto or virtual or not - so long as it has a getter and setter it is fine.

    However, properties that are declared on implemented interface do become virtual even if they are not marked as such. You can easily check it yourself by running this code:

    [Button]
    public void Test()
    {
        Debug.Log(typeof(SomeClass).GetProperty("Prop").GetGetMethod().IsVirtual);
    }
    
    public class SomeClass 
      : ISomeInterface
    {
        public string Prop { get; set; }
    }
    
    public interface ISomeInterface
    {
        string Prop { get; set; }
    }
    

    This will log True, since SomeClass.Prop is really virtual. If you comment out line 8, then it logs False since the interface is no longer implemented.

  4. Log in to comment