本文整理汇总了C#中Type.IsEnum方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsEnum方法的具体用法?C# Type.IsEnum怎么用?C# Type.IsEnum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.IsEnum方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryCreateType
public virtual EdmType TryCreateType(Type type, EdmType cspaceType)
{
DebugCheck.NotNull(type);
DebugCheck.NotNull(cspaceType);
Debug.Assert(cspaceType is StructuralType || Helper.IsEnumType(cspaceType), "Structural or enum type expected");
// if one of the types is an enum while the other is not there is no match
if (Helper.IsEnumType(cspaceType)
^ type.IsEnum())
{
LogLoadMessage(
Strings.Validator_OSpace_Convention_SSpaceOSpaceTypeMismatch(cspaceType.FullName, cspaceType.FullName),
cspaceType);
return null;
}
EdmType newOSpaceType;
if (Helper.IsEnumType(cspaceType))
{
TryCreateEnumType(type, (EnumType)cspaceType, out newOSpaceType);
return newOSpaceType;
}
Debug.Assert(cspaceType is StructuralType);
TryCreateStructuralType(type, (StructuralType)cspaceType, out newOSpaceType);
return newOSpaceType;
}
示例2: ClrEnumType
// <summary>
// Initializes a new instance of ClrEnumType class with properties from the CLR type.
// </summary>
// <param name="clrType"> The CLR type to construct from. </param>
// <param name="cspaceNamespaceName"> CSpace namespace name. </param>
// <param name="cspaceTypeName"> CSpace type name. </param>
internal ClrEnumType(Type clrType, string cspaceNamespaceName, string cspaceTypeName)
: base(clrType)
{
DebugCheck.NotNull(clrType);
DebugCheck.NotEmpty(cspaceNamespaceName);
DebugCheck.NotEmpty(cspaceTypeName);
Debug.Assert(clrType.IsEnum(), "enum type expected");
_type = clrType;
_cspaceTypeName = cspaceNamespaceName + "." + cspaceTypeName;
}
示例3: TryCreateEnumType
private bool TryCreateEnumType(Type enumType, EnumType cspaceEnumType, out EdmType newOSpaceType)
{
DebugCheck.NotNull(enumType);
Debug.Assert(enumType.IsEnum(), "enum type expected");
DebugCheck.NotNull(cspaceEnumType);
Debug.Assert(Helper.IsEnumType(cspaceEnumType), "Enum type expected");
newOSpaceType = null;
// Check if the OSpace and CSpace enum type match
if (!UnderlyingEnumTypesMatch(enumType, cspaceEnumType)
|| !EnumMembersMatch(enumType, cspaceEnumType))
{
return false;
}
newOSpaceType = new ClrEnumType(enumType, cspaceEnumType.NamespaceName, cspaceEnumType.Name);
LoadedTypes.Add(enumType.FullName, newOSpaceType);
return true;
}
示例4: EnumType
// <summary>
// Initializes a new instance of the EnumType class from CLR enumeration type.
// </summary>
// <param name="clrType"> CLR enumeration type to create EnumType from. </param>
// <remarks>
// Note that this method expects that the <paramref name="clrType" /> is a valid CLR enum type
// whose underlying type is a valid EDM primitive type.
// Ideally this constructor should be protected and internal (Family and Assembly modifier) but
// C# does not support this. In order to not expose this constructor to everyone internal is the
// only option.
// </remarks>
internal EnumType(Type clrType)
:
base(clrType.Name, clrType.NestingNamespace() ?? string.Empty, DataSpace.OSpace)
{
DebugCheck.NotNull(clrType);
Debug.Assert(clrType.IsEnum(), "enum type expected");
ClrProviderManifest.Instance.TryGetPrimitiveType(clrType.GetEnumUnderlyingType(), out _underlyingType);
Debug.Assert(_underlyingType != null, "only primitive types expected here.");
Debug.Assert(
Helper.IsSupportedEnumUnderlyingType(_underlyingType.PrimitiveTypeKind),
"unsupported CLR types should have been filtered out by .TryGetPrimitiveType() method.");
_isFlags = clrType.GetCustomAttributes<FlagsAttribute>(inherit: false).Any();
foreach (var name in Enum.GetNames(clrType))
{
AddMember(
new EnumMember(
name,
Convert.ChangeType(Enum.Parse(clrType, name), clrType.GetEnumUnderlyingType(), CultureInfo.InvariantCulture)));
}
}
示例5: TryGetPrimitiveTypeKind
// <summary>
// Returns the <see cref="PrimitiveTypeKind" /> corresponding to the given CLR type
// </summary>
// <param name="clrType"> The CLR type for which the PrimitiveTypeKind value should be resolved </param>
// <param name="resolvedPrimitiveTypeKind"> The PrimitiveTypeKind value to which the CLR type resolves, if any. </param>
// <returns> True if the CLR type represents a primitive (EDM) type; otherwise false. </returns>
internal static bool TryGetPrimitiveTypeKind(Type clrType, out PrimitiveTypeKind resolvedPrimitiveTypeKind)
{
PrimitiveTypeKind? primitiveTypeKind = null;
if (!clrType.IsEnum()) // Enums return the TypeCode of their underlying type
{
// As an optimization, short-circuit when the provided type has a known type code.
switch (Type.GetTypeCode(clrType))
{
// PrimitiveTypeKind.Binary = byte[] = TypeCode.Object
case TypeCode.Boolean:
primitiveTypeKind = PrimitiveTypeKind.Boolean;
break;
case TypeCode.Byte:
primitiveTypeKind = PrimitiveTypeKind.Byte;
break;
case TypeCode.DateTime:
primitiveTypeKind = PrimitiveTypeKind.DateTime;
break;
// PrimitiveTypeKind.DateTimeOffset = System.DateTimeOffset = TypeCode.Object
case TypeCode.Decimal:
primitiveTypeKind = PrimitiveTypeKind.Decimal;
break;
case TypeCode.Double:
primitiveTypeKind = PrimitiveTypeKind.Double;
break;
// PrimitiveTypeKind.Geography = System.Data.Entity.Spatial.DbGeometry (or subtype) = TypeCode.Object
// PrimitiveTypeKind.Geometry = System.Data.Entity.Spatial.DbGeometry (or subtype) = TypeCode.Object
// PrimitiveTypeKind.Guid = System.Guid = TypeCode.Object
case TypeCode.Int16:
primitiveTypeKind = PrimitiveTypeKind.Int16;
break;
case TypeCode.Int32:
primitiveTypeKind = PrimitiveTypeKind.Int32;
break;
case TypeCode.Int64:
primitiveTypeKind = PrimitiveTypeKind.Int64;
break;
case TypeCode.SByte:
primitiveTypeKind = PrimitiveTypeKind.SByte;
break;
case TypeCode.Single:
primitiveTypeKind = PrimitiveTypeKind.Single;
break;
case TypeCode.String:
primitiveTypeKind = PrimitiveTypeKind.String;
break;
// PrimitiveTypeKind.Time = System.TimeSpan = TypeCode.Object
case TypeCode.Object:
{
if (typeof(byte[]) == clrType)
{
primitiveTypeKind = PrimitiveTypeKind.Binary;
}
else if (typeof(DateTimeOffset) == clrType)
{
primitiveTypeKind = PrimitiveTypeKind.DateTimeOffset;
}
// DbGeography/Geometry are abstract so subtypes must be allowed
else if (typeof(DbGeography).IsAssignableFrom(clrType))
{
primitiveTypeKind = PrimitiveTypeKind.Geography;
}
else if (typeof(DbGeometry).IsAssignableFrom(clrType))
{
primitiveTypeKind = PrimitiveTypeKind.Geometry;
}
else if (typeof(Guid) == clrType)
{
primitiveTypeKind = PrimitiveTypeKind.Guid;
}
else if (typeof(TimeSpan) == clrType)
{
primitiveTypeKind = PrimitiveTypeKind.Time;
}
break;
}
}
}
if (primitiveTypeKind.HasValue)
{
resolvedPrimitiveTypeKind = primitiveTypeKind.Value;
return true;
}
else
{
resolvedPrimitiveTypeKind = default(PrimitiveTypeKind);
return false;
}
}
示例6: IsEnumType
private static bool IsEnumType(Type type)
{
type.TryUnwrapNullableType(out type);
return type.IsEnum();
}
示例7: EnumMembersMatch
private bool EnumMembersMatch(Type enumType, EnumType cspaceEnumType)
{
DebugCheck.NotNull(enumType);
Debug.Assert(enumType.IsEnum(), "expected enum OSpace type");
DebugCheck.NotNull(cspaceEnumType);
Debug.Assert(Helper.IsEnumType(cspaceEnumType), "Enum type expected");
Debug.Assert(
cspaceEnumType.UnderlyingType.ClrEquivalentType == enumType.GetEnumUnderlyingType(),
"underlying types should have already been checked");
var enumUnderlyingType = enumType.GetEnumUnderlyingType();
var cspaceSortedEnumMemberEnumerator = cspaceEnumType.Members.OrderBy(m => m.Name).GetEnumerator();
var ospaceSortedEnumMemberNamesEnumerator = enumType.GetEnumNames().OrderBy(n => n).GetEnumerator();
// no checks required if edm enum type does not have any members
if (!cspaceSortedEnumMemberEnumerator.MoveNext())
{
return true;
}
while (ospaceSortedEnumMemberNamesEnumerator.MoveNext())
{
if (cspaceSortedEnumMemberEnumerator.Current.Name == ospaceSortedEnumMemberNamesEnumerator.Current
&&
cspaceSortedEnumMemberEnumerator.Current.Value.Equals(
Convert.ChangeType(
Enum.Parse(enumType, ospaceSortedEnumMemberNamesEnumerator.Current), enumUnderlyingType,
CultureInfo.InvariantCulture)))
{
if (!cspaceSortedEnumMemberEnumerator.MoveNext())
{
return true;
}
}
}
LogLoadMessage(
Strings.Mapping_Enum_OCMapping_MemberMismatch(
enumType.FullName,
cspaceSortedEnumMemberEnumerator.Current.Name,
cspaceSortedEnumMemberEnumerator.Current.Value,
cspaceEnumType.FullName), cspaceEnumType);
return false;
}
示例8: UnderlyingEnumTypesMatch
private bool UnderlyingEnumTypesMatch(Type enumType, EnumType cspaceEnumType)
{
DebugCheck.NotNull(enumType);
Debug.Assert(enumType.IsEnum(), "expected enum OSpace type");
DebugCheck.NotNull(cspaceEnumType);
Debug.Assert(Helper.IsEnumType(cspaceEnumType), "Enum type expected");
// Note that TryGetPrimitiveType() will return false not only for types that are not primitive
// but also for CLR primitive types that are valid underlying enum types in CLR but are not
// a valid Edm primitive types (e.g. ulong)
PrimitiveType underlyingEnumType;
if (!ClrProviderManifest.Instance.TryGetPrimitiveType(enumType.GetEnumUnderlyingType(), out underlyingEnumType))
{
LogLoadMessage(
Strings.Validator_UnsupportedEnumUnderlyingType(enumType.GetEnumUnderlyingType().FullName),
cspaceEnumType);
return false;
}
else if (underlyingEnumType.PrimitiveTypeKind
!= cspaceEnumType.UnderlyingType.PrimitiveTypeKind)
{
LogLoadMessage(
Strings.Validator_OSpace_Convention_NonMatchingUnderlyingTypes, cspaceEnumType);
return false;
}
return true;
}
示例9: IsArrayTypeSupported
private static bool IsArrayTypeSupported(Type type)
{
if (type.IsEnum())
return false;
switch (type.GetTypeCode())
{
case TypeCode.Byte:
case TypeCode.Boolean:
case TypeCode.DateTime:
case TypeCode.Decimal:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
return true;
default:
return false;
}
}
示例10: ParseEnum
static object ParseEnum(string name, Type type)
{
#if !(NETFX_CORE || DNXCORE50)
if (type.IsEnum)
{
MemberInfo[] memberInfos = type.FindMembers(MemberTypes.Field,
BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static,
Type.FilterNameIgnoreCase, name);
if (memberInfos.Length != 0) return ((FieldInfo)memberInfos[0]).GetValue(null);
}
#else
if (type.IsEnum())
{
return Enum.Parse(type, name, true);
}
#endif
return null;
}
示例11: GetNumericTypeKind
static int GetNumericTypeKind(Type type)
{
type = GetNonNullableType(type);
#if !(NETFX_CORE || DNXCORE50 || DOTNET5_4)
if (type.IsEnum()) return 0;
switch (Type.GetTypeCode(type))
{
case TypeCode.Char:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return 1;
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return 2;
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return 3;
default:
return 0;
}
#else
if (type.IsEnum()) return 0;
if (type == typeof(Char) || type == typeof(Single) || type == typeof(Double) || type == typeof(Decimal))
return 1;
if (type == typeof(SByte) || type == typeof(Int16) || type == typeof(Int32) || type == typeof(Int64))
return 2;
if (type == typeof(Byte) || type == typeof(UInt16) || type == typeof(UInt32) || type == typeof(UInt64))
return 3;
return 0;
#endif
}
示例12: ParseMemberAccess
Expression ParseMemberAccess(Type type, Expression instance)
{
if (instance != null) type = instance.Type;
int errorPos = _token.pos;
string id = GetIdentifier();
NextToken();
if (_token.id == TokenId.OpenParen)
{
if (instance != null && type != typeof(string))
{
Type enumerableType = FindGenericType(typeof(IEnumerable<>), type);
if (enumerableType != null)
{
#if DNXCORE50 || DOTNET5_4
Type elementType = ReflectionBridgeExtensions.GetGenericArguments(enumerableType)[0];
#else
Type elementType = enumerableType.GetGenericArguments()[0];
#endif
return ParseAggregate(instance, elementType, id, errorPos);
}
}
Expression[] args = ParseArgumentList();
MethodBase mb;
switch (FindMethod(type, id, instance == null, args, out mb))
{
case 0:
throw ParseError(errorPos, Res.NoApplicableMethod, id, GetTypeName(type));
case 1:
MethodInfo method = (MethodInfo)mb;
if (!IsPredefinedType(method.DeclaringType))
throw ParseError(errorPos, Res.MethodsAreInaccessible, GetTypeName(method.DeclaringType));
if (method.ReturnType == typeof(void))
throw ParseError(errorPos, Res.MethodIsVoid, id, GetTypeName(method.DeclaringType));
return Expression.Call(instance, (MethodInfo)method, args);
default:
throw ParseError(errorPos, Res.AmbiguousMethodInvocation, id, GetTypeName(type));
}
}
else
{
if (type.IsEnum())
{
var wr = Enum.Parse(type, id, true);
if (wr != null)
return Expression.Constant(wr);
}
#if NETFX_CORE
if (type == typeof(DynamicObjectClass))
{
return Expression.MakeIndex(instance, typeof(DynamicObjectClass).GetProperty("Item"), new[] { Expression.Constant(id) });
}
#endif
MemberInfo member = FindPropertyOrField(type, id, instance == null);
if (member == null)
{
throw ParseError(errorPos, Res.UnknownPropertyOrField,
id, GetTypeName(type));
}
var property = member as PropertyInfo;
if (property != null) return Expression.Property(instance, property);
return Expression.Field(instance, (FieldInfo)member);
}
}