What should the method `FindBaseClassWith` return?

邮差的信 提交于 2020-01-05 04:22:29

问题


Suppose I have following class:

public class Xyz {
}

public class Abc: Xyz {
}

public class Pqr: Xyz {
}

public class Wtf: Abc {
}

with a method to find the common base type of type1 and type2:

public static Type FindBaseClassWith(this Type type1, Type type2);

Then, I call the method with:

typeof(Wtf).FindBaseClassWith(variousType).FindBaseClassWith(typeof(Xyz));

Where variousType is a variable of Type, it possibly sometimes be null.

And FindBaseClassWith is meant to be chained call like:

t1.FindBaseClassWith(t2).FindBaseClassWith(t3).FindBaseClassWith(t4);

to find the only base type between them.

What should the method FindBaseClassWith return?

A most acceptable answer will be mark whether it will be the solution.


回答1:


Since it's an extension method, it will work fine even if one of the members of the chain is null. You can call extension methods on a null reference because it's syntactic sugar for a method call where the object is passed as a parameter. However, you will get a NullReferenceException if you try to access a property like .Name at the end when the result is null.

If you want to use a chain call to collect a series of parameters, and then only "process" them at the end, you want a similar pattern to LINQ. The intermediate types should be some sort of a wrapper that simply collects the values in the chain. Then there should be a final call that actually initiates the process of matching the types, i.e., something like .Resolve(). Here's an example implementation called TypeChain:

public class TypeChain
{
    private List<Type> types;

    public TypeChain(Type t)
    {
        types = new List<Type>();
        types.Add(t);
    }

    // searching for common base class (either concrete or abstract)
    public TypeChain FindBaseClassWith(Type typeRight)
    {
        this.types.Add(typeRight);
        return this;
    }

    public Type Resolve()
    {
        // do something to analyze all of the types
        if (types.All (t => t == null))
            return null;
        else
            types = types.Where (t => t != null).ToList();

        var temp = types.First();
        foreach (var type in types.Skip(1))
        {
            temp = temp.FindBaseClassWithHelper(type);
        }
        return temp;
    }
}

There would be a few changes to the FindBaseClassWith static implementations:

// searching for common base class (either concrete or abstract)
public static Type FindBaseClassWithHelper(this Type typeLeft, Type typeRight)
{
    if(typeLeft == null || typeRight == null) return null;

    return typeLeft
            .GetClassHierarchy()
            .Intersect(typeRight.GetClassHierarchy())
            .FirstOrDefault(type => !type.IsInterface);
}

// searching for common base class (either concrete or abstract)
public static TypeChain FindBaseClassWith(this Type typeLeft, Type typeRight)
{
    return new TypeChain(typeLeft).FindBaseClassWith(typeRight);
}

Using the chaining logic above allows for a logic scenario that isn't possible with the original code. It's now possible to check if all entries are null, and then return null, or if any are not null, then purge all the null entries first before processing so they don't pollute the result. This can be extended of course to any desired logic.




回答2:


See http://msdn.microsoft.com/en-us/library/system.type.basetype.aspx

You can write a recursive function to transverse the hierarchy by repeatedly comparing BaseType with the desired type.



来源:https://stackoverflow.com/questions/14360593/what-should-the-method-findbaseclasswith-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!