本文整理汇总了C#中System.Type.GetEnumUnderlyingType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetEnumUnderlyingType方法的具体用法?C# Type.GetEnumUnderlyingType怎么用?C# Type.GetEnumUnderlyingType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetEnumUnderlyingType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateEnumTypeDeclaration
private void GenerateEnumTypeDeclaration(Type enumType)
{
DataContractAttribute dataContractAttr = (DataContractAttribute)Attribute.GetCustomAttribute(enumType, typeof(DataContractAttribute));
if (dataContractAttr != null)
{
this.GenerateDataContractAttribute(enumType);
}
if (enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0)
{
this.Write("[System.Flags]\r\n");
}
this.Write("public enum ");
this.Write(this.ToStringHelper.ToStringWithCulture(CodeGenUtilities.GetSafeName(enumType.Name)));
Type underlyingType = enumType.GetEnumUnderlyingType();
if (underlyingType != typeof(int))
{
this.Write(" : ");
this.Write(this.ToStringHelper.ToStringWithCulture(CodeGenUtilities.GetTypeName(underlyingType)));
}
}
示例2: EnumConfiguration
public EnumConfiguration(Type enumType)
{
_signed = IsSignedEnum(enumType);
_flags = IsFlagsEnum(enumType);
_names = enumType.GetEnumNames();
var members = enumType.GetMembers(BindingFlags.Static | BindingFlags.Public);
Debug.Assert(members.Length == _names.Length);
for (int i = 0; i < members.Length; i++)
{
var a = members[i].GetCustomAttributes<PersistedNameAttribute>().FirstOrDefault();
if (a != null) _names[i] = a.Name;
}
var undertype = enumType.GetEnumUnderlyingType();
var enumValues = enumType.GetEnumValues();
IEnumerable<ulong> enumValuesUlongs;
if (undertype == typeof(int)) enumValuesUlongs = enumValues.Cast<int>().Select(i => (ulong)i);
else if (undertype == typeof(uint)) enumValuesUlongs = enumValues.Cast<uint>().Select(i => (ulong)i);
else if (undertype == typeof(sbyte)) enumValuesUlongs = enumValues.Cast<sbyte>().Select(i => (ulong)i);
else if (undertype == typeof(byte)) enumValuesUlongs = enumValues.Cast<byte>().Select(i => (ulong)i);
else if (undertype == typeof(short)) enumValuesUlongs = enumValues.Cast<short>().Select(i => (ulong)i);
else if (undertype == typeof(ushort)) enumValuesUlongs = enumValues.Cast<ushort>().Select(i => (ulong)i);
else if (undertype == typeof(long)) enumValuesUlongs = enumValues.Cast<long>().Select(i => (ulong)i);
else enumValuesUlongs = enumValues.Cast<ulong>();
_values = enumValuesUlongs.ToArray();
}
示例3: ToQuotedString
public override string ToQuotedString(Type fieldType, object value)
{
if (fieldType.HasAttribute<EnumAsIntAttribute>())
{
return this.ConvertNumber(fieldType.GetEnumUnderlyingType(), value).ToString();
}
if (value is int && !fieldType.IsEnumFlags())
{
value = fieldType.GetEnumName(value);
}
if (fieldType.IsEnum)
{
var enumValue = DialectProvider.StringSerializer.SerializeToString(value);
// Oracle stores empty strings in varchar columns as null so match that behavior here
if (enumValue == null)
return null;
enumValue = DialectProvider.GetQuotedValue(enumValue.Trim('"'));
return enumValue == "''"
? "null"
: enumValue;
}
return base.ToQuotedString(fieldType, value);
}
示例4: GenerateEnum2EnumConversion
Action<IILGen> GenerateEnum2EnumConversion(Type from, Type to)
{
var fromcfg = new EnumFieldHandler.EnumConfiguration(from);
var tocfg = new EnumFieldHandler.EnumConfiguration(to);
if (fromcfg.IsSubsetOf(tocfg))
{
return GenerateConversion(from.GetEnumUnderlyingType(), to.GetEnumUnderlyingType());
}
return null;
}
示例5: TypeToAutomationType
public static UIAutomationType TypeToAutomationType(Type type)
{
if (IsElementOnServerSide(type))
return UIAutomationType.UIAutomationType_Element;
if (type.IsEnum && type.GetEnumUnderlyingType() == typeof(int))
type = typeof(int);
UIAutomationType res;
if (_typeMapping.TryGetValue(type, out res))
return res;
throw new NotSupportedException("Provided type is not supported");
}
示例6: GetConvertedValue
protected Expression GetConvertedValue(Type targetType)
{
var value = ExtendedProperties.GetExtendedProperty("value") ?? String.Empty;
if (targetType.IsEnum)
return Expression.Constant(((IConvertible)Enum.Parse(targetType, value, true)).ToType(targetType.GetEnumUnderlyingType(), CultureInfo.InvariantCulture));
return typeof(IConvertible).IsAssignableFrom(targetType)
? Expression.Constant(((IConvertible)value).ToType(targetType, CultureInfo.InvariantCulture))
: Expression.Convert(Expression.Constant(value), targetType) as Expression;
}
示例7: WriteEnumList
/// <summary>
/// </summary>
/// <param name="fileName">
/// </param>
/// <param name="enumName">
/// </param>
/// <param name="enumType">
/// </param>
/// <param name="writeValues">
/// </param>
public static void WriteEnumList(string fileName, string enumName, Type enumType, bool writeValues = true)
{
bool isFlagsEnum = false;
object[] memInfo = enumType.GetCustomAttributes(typeof(FlagsAttribute), false);
isFlagsEnum = memInfo.Length > 0;
TextWriter tw = new StreamWriter(fileName);
WriteHeader1(tw, enumName);
WriteCode(tw, enumType.FullName);
if (writeValues)
{
tw.Write(" : ");
WriteCode(tw, enumType.GetEnumUnderlyingType().Name);
}
if (isFlagsEnum)
{
tw.WriteLine();
tw.WriteLine();
WriteCode(tw, "[Flags]");
}
tw.WriteLine();
WriteHorizonalLine(tw);
tw.WriteLine();
Array temp2 = Enum.GetValues(enumType);
int max = temp2.Length;
foreach (object v in temp2)
{
string eName = Enum.GetName(enumType, v);
tw.Write("**" + eName + "**");
if (writeValues)
{
object underlyingValue = Convert.ChangeType(v, Enum.GetUnderlyingType(v.GetType()));
tw.Write(" = " + underlyingValue);
}
max--;
if (max > 0)
{
tw.WriteLine(",");
}
tw.WriteLine();
}
WriteFooter(tw);
tw.Close();
}
示例8: ReadEnum
public Enum ReadEnum(Type enumType, ByteOrder byteOrder)
{
Require.NotNull(enumType, "enumType");
Require.That("enumType", enumType.IsEnum);
var enumBaseType = enumType.GetEnumUnderlyingType();
if (enumBaseType == typeof(byte))
{
var value = ReadByte();
return (Enum)Enum.ToObject(enumType, value);
}
if (enumBaseType == typeof(sbyte))
{
var value = ReadSByte();
return (Enum)Enum.ToObject(enumType, value);
}
if(enumBaseType == typeof(int))
{
var value = ReadInt32(byteOrder);
return (Enum)Enum.ToObject(enumType, value);
}
if (enumBaseType == typeof(uint))
{
var value = ReadUInt32(byteOrder);
return (Enum)Enum.ToObject(enumType, value);
}
if (enumBaseType == typeof(short))
{
var value = ReadInt16(byteOrder);
return (Enum)Enum.ToObject(enumType, value);
}
if (enumBaseType == typeof(ushort))
{
var value = ReadUInt16(byteOrder);
return (Enum)Enum.ToObject(enumType, value);
}
if (enumBaseType == typeof(long))
{
var value = ReadInt64(byteOrder);
return (Enum)Enum.ToObject(enumType, value);
}
if (enumBaseType == typeof(ulong))
{
var value = ReadUInt64(byteOrder);
return (Enum)Enum.ToObject(enumType, value);
}
throw new ArgumentOutOfRangeException("enumType", "The provided argument did not meet the specified requirements.");
}
示例9: EmitLdDefaultValue
public static void EmitLdDefaultValue(this ILGenerator gen, Type returnType)
{
if (returnType.IsEnum)
returnType = returnType.GetEnumUnderlyingType();
if (returnType==typeof(void)) {}
else if (returnType.IsClass || returnType.IsInterface)
{
gen.Emit(OpCodes.Ldnull);
}
else if (I4CompatibleTypes.Contains(returnType))
{
gen.Emit(OpCodes.Ldc_I4_0);
}
else if (returnType==typeof(double))
{
gen.Emit(OpCodes.Ldc_R8, 0.0);
}
else if (returnType==typeof(float))
{
gen.Emit(OpCodes.Ldc_R4, 0.0f);
}
else if (returnType==typeof(long) || returnType==typeof(ulong))
{
gen.Emit(OpCodes.Ldc_I8, 0L);
}
else if (returnType==typeof(decimal))
{
var consDecimal = typeof(decimal).GetConstructor(new[] {typeof(int)});
gen.Emit(OpCodes.Ldc_I4_0);
gen.Emit(OpCodes.Newobj, consDecimal);
}
else if (returnType.IsValueType)
{
var returnTypeLocal = gen.DeclareLocal(returnType);
gen.Emit(OpCodes.Ldloca, returnTypeLocal);
gen.Emit(OpCodes.Initobj, returnType);
gen.Emit(OpCodes.Ldloc, returnTypeLocal);
}
else
{
throw new InvalidOperationException("Unable to handle return type");
}
}
示例10: EnumTypeDescriptor
public EnumTypeDescriptor(ITypeDescriptorCallbacks typeSerializers, Type type)
{
_typeSerializers = typeSerializers;
_type = type;
_name = typeSerializers.TypeNameMapper.ToName(type);
_signed = IsSignedEnum(type);
_flags = IsFlagsEnum(type);
var undertype = type.GetEnumUnderlyingType();
var enumValues = type.GetEnumValues();
IEnumerable<ulong> enumValuesUlongs;
if (undertype == typeof(int)) enumValuesUlongs = enumValues.Cast<int>().Select(i => (ulong)i);
else if (undertype == typeof(uint)) enumValuesUlongs = enumValues.Cast<uint>().Select(i => (ulong)i);
else if (undertype == typeof(sbyte)) enumValuesUlongs = enumValues.Cast<sbyte>().Select(i => (ulong)i);
else if (undertype == typeof(byte)) enumValuesUlongs = enumValues.Cast<byte>().Select(i => (ulong)i);
else if (undertype == typeof(short)) enumValuesUlongs = enumValues.Cast<short>().Select(i => (ulong)i);
else if (undertype == typeof(ushort)) enumValuesUlongs = enumValues.Cast<ushort>().Select(i => (ulong)i);
else if (undertype == typeof(long)) enumValuesUlongs = enumValues.Cast<long>().Select(i => (ulong)i);
else enumValuesUlongs = enumValues.Cast<ulong>();
_pairs = type.GetEnumNames().Zip(enumValuesUlongs.ToArray(), (s, v) => new KeyValuePair<string, ulong>(s, v)).ToList();
}
示例11: ToQuotedString
public override string ToQuotedString(Type fieldType, object value)
{
var isEnumAsInt = fieldType.HasAttribute<EnumAsIntAttribute>();
if (isEnumAsInt)
return this.ConvertNumber(fieldType.GetEnumUnderlyingType(), 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;
}
示例12: WritePrimitiveValue
/// <summary>
/// Writes a primitive value of the specified type.
/// Stack transition: ..., writer, value => ...
/// </summary>
void WritePrimitiveValue(ILGenerator il, Type fieldType)
{
if (fieldType.IsEnum) {
fieldType = fieldType.GetEnumUnderlyingType();
Debug.Assert(fieldType.IsPrimitive);
}
switch (Type.GetTypeCode(fieldType)) {
case TypeCode.Boolean:
case TypeCode.SByte:
case TypeCode.Byte:
il.Emit(callVirt, writeByte); // writer.Write(value);
break;
case TypeCode.Char:
case TypeCode.Int16:
case TypeCode.UInt16:
il.Emit(callVirt, writeShort); // writer.Write(value);
break;
case TypeCode.Int32:
case TypeCode.UInt32:
il.Emit(callVirt, writeInt); // writer.Write(value);
break;
case TypeCode.Int64:
case TypeCode.UInt64:
il.Emit(callVirt, writeLong); // writer.Write(value);
break;
case TypeCode.Single:
il.Emit(callVirt, writeFloat); // writer.Write(value);
break;
case TypeCode.Double:
il.Emit(callVirt, writeDouble); // writer.Write(value);
break;
default:
throw new NotSupportedException("Unknown primitive type " + fieldType);
}
}
示例13: CreateWriter
ObjectWriter CreateWriter(Type type)
{
if (type == typeof(string)) {
// String contents are written in the object creation section,
// not into the field value section.
return delegate {};
}
bool isArray = type.IsArray;
if (isArray) {
if (type.GetArrayRank() != 1)
throw new NotSupportedException();
type = type.GetElementType();
if (!type.IsValueType) {
return delegate (SerializationContext context, object array) {
foreach (object val in (object[])array) {
context.WriteObjectID(val);
}
};
} else if (type == typeof(byte)) {
return delegate (SerializationContext context, object array) {
context.writer.Write((byte[])array);
};
}
}
List<FieldInfo> fields = GetSerializableFields(type);
if (fields.Count == 0) {
// The writer has nothing to do for this object.
return delegate { };
}
DynamicMethod dynamicMethod = new DynamicMethod(
(isArray ? "WriteArray_" : "Write_") + type.Name,
typeof(void), new [] { typeof(SerializationContext), typeof(object) },
true);
ILGenerator il = dynamicMethod.GetILGenerator();
var writer = il.DeclareLocal(typeof(BinaryWriter));
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, writerField);
il.Emit(OpCodes.Stloc, writer); // writer = context.writer;
if (isArray) {
var instance = il.DeclareLocal(type.MakeArrayType());
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Castclass, type.MakeArrayType());
il.Emit(OpCodes.Stloc, instance); // instance = (type[])arg_1;
// for (int i = 0; i < instance.Length; i++) write instance[i];
var loopStart = il.DefineLabel();
var loopHead = il.DefineLabel();
var loopVariable = il.DeclareLocal(typeof(int));
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Stloc, loopVariable); // loopVariable = 0
il.Emit(OpCodes.Br, loopHead); // goto loopHead;
il.MarkLabel(loopStart);
if (type.IsEnum || type.IsPrimitive) {
if (type.IsEnum) {
type = type.GetEnumUnderlyingType();
}
Debug.Assert(type.IsPrimitive);
il.Emit(OpCodes.Ldloc, writer); // writer
il.Emit(OpCodes.Ldloc, instance); // writer, instance
il.Emit(OpCodes.Ldloc, loopVariable); // writer, instance, loopVariable
switch (Type.GetTypeCode(type)) {
case TypeCode.Boolean:
case TypeCode.SByte:
case TypeCode.Byte:
il.Emit(OpCodes.Ldelem_I1); // writer, instance[loopVariable]
il.Emit(callVirt, writeByte); // writer.Write(instance[loopVariable]);
break;
case TypeCode.Char:
case TypeCode.Int16:
case TypeCode.UInt16:
il.Emit(OpCodes.Ldelem_I2); // writer, instance[loopVariable]
il.Emit(callVirt, writeShort); // writer.Write(instance[loopVariable]);
break;
case TypeCode.Int32:
case TypeCode.UInt32:
il.Emit(OpCodes.Ldelem_I4); // writer, instance[loopVariable]
il.Emit(callVirt, writeInt); // writer.Write(instance[loopVariable]);
break;
case TypeCode.Int64:
case TypeCode.UInt64:
il.Emit(OpCodes.Ldelem_I8); // writer, instance[loopVariable]
il.Emit(callVirt, writeLong); // writer.Write(instance[loopVariable]);
break;
case TypeCode.Single:
il.Emit(OpCodes.Ldelem_R4); // writer, instance[loopVariable]
il.Emit(callVirt, writeFloat); // writer.Write(instance[loopVariable]);
break;
case TypeCode.Double:
il.Emit(OpCodes.Ldelem_R8); // writer, instance[loopVariable]
il.Emit(callVirt, writeDouble); // writer.Write(instance[loopVariable]);
break;
default:
//.........这里部分代码省略.........
示例14: ReadPrimitiveValue
/// <summary>
/// Reads a primitive value of the specified type.
/// Stack transition: ... => ..., value
/// </summary>
void ReadPrimitiveValue(ILGenerator il, LocalBuilder reader, Type fieldType)
{
if (fieldType.IsEnum) {
fieldType = fieldType.GetEnumUnderlyingType();
Debug.Assert(fieldType.IsPrimitive);
}
il.Emit(OpCodes.Ldloc, reader);
switch (Type.GetTypeCode(fieldType)) {
case TypeCode.Boolean:
case TypeCode.SByte:
case TypeCode.Byte:
il.Emit(callVirt, readByte);
break;
case TypeCode.Char:
case TypeCode.Int16:
case TypeCode.UInt16:
il.Emit(callVirt, readShort);
break;
case TypeCode.Int32:
case TypeCode.UInt32:
il.Emit(callVirt, readInt);
break;
case TypeCode.Int64:
case TypeCode.UInt64:
il.Emit(callVirt, readLong);
break;
case TypeCode.Single:
il.Emit(callVirt, readFloat);
break;
case TypeCode.Double:
il.Emit(callVirt, readDouble);
break;
default:
throw new NotSupportedException("Unknown primitive type " + fieldType);
}
}
示例15: CreateReader
ObjectReader CreateReader(Type type)
{
if (type == typeof(string)) {
// String contents are written in the object creation section,
// not into the field value section; so there's nothing to read here.
return delegate {};
}
bool isArray = type.IsArray;
if (isArray) {
if (type.GetArrayRank() != 1)
throw new NotSupportedException();
type = type.GetElementType();
if (!type.IsValueType) {
return delegate (DeserializationContext context, object arrayInstance) {
object[] array = (object[])arrayInstance;
for (int i = 0; i < array.Length; i++) {
array[i] = context.ReadObject();
}
};
} else if (type == typeof(byte)) {
return delegate (DeserializationContext context, object arrayInstance) {
byte[] array = (byte[])arrayInstance;
BinaryReader binaryReader = context.Reader;
int pos = 0;
int bytesRead;
do {
bytesRead = binaryReader.Read(array, pos, array.Length - pos);
pos += bytesRead;
} while (bytesRead > 0);
if (pos != array.Length)
throw new EndOfStreamException();
};
}
}
var fields = GetSerializableFields(type);
if (fields.Count == 0) {
// The reader has nothing to do for this object.
return delegate { };
}
DynamicMethod dynamicMethod = new DynamicMethod(
(isArray ? "ReadArray_" : "Read_") + type.Name,
MethodAttributes.Public | MethodAttributes.Static,
CallingConventions.Standard,
typeof(void), new [] { typeof(DeserializationContext), typeof(object) },
type,
true);
ILGenerator il = dynamicMethod.GetILGenerator();
var reader = il.DeclareLocal(typeof(BinaryReader));
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, readerField);
il.Emit(OpCodes.Stloc, reader); // reader = context.reader;
if (isArray) {
var instance = il.DeclareLocal(type.MakeArrayType());
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Castclass, type.MakeArrayType());
il.Emit(OpCodes.Stloc, instance); // instance = (type[])arg_1;
// for (int i = 0; i < instance.Length; i++) read &instance[i];
var loopStart = il.DefineLabel();
var loopHead = il.DefineLabel();
var loopVariable = il.DeclareLocal(typeof(int));
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Stloc, loopVariable); // loopVariable = 0
il.Emit(OpCodes.Br, loopHead); // goto loopHead;
il.MarkLabel(loopStart);
if (type.IsEnum || type.IsPrimitive) {
if (type.IsEnum) {
type = type.GetEnumUnderlyingType();
}
Debug.Assert(type.IsPrimitive);
il.Emit(OpCodes.Ldloc, instance); // instance
il.Emit(OpCodes.Ldloc, loopVariable); // instance, loopVariable
ReadPrimitiveValue(il, reader, type); // instance, loopVariable, value
switch (Type.GetTypeCode(type)) {
case TypeCode.Boolean:
case TypeCode.SByte:
case TypeCode.Byte:
il.Emit(OpCodes.Stelem_I1); // instance[loopVariable] = value;
break;
case TypeCode.Char:
case TypeCode.Int16:
case TypeCode.UInt16:
il.Emit(OpCodes.Stelem_I2); // instance[loopVariable] = value;
break;
case TypeCode.Int32:
case TypeCode.UInt32:
il.Emit(OpCodes.Stelem_I4); // instance[loopVariable] = value;
break;
case TypeCode.Int64:
case TypeCode.UInt64:
il.Emit(OpCodes.Stelem_I8); // instance[loopVariable] = value;
break;
case TypeCode.Single:
//.........这里部分代码省略.........