[英]C# Object Type Comparison
如何比较声明为类型的两个对象的类型。
我想知道两个对象是同一类型还是来自同一基础 class。
任何帮助表示赞赏。
例如
private bool AreSame(Type a, Type b) {
}
假设a
和b
是两个对象。 如果您想查看a
和b
是否在同一个 inheritance 层次结构中,请使用Type.IsAssignableFrom
:
var t = a.GetType();
var u = b.GetType();
if (t.IsAssignableFrom(u) || u.IsAssignableFrom(t)) {
// x.IsAssignableFrom(y) returns true if:
// (1) x and y are the same type
// (2) x and y are in the same inheritance hierarchy
// (3) y is implemented by x
// (4) y is a generic type parameter and one of its constraints is x
}
如果您想检查一个是否是另一个的基础 class,请尝试Type.IsSubclassOf
。
如果您知道具体的基数 class,那么只需使用is
关键字:
if (a is T && b is T) {
// Objects are both of type T.
}
否则,您必须直接遍历 inheritance 层次结构。
不过,这个想法有一点问题,因为每个 object(实际上,每个类型)都有一个共同的基础 class、Object。 您需要定义的是您想要 go 的 inheritance 链上多远(无论它们是相同的还是具有相同的直系父级,或者一个是另一个的直系父级,等等)并执行您的那样检查。 IsAssignableFrom
对于确定类型是否相互兼容很有用,但不能完全确定它们是否具有相同的父级(如果这是您所追求的)。
如果您的严格标准是 function 应该返回 true 如果...
你可以使用
private bool AreSame(Type a, Type b)
{
if(a == b) return true; // Either both are null or they are the same type
if(a == null || b == null) return false;
if(a.IsSubclassOf(b) || b.IsSubclassOf(a)) return true; // One inherits from the other
return a.BaseType == b.BaseType; // They have the same immediate parent
}
如果您希望两个 object 实例属于某种类型,您也可以使用“IS”关键字。 这也适用于将子类与父类以及实现接口的类等进行比较。 不过,这不适用于 Type 类型的类型。
if (objA Is string && objB Is string)
// they are the same.
public class a {}
public class b : a {}
b objb = new b();
if (objb Is a)
// they are of the same via inheritance
我使用接口和具体类尝试了以下层次结构。 它遍历其中一种类型的基本 class 链,直到它到达“对象”,在此我们检查当前目标类型是否可分配给源类型。 我们还检查类型是否具有公共接口。 如果他们这样做,那么他们'AreSame'
希望这可以帮助。
public interface IUser
{
int ID { get; set; }
string Name { get; set; }
}
public class NetworkUser : IUser
{
public int ID
{
get;
set;
}
public string Name
{
get;
set;
}
}
public class Associate : NetworkUser,IUser
{
#region IUser Members
public int ID
{
get;
set;
}
public string Name
{
get;
set;
}
#endregion
}
public class Manager : NetworkUser,IUser
{
#region IUser Members
public int ID
{
get;
set;
}
public string Name
{
get;
set;
}
#endregion
}
public class Program
{
public static bool AreSame(Type sourceType, Type destinationType)
{
if (sourceType == null || destinationType == null)
{
return false;
}
if (sourceType == destinationType)
{
return true;
}
//walk up the inheritance chain till we reach 'object' at which point check if
//the current destination type is assignable from the source type
Type tempDestinationType = destinationType;
while (tempDestinationType.BaseType != typeof(object))
{
tempDestinationType = tempDestinationType.BaseType;
}
if( tempDestinationType.IsAssignableFrom(sourceType))
{
return true;
}
var query = from d in destinationType.GetInterfaces() join s in sourceType.GetInterfaces()
on d.Name equals s.Name
select s;
//if the results of the query are not empty then we have a common interface , so return true
if (query != Enumerable.Empty<Type>())
{
return true;
}
return false;
}
public static void Main(string[] args)
{
AreSame(new Manager().GetType(), new Associate().GetType());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.