方法"FindBaseClassWithWith"应该返回什么? [英] What should the method `FindBaseClassWith` return?
问题描述
假设我有以下课程:
public class Xyz {
}
public class Abc: Xyz {
}
public class Pqr: Xyz {
}
public class Wtf: Abc {
}
使用一种查找类型1和类型2的通用基本类型的方法:
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));
如果variousType
是Type
的变量,则有时可能是null
.
Where variousType
is a variable of Type
, it possibly sometimes be null
.
FindBaseClassWith
和链式调用一样:
t1.FindBaseClassWith(t2).FindBaseClassWith(t3).FindBaseClassWith(t4);
查找它们之间的唯一基本类型.
to find the only base type between them.
方法FindBaseClassWith
应该返回什么?
一个最可接受的答案将是它是否将成为解决方案.
A most acceptable answer will be mark whether it will be the solution.
推荐答案
由于它是扩展方法,即使链中的一个成员为null,它也可以正常工作.您可以在空引用上调用扩展方法,因为它是将对象作为参数传递的方法调用的语法糖.但是,如果尝试在结果为null
时最后尝试访问诸如.Name
之类的属性,则会得到NullReferenceException
.
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
.
如果要使用链式调用来收集一系列参数,然后仅在最后处理"它们,则需要与LINQ类似的模式.中间类型应该是某种包装器,可以简单地收集链中的值.然后应该有一个最终调用,该调用实际上会启动匹配类型的过程,即类似.Resolve()
的过程.这是一个称为TypeChain
的示例实现:
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;
}
}
FindBaseClassWith
静态实现将有一些更改:
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);
}
使用上面的链接逻辑,可以提供原始代码无法实现的逻辑方案.现在可以检查是否所有条目都是null
,然后返回null
,或者如果不是null
,则先清除所有null
条目,然后再进行处理,以免污染结果.当然,这可以扩展到任何所需的逻辑.
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.
这篇关于方法"FindBaseClassWithWith"应该返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!