本文整理汇总了C#中Type.GetEnumUnderlyingType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetEnumUnderlyingType方法的具体用法?C# Type.GetEnumUnderlyingType怎么用?C# Type.GetEnumUnderlyingType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.GetEnumUnderlyingType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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)));
}
}
示例2: GetTypeCode
public static TypeCode GetTypeCode(Type type)
{
if (type == null)
{
return TypeCode.Empty;
}
if (!type.__IsMissing && type.IsEnum)
{
type = type.GetEnumUnderlyingType();
}
Universe u = type.Module.universe;
if (type == u.System_Boolean)
{
return TypeCode.Boolean;
}
else if (type == u.System_Char)
{
return TypeCode.Char;
}
else if (type == u.System_SByte)
{
return TypeCode.SByte;
}
else if (type == u.System_Byte)
{
return TypeCode.Byte;
}
else if (type == u.System_Int16)
{
return TypeCode.Int16;
}
else if (type == u.System_UInt16)
{
return TypeCode.UInt16;
}
else if (type == u.System_Int32)
{
return TypeCode.Int32;
}
else if (type == u.System_UInt32)
{
return TypeCode.UInt32;
}
else if (type == u.System_Int64)
{
return TypeCode.Int64;
}
else if (type == u.System_UInt64)
{
return TypeCode.UInt64;
}
else if (type == u.System_Single)
{
return TypeCode.Single;
}
else if (type == u.System_Double)
{
return TypeCode.Double;
}
else if (type == u.System_DateTime)
{
return TypeCode.DateTime;
}
else if (type == u.System_DBNull)
{
return TypeCode.DBNull;
}
else if (type == u.System_Decimal)
{
return TypeCode.Decimal;
}
else if (type == u.System_String)
{
return TypeCode.String;
}
else if (type.__IsMissing)
{
throw new MissingMemberException(type);
}
else
{
return TypeCode.Object;
}
}
示例3: 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;
}
示例4: 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;
}
示例5: LoadType
/// <summary>
/// Load metadata of the given type - when you call this method, you should check and make sure that the type has
/// edm attribute. If it doesn't,we won't load the type and it will be returned as null
/// </summary>
/// <param name="clrType"> </param>
/// <param name="context"> </param>
/// <returns> </returns>
private void LoadType(Type clrType)
{
Debug.Assert(clrType.Assembly == SourceAssembly, "Why are we loading a type that is not in our assembly?");
Debug.Assert(!SessionData.TypesInLoading.ContainsKey(clrType.FullName), "Trying to load a type that is already loaded???");
Debug.Assert(!clrType.IsGenericType, "Generic type is not supported");
EdmType edmType = null;
var typeAttributes = (EdmTypeAttribute[])clrType.GetCustomAttributes(typeof(EdmTypeAttribute), false /*inherit*/);
// the CLR doesn't allow types to have duplicate/multiple attribute declarations
if (typeAttributes.Length != 0)
{
if (clrType.IsNested)
{
SessionData.EdmItemErrors.Add(
new EdmItemError(Strings.NestedClassNotSupported(clrType.FullName, clrType.Assembly.FullName)));
return;
}
var typeAttribute = typeAttributes[0];
var cspaceTypeName = String.IsNullOrEmpty(typeAttribute.Name) ? clrType.Name : typeAttribute.Name;
if (String.IsNullOrEmpty(typeAttribute.NamespaceName)
&& clrType.Namespace == null)
{
SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_TypeHasNoNamespace));
return;
}
var cspaceNamespaceName = String.IsNullOrEmpty(typeAttribute.NamespaceName)
? clrType.Namespace
: typeAttribute.NamespaceName;
if (typeAttribute.GetType()
== typeof(EdmEntityTypeAttribute))
{
edmType = new ClrEntityType(clrType, cspaceNamespaceName, cspaceTypeName);
}
else if (typeAttribute.GetType()
== typeof(EdmComplexTypeAttribute))
{
edmType = new ClrComplexType(clrType, cspaceNamespaceName, cspaceTypeName);
}
else
{
Debug.Assert(typeAttribute is EdmEnumTypeAttribute, "Invalid type attribute encountered");
// 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(clrType.GetEnumUnderlyingType(), out underlyingEnumType))
{
SessionData.EdmItemErrors.Add(
new EdmItemError(
Strings.Validator_UnsupportedEnumUnderlyingType(clrType.GetEnumUnderlyingType().FullName)));
return;
}
edmType = new ClrEnumType(clrType, cspaceNamespaceName, cspaceTypeName);
}
}
else
{
// not a type we are interested
return;
}
Debug.Assert(
!CacheEntry.ContainsType(edmType.Identity), "This type must not be already present in the list of types for this assembly");
// Also add this to the list of the types for this assembly
CacheEntry.TypesInAssembly.Add(edmType);
// Add this to the known type map so we won't try to load it again
SessionData.TypesInLoading.Add(clrType.FullName, edmType);
// Load properties for structural type
if (Helper.IsStructuralType(edmType))
{
//Load base type only for entity type - not sure if we will allow complex type inheritance
if (Helper.IsEntityType(edmType))
{
TrackClosure(clrType.BaseType);
AddTypeResolver(
() => edmType.BaseType = ResolveBaseType(clrType.BaseType));
}
// Load the properties for this type
LoadPropertiesFromType((StructuralType)edmType);
}
return;
//.........这里部分代码省略.........