Assuming I have a model like this (I shortened it a bit):
class NewsletterDatum
{
public string FullName{ get; set; }
public string Email { get; set; }
public string OptOutLink { get; set; }
public long ConciergeId { get; set; }
public long AwardCount { get; set; }
public int YearsMember {get; set; }
public string CardNumber { get; set; }
public string MemberName { get; set; }
public string PointBalance { get; set; }
public List<string> StoredKeyWords { get; set; }
public List<string> FriendIds { get; set; }
}
I want to get the list of properties of this model that are not numerical, is there a way of doing this without comparing types with int, long, decimal, etc..?
答案 0 :(得分:3)
There is no equivalent to Type.IsNumeric()
.
I created an extension method to this. It's implemented in VB, but could be done in C#.
public static class TypeExtensions {
private static HashSet<Type> NumericTypes = new HashSet<Type> {
typeof(byte),
typeof(sbyte),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(decimal),
typeof(IntPtr),
typeof(UIntPtr),
};
private static HashSet<Type> NullableNumericTypes = new HashSet<Type>(
from type in NumericTypes
select typeof(Nullable<>).MakeGenericType(type)
);
public static bool IsNumeric(this Type @this, bool allowNullable = false) {
return NumericTypes.Contains(@this)
|| allowNullable && NullableNumericTypes.Contains(@this);
}
}