hazzik / DelegateDecompiler

A library which is able to decompile a delegate or a method body to its lambda representation
MIT License
522 stars 62 forks source link

Abstract property in hierachy with intermediate generic classes decompiled as default #179

Closed DennisInSky closed 2 years ago

DennisInSky commented 2 years ago

For the case like below

abstract class Base
{
    public abstract string Value { get; }
}

abstract class Base<T> : Base
{
}

class IntChild : Base<int>
{
     public override string Value => "Int";
}

class StringChild : Base<string>
{
    public override string Value => "String";
}

the Base.Value property gets decompiled as null

The below change fixes it:

@@ -9,10 +9,14 @@ static class TypeHierarchy
        public static IEnumerable<Type> Traverse(Type root, IEnumerable<Type> ancestors)
        {
            var result = new List<Type>();
            var children = ancestors.ToLookup(t => t.BaseType);
            var children = ancestors.ToLookup(t =>
                t.BaseType.IsGenericType && !t.BaseType.IsGenericTypeDefinition // This covers hierarchies which include intermediate generic class like C -> B<int> -> A, D -> B<string> -> A
                    ? t.BaseType.GetGenericTypeDefinition()
                    : t.BaseType);

            if (!root.IsInterface)
            {
                Traverse(result, root, children);                
                Traverse(result, root, children);
            }
            else
            {