C# 获取不是数字的模型的所有属性
假设我有一个这样的模型,我把它缩短了一点:C# 获取不是数字的模型的所有属性,c#,reflection,C#,Reflection,假设我有一个这样的模型,我把它缩短了一点: 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; }
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; }
}
我想得到这个模型的非数值属性列表,有没有一种方法可以在不将类型与int、long、decimal等进行比较的情况下实现这一点?没有与Type.IsNumeric等效的属性
我为此创建了一个扩展方法。它是用VB实现的,但也可以用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);
}
}
没有与Type.IsNumeric等效的项
我为此创建了一个扩展方法。它是用VB实现的,但也可以用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);
}
}
没有与Type.IsNumeric等效的项
我为此创建了一个扩展方法。它是用VB实现的,但也可以用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);
}
}
没有与Type.IsNumeric等效的项
我为此创建了一个扩展方法。它是用VB实现的,但也可以用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);
}
}
是否要在不与现有数据类型进行比较的情况下获取属性数据类型?我看不出这应该如何工作。相关问题:也相关:您想在不与现有数据类型进行比较的情况下获取属性数据类型吗?我看不出这应该如何工作。相关问题:也相关:您想在不与现有数据类型进行比较的情况下获取属性数据类型吗?我看不出这应该如何工作。相关问题:也相关:您想在不与现有数据类型进行比较的情况下获取属性数据类型吗?我不知道该怎么做。相关问题:也相关:我明白了,扩展方法是什么,谢谢@YuvalItzchakov:我已经转换了。谢谢.NET高手。我明白了,扩展方法就是这样,谢谢@YuvalItzchakov:我已经转换了。谢谢.NET高手。我明白了,扩展方法就是这样,谢谢@YuvalItzchakov:我已经转换了。谢谢.NET高手。我明白了,扩展方法就是这样,谢谢@尤瓦利扎科夫:我已经转换了。谢谢。网络大师。