本文整理汇总了C#中System.Type.IsEnum方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsEnum方法的具体用法?C# Type.IsEnum怎么用?C# Type.IsEnum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.IsEnum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ArgumentTypeIsEnum
public static void ArgumentTypeIsEnum(Type enumType, string parameterName)
{
ArgumentNotNull(enumType, "enumType");
if (!enumType.IsEnum())
throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), parameterName);
}
示例2: Create
public static Instruction Create(Type type)
{
// Boxed enums can be unboxed as their underlying types:
switch ((type.IsEnum() ? Enum.GetUnderlyingType(type) : type).GetTypeCode()) {
case TypeCode.Boolean: return _Boolean ?? (_Boolean = new NotEqualBoolean());
case TypeCode.SByte: return _SByte ?? (_SByte = new NotEqualSByte());
case TypeCode.Byte: return _Byte ?? (_Byte = new NotEqualByte());
case TypeCode.Char: return _Char ?? (_Char = new NotEqualChar());
case TypeCode.Int16: return _Int16 ?? (_Int16 = new NotEqualInt16());
case TypeCode.Int32: return _Int32 ?? (_Int32 = new NotEqualInt32());
case TypeCode.Int64: return _Int64 ?? (_Int64 = new NotEqualInt64());
case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new NotEqualInt16());
case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new NotEqualInt32());
case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new NotEqualInt64());
case TypeCode.Single: return _Single ?? (_Single = new NotEqualSingle());
case TypeCode.Double: return _Double ?? (_Double = new NotEqualDouble());
case TypeCode.Object:
if (!type.IsValueType()) {
return _Reference ?? (_Reference = new NotEqualReference());
}
// TODO: Nullable<T>
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
}
示例3: IsNumeric
// keep in sync with System.Core version
internal static bool IsNumeric(Type type) {
type = GetNonNullableType(type);
if (!type.IsEnum()) {
return IsNumeric(type.GetTypeCode());
}
return false;
}
示例4: read
private object read(Type nativeType)
{
object primitiveValue = _current.GetPrimitiveValue();
if (nativeType.IsEnum() && primitiveValue.GetType() == typeof(string))
{
var enumMapping = _inspector.FindEnumMappingByType(nativeType);
if (enumMapping != null)
{
var enumLiteral = (string)primitiveValue;
if (enumMapping.ContainsLiteral(enumLiteral))
return enumMapping.ParseLiteral((string)primitiveValue);
else
throw Error.Format("Literal {0} is not a valid value for enumeration {1}", _current, enumLiteral, enumMapping.Name);
}
else
throw Error.Format("Cannot find an enumeration mapping for enum " + nativeType.Name, _current);
}
try
{
return PrimitiveTypeConverter.ConvertTo(primitiveValue, nativeType);
}
catch (NotSupportedException exc)
{
// thrown when an unsupported conversion was required
throw Error.Format(exc.Message, _current);
}
}
示例5: AddModelType
public static void AddModelType(Type type)
{
if (type.IsEnum())
Inspector.ImportEnum(type);
else
Inspector.ImportType(type);
}
示例6: Create
public static EnumMapping Create(Type enumType)
{
if (enumType == null) throw Error.ArgumentNull("enumType");
if (!enumType.IsEnum()) throw Error.Argument("enumType", "Type {0} is not an enumerated type", enumType.Name);
var result = new EnumMapping();
result.Name = getEnumName(enumType);
result.EnumType = enumType;
result._enumToLiteral = new Dictionary<Enum, string>();
result._literalToEnum = new Dictionary<string, Enum>();
foreach(var enumValue in ReflectionHelper.FindEnumFields(enumType))
{
var attr = ReflectionHelper.GetAttribute<EnumLiteralAttribute>(enumValue);
string literal = enumValue.Name;
if (attr != null) literal = attr.Literal;
Enum value = (Enum)enumValue.GetValue(null);
result._enumToLiteral.Add(value, literal);
result._literalToEnum.Add(literal, value);
}
return result;
}
示例7: ExtractObject
public override object ExtractObject(Type requestedType)
{
if (requestedType == null)
return m_value;
if (requestedType.IsEnum())
return ConvertEnum(requestedType, m_value);
object v = Convert.ChangeType(m_value, requestedType);
return v;
}
示例8: GetEnumerationType
public static Type GetEnumerationType(Type enumType)
{
if (enumType.IsNullableType())
{
enumType = enumType.GetTypeInfo().GenericTypeArguments[0];
}
if (!enumType.IsEnum())
return null;
return enumType;
}
示例9: GetTypeCode
public static TypeCode GetTypeCode(Type type)
{
if (type == null) return TypeCode.Empty;
TypeCode result;
if (typeCodeLookup.TryGetValue(type, out result)) return result;
if (type.IsEnum())
{
type = Enum.GetUnderlyingType(type);
if (typeCodeLookup.TryGetValue(type, out result)) return result;
}
return TypeCode.Object;
}
示例10: Create
public static Instruction Create(Type type) {
Debug.Assert(!type.IsEnum());
switch (type.GetTypeCode()) {
case TypeCode.Int16: return _Int16 ?? (_Int16 = new AddInt16());
case TypeCode.Int32: return _Int32 ?? (_Int32 = new AddInt32());
case TypeCode.Int64: return _Int64 ?? (_Int64 = new AddInt64());
case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new AddUInt16());
case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new AddUInt32());
case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new AddUInt64());
case TypeCode.Single: return _Single ?? (_Single = new AddSingle());
case TypeCode.Double: return _Double ?? (_Double = new AddDouble());
default:
throw Assert.Unreachable;
}
}
示例11: IsArithmetic
// keep in sync with System.Core version
internal static bool IsArithmetic(Type type) {
type = GetNonNullableType(type);
if (!type.IsEnum()) {
switch (type.GetTypeCode()) {
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Double:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
}
}
return false;
}
示例12: GetNonGenericEnumInfo
internal static NonGenericEnumInfo GetNonGenericEnumInfo(Type enumType)
{
Preconditions.NotNull(enumType, nameof(enumType));
var nonGenericEnumInfos = _nonGenericEnumInfos;
NonGenericEnumInfo info;
if (!nonGenericEnumInfos.TryGetValue(enumType, out info))
{
if (enumType.IsEnum())
{
var closedEnumsType = typeof(Enums<>).MakeGenericType(enumType);
info = new NonGenericEnumInfo((IEnumInfo)closedEnumsType.
#if TYPE_REFLECTION
GetField("Info", BindingFlags.Static | BindingFlags.Public)
#else
GetTypeInfo().GetDeclaredField("Info")
#endif
.GetValue(null), false);
}
else
{
var nonNullableEnumType = Nullable.GetUnderlyingType(enumType);
if (nonNullableEnumType?.IsEnum() != true)
{
throw new ArgumentException("must be an enum type", nameof(enumType));
}
info = new NonGenericEnumInfo(GetInfo(nonNullableEnumType), true);
}
Dictionary<Type, NonGenericEnumInfo> oldNonGenericEnumInfos;
do
{
NonGenericEnumInfo foundInfo;
if (nonGenericEnumInfos.TryGetValue(enumType, out foundInfo))
{
return foundInfo;
}
oldNonGenericEnumInfos = nonGenericEnumInfos;
nonGenericEnumInfos = new Dictionary<Type, NonGenericEnumInfo>(nonGenericEnumInfos);
nonGenericEnumInfos.Add(enumType, info);
} while ((nonGenericEnumInfos = Interlocked.CompareExchange(ref _nonGenericEnumInfos, nonGenericEnumInfos, oldNonGenericEnumInfos)) != oldNonGenericEnumInfos);
}
return info;
}
示例13: Create
public static Instruction Create(Type type)
{
Debug.Assert(!type.IsEnum());
switch (type.GetTypeCode()) {
case TypeCode.SByte: return _SByte ?? (_SByte = new LessThanSByte());
case TypeCode.Byte: return _Byte ?? (_Byte = new LessThanByte());
case TypeCode.Char: return _Char ?? (_Char = new LessThanChar());
case TypeCode.Int16: return _Int16 ?? (_Int16 = new LessThanInt16());
case TypeCode.Int32: return _Int32 ?? (_Int32 = new LessThanInt32());
case TypeCode.Int64: return _Int64 ?? (_Int64 = new LessThanInt64());
case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new LessThanUInt16());
case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new LessThanUInt32());
case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new LessThanUInt64());
case TypeCode.Single: return _Single ?? (_Single = new LessThanSingle());
case TypeCode.Double: return _Double ?? (_Double = new LessThanDouble());
default:
throw Assert.Unreachable;
}
}
示例14: ToQuotedString
public override string ToQuotedString(Type fieldType, object value)
{
var isEnumAsInt = fieldType.HasAttribute<EnumAsIntAttribute>();
if (isEnumAsInt)
return this.ConvertNumber(Enum.GetUnderlyingType(fieldType), value).ToString();
var isEnumFlags = fieldType.IsEnumFlags() ||
(!fieldType.IsEnum() && fieldType.IsNumericType()); //i.e. is real int && not Enum
long enumValue;
if (!isEnumFlags && long.TryParse(value.ToString(), out enumValue))
value = Enum.ToObject(fieldType, enumValue);
var enumString = DialectProvider.StringSerializer.SerializeToString(value);
if (enumString == null || enumString == "null")
enumString = value.ToString();
return !isEnumFlags
? DialectProvider.GetQuotedValue(enumString.Trim('"'))
: enumString;
}
示例15: read
private object read(Type nativeType)
{
object primitiveValue = _current.GetPrimitiveValue();
if (nativeType.IsEnum() && primitiveValue.GetType() == typeof(string))
{
var enumMapping = _inspector.FindEnumMappingByType(nativeType);
if (enumMapping != null)
return enumMapping.ParseLiteral((string)primitiveValue);
}
try
{
return PrimitiveTypeConverter.Convert(primitiveValue, nativeType);
}
catch (NotSupportedException exc)
{
// thrown when an unsupported conversion was required
throw Error.Format(exc.Message, _current);
}
}