20 Ocak 2017 Cuma

Nullable Sınıfı

Giriş
Nullable Yapısı ve Nullable Sınıfı farklı şeyler.

Compare metodu
Şöyle yaparız.
Nullable.Compare(X, Y);
Equals metodu
Örnek ver

GetUnderlyingType metodu
Şöyle yaparız.
Type t = ...;
t = (Nullable.GetUnderlyingType(t) ?? t);
Şöyle yaparız.
PropertyInfo propertyInfo = ...;
if (Nullable.GetUnderlyingType(propertyInfo.PropertyType) == null)
{
  //Property is not nullable
  // Do whatever you want here
}
Şöyle yaparız.
public static bool Is<T>(this T variable,Type type) {
  if (var != null) {
    Type currentType = variable.GetType();
    type = Nullable.GetUnderlyingType(type) ?? type;
    while (currentType != typeof(object)) {
      if (currentType == type) {
        return true;
      }
      currentType = currentType.BaseType;
    }
  }                  
  return false;
}

Hiç yorum yok:

Yorum Gönder