C# 在C语言中,我怎样才能得到一个类';到基类的继承距离是多少?
例如: 我有以下课程:C# 在C语言中,我怎样才能得到一个类';到基类的继承距离是多少?,c#,subclass,distance,C#,Subclass,Distance,例如: 我有以下课程: public class A {} public class B:A {} public class C:B {} public class D:C {} 如果有一种方便的方法,比如获取到基类的层次距离(而不是测试D.IsubClassof(B))来确定D是更接近a,还是B更接近a 也许这有什么帮助: public static int? GetDegreeOfRelationship(Type typeA, Type typeB) { if (typeA.Is
public class A {}
public class B:A {}
public class C:B {}
public class D:C {}
如果有一种方便的方法,比如获取到基类的层次距离(而不是测试D.IsubClassof(B))来确定D是更接近a,还是B更接近a 也许这有什么帮助:
public static int? GetDegreeOfRelationship(Type typeA, Type typeB)
{
if (typeA.IsInterface || typeB.IsInterface) return null; // interfaces are not part of the inheritance tree
if (typeA == typeB) return 0;
int distance = 0;
Type child;
if (typeA.IsAssignableFrom(typeB))
{
child = typeB;
while ((child = child.BaseType) != typeA)
distance--;
return --distance;
}
else if(typeB.IsAssignableFrom(typeA))
{
child = typeA;
while ((child = child.BaseType) != typeB)
distance++;
return ++distance;
}
else
return null;
}
用法:
int? distance = GetDegreeOfRelationship(typeof(A), typeof(D)); // -3
int? distance = GetDegreeOfRelationship(typeof(D), typeof(A)); // 3
int? distance = GetDegreeOfRelationship(typeof(B), typeof(B)); // 0
int? distance = GetDegreeOfRelationship(typeof(D), typeof(string)); // null
我已经根据@kha的建议截取了代码,并根据以下答案为您进行了改编:
公共静态类类型扩展
{
公共静态IEnumerable GetInheritancHierarchy
(此类型)
{
for(var current=type;current!=null;current=current.BaseType)
产生回流电流;
}
公共静态整型GetInheritanceDistance(此类型)
{
返回类型.GetInheritancHierarchy().TakeWhile(t=>t!=typeof(TOther)).Count();
}
}
用法
var c = new C(); // 2
Console.WriteLine(c.GetType().GetInheritanceDistance<A>());
var b = new B(); // 1
Console.WriteLine(b.GetType().GetInheritanceDistance<A>());
var c=new c();//2.
WriteLine(c.GetType().GetInheritanceDistance());
var b=新的b();//1.
WriteLine(b.GetType().GetInheritanceDistance());
您可以使用扩展方法扩展类型的行为
在层次结构中进行迭代,以得出到基准的距离或到基准的路径:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
System.Windows.Forms.Form a = new System.Windows.Forms.Form();
Console.WriteLine(a.GetType().DistanceToBase());
Console.WriteLine(a.GetType().PathToBase());
}
}
public static class Extensions
{
public static int DistanceToBase(this Type ob)
{
if (ob.BaseType == null)
{
return 0;
}
else return 1 + ob.BaseType.DistanceToBase();
}
public static string PathToBase(this Type ob)
{
if (ob.BaseType == null)
{
return ob.Name;
}
return ob.Name + "->" + ob.BaseType.PathToBase();
}
}
这当然有可能,但为什么呢?你需要这个做什么?这可能会帮助你得到继承树。但是,你为什么要这样做呢?一种方法是修改这里的解决方案(答案中的代码片段),这应该会给你所需要的价值。好吧,我正在使用C#开发一个Unity游戏,我们正在使用这个项目:使我们的游戏可以在IOS下进行热修复。因此,这个项目分析dll并运行它的解释dll中的代码。问题是当它在DLL中查找重载方法时,无法工作well@simon:注意,我已经修复了接口的一个问题,它们不是继承树的一部分,因此我返回null。否则您会遇到
NullReferenceException
,因为IsAssignableFrom
返回true
,但您无法通过BaseType
访问它们。
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
System.Windows.Forms.Form a = new System.Windows.Forms.Form();
Console.WriteLine(a.GetType().DistanceToBase());
Console.WriteLine(a.GetType().PathToBase());
}
}
public static class Extensions
{
public static int DistanceToBase(this Type ob)
{
if (ob.BaseType == null)
{
return 0;
}
else return 1 + ob.BaseType.DistanceToBase();
}
public static string PathToBase(this Type ob)
{
if (ob.BaseType == null)
{
return ob.Name;
}
return ob.Name + "->" + ob.BaseType.PathToBase();
}
}