2013-09-24 25 views
9

Ben T gibi tanımlanmış bir genel olduğu aşağıdaki kod,:C# IS deyim neden çalışmıyor?

public abstract class RepositoryBase<T> where T : class, IDataModel 

Bu kod sadece iyi çalışır:

PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName); 
if (propertyInfo.DeclaringType.FullName == typeof(T).FullName) <--- Works just fine 

yanlış

PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName); 
if (propertyInfo.DeclaringType is T) <-- does not work 
olarak değerlendirilen bu kod vs

Burada yanlış olan ne yapıyorum?

+0

http://www.youtube.com/watch?v=j4XT-l-_3y0 olduğunu – JoelFan

cevap

24

is iki nesne arasındaki tür karşılaştırmasını kullanır. Bu nedenle DeclaringType, Type tipindedir ve typeof(T), T türünde olup, eşit değildir.

var aType = typeof(propertyInfo.DeclaringType); 
var bType = typeof(T); 
bool areEqual = aType is bType; // Always false, unless T is Type