mikhailshilkov / With.Fody

A Fody addin to extend immutable C# classes with With() methods to return a copy of an object with one property modified.
MIT License
36 stars 3 forks source link

Support for inheritance #7

Closed aalmada closed 7 years ago

aalmada commented 7 years ago

Cecil ignores inherited properties so, With.Fody does not support inheritance. I added a GetAllProperties() that goes up the hierarchy to get all the properties. Added exception handling to gracefully step over any assembly resolution issue. Added the test cases Inheritance, that inherits from a class in the same assembly, and ReferenceInheritance, that inherits from a class in the ReferenceAssembly assembly.

Supports cases like this one:

    public class BaseInheritance
    {
        public BaseInheritance(int value1, string value2)
        {
            this.Value1 = value1;
            this.Value2 = value2;
        }

        public int Value1 { get; }

        public string Value2 { get; }

        public BaseInheritance With<T>(T value) => this;
    }

    public class Inheritance : BaseInheritance
    {
        public Inheritance(int value1, string value2, long value3)
            : base(value1, value2)
        {
            this.Value3 = value3;
        }

        public long Value3 { get; }

        public new Inheritance With<T>(T value) => this;
    }
aalmada commented 7 years ago

Hi @mikhailshilkov I updated this PR to include the changes from #10. I have in my project a case of inheritance. To use With.Fody, I have to break the inheritance and copy the properties to the other class. This PR would fix this. What do you think? Do you find any case that would be a regression?

mikhailshilkov commented 7 years ago

Merged

mikhailshilkov commented 7 years ago

This version works fine for my projects, no regression detected by tests. Well done!