public static TraceLoggingTypeInfo <DataType> CreateDefaultTypeInfo <DataType>( List <Type> recursionCheck) { TraceLoggingTypeInfo result; var dataType = typeof(DataType); if (recursionCheck.Contains(dataType)) { throw new NotSupportedException(Environment.GetResourceString("EventSource_RecursiveTypeDefinition")); } recursionCheck.Add(dataType); var eventAttrib = Statics.GetCustomAttribute <EventDataAttribute>(dataType); if (eventAttrib != null || Statics.GetCustomAttribute <CompilerGeneratedAttribute>(dataType) != null) { var analysis = new TypeAnalysis(dataType, eventAttrib, recursionCheck); result = new InvokeTypeInfo <DataType>(analysis); } else if (dataType.IsArray) { var elementType = dataType.GetElementType(); if (elementType == typeof(Boolean)) { result = new BooleanArrayTypeInfo(); } else if (elementType == typeof(Byte)) { result = new ByteArrayTypeInfo(); } else if (elementType == typeof(SByte)) { result = new SByteArrayTypeInfo(); } else if (elementType == typeof(Int16)) { result = new Int16ArrayTypeInfo(); } else if (elementType == typeof(UInt16)) { result = new UInt16ArrayTypeInfo(); } else if (elementType == typeof(Int32)) { result = new Int32ArrayTypeInfo(); } else if (elementType == typeof(UInt32)) { result = new UInt32ArrayTypeInfo(); } else if (elementType == typeof(Int64)) { result = new Int64ArrayTypeInfo(); } else if (elementType == typeof(UInt64)) { result = new UInt64ArrayTypeInfo(); } else if (elementType == typeof(Char)) { result = new CharArrayTypeInfo(); } else if (elementType == typeof(Double)) { result = new DoubleArrayTypeInfo(); } else if (elementType == typeof(Single)) { result = new SingleArrayTypeInfo(); } else if (elementType == typeof(IntPtr)) { result = new IntPtrArrayTypeInfo(); } else if (elementType == typeof(UIntPtr)) { result = new UIntPtrArrayTypeInfo(); } else if (elementType == typeof(Guid)) { result = new GuidArrayTypeInfo(); } else { result = (TraceLoggingTypeInfo <DataType>)CreateInstance( typeof(ArrayTypeInfo <>).MakeGenericType(elementType), GetTypeInfoInstance(elementType, recursionCheck)); } } else if (Statics.IsEnum(dataType)) { var underlyingType = Enum.GetUnderlyingType(dataType); if (underlyingType == typeof(Int32)) { result = new EnumInt32TypeInfo <DataType>(); } else if (underlyingType == typeof(UInt32)) { result = new EnumUInt32TypeInfo <DataType>(); } else if (underlyingType == typeof(Byte)) { result = new EnumByteTypeInfo <DataType>(); } else if (underlyingType == typeof(SByte)) { result = new EnumSByteTypeInfo <DataType>(); } else if (underlyingType == typeof(Int16)) { result = new EnumInt16TypeInfo <DataType>(); } else if (underlyingType == typeof(UInt16)) { result = new EnumUInt16TypeInfo <DataType>(); } else if (underlyingType == typeof(Int64)) { result = new EnumInt64TypeInfo <DataType>(); } else if (underlyingType == typeof(UInt64)) { result = new EnumUInt64TypeInfo <DataType>(); } else { // Unexpected throw new NotSupportedException(Environment.GetResourceString("EventSource_NotSupportedEnumType", dataType.Name, underlyingType.Name)); } } else if (dataType == typeof(String)) { result = new StringTypeInfo(); } else if (dataType == typeof(Boolean)) { result = new BooleanTypeInfo(); } else if (dataType == typeof(Byte)) { result = new ByteTypeInfo(); } else if (dataType == typeof(SByte)) { result = new SByteTypeInfo(); } else if (dataType == typeof(Int16)) { result = new Int16TypeInfo(); } else if (dataType == typeof(UInt16)) { result = new UInt16TypeInfo(); } else if (dataType == typeof(Int32)) { result = new Int32TypeInfo(); } else if (dataType == typeof(UInt32)) { result = new UInt32TypeInfo(); } else if (dataType == typeof(Int64)) { result = new Int64TypeInfo(); } else if (dataType == typeof(UInt64)) { result = new UInt64TypeInfo(); } else if (dataType == typeof(Char)) { result = new CharTypeInfo(); } else if (dataType == typeof(Double)) { result = new DoubleTypeInfo(); } else if (dataType == typeof(Single)) { result = new SingleTypeInfo(); } else if (dataType == typeof(DateTime)) { result = new DateTimeTypeInfo(); } else if (dataType == typeof(Decimal)) { result = new DecimalTypeInfo(); } else if (dataType == typeof(IntPtr)) { result = new IntPtrTypeInfo(); } else if (dataType == typeof(UIntPtr)) { result = new UIntPtrTypeInfo(); } else if (dataType == typeof(Guid)) { result = new GuidTypeInfo(); } else if (dataType == typeof(TimeSpan)) { result = new TimeSpanTypeInfo(); } else if (dataType == typeof(DateTimeOffset)) { result = new DateTimeOffsetTypeInfo(); } else if (dataType == typeof(EmptyStruct)) { result = new NullTypeInfo <EmptyStruct>(); } else if (IsGenericMatch(dataType, typeof(KeyValuePair <,>))) { var args = GetGenericArguments(dataType); result = (TraceLoggingTypeInfo <DataType>)CreateInstance( typeof(KeyValuePairTypeInfo <,>).MakeGenericType(args[0], args[1]), recursionCheck); } else if (IsGenericMatch(dataType, typeof(Nullable <>))) { var args = GetGenericArguments(dataType); result = (TraceLoggingTypeInfo <DataType>)CreateInstance( typeof(NullableTypeInfo <>).MakeGenericType(args[0]), recursionCheck); } else { var elementType = FindEnumerableElementType(dataType); if (elementType != null) { result = (TraceLoggingTypeInfo <DataType>)CreateInstance( typeof(EnumerableTypeInfo <,>).MakeGenericType(dataType, elementType), GetTypeInfoInstance(elementType, recursionCheck)); } else { throw new ArgumentException(Environment.GetResourceString("EventSource_NonCompliantTypeError", dataType.Name)); } } return((TraceLoggingTypeInfo <DataType>)result); }
public static TraceLoggingTypeInfo CreateDefaultTypeInfo( Type dataType, List <Type> recursionCheck) { TraceLoggingTypeInfo result; if (recursionCheck.Contains(dataType)) { throw new NotSupportedException(SR.EventSource_RecursiveTypeDefinition); } recursionCheck.Add(dataType); var eventAttrib = Statics.GetCustomAttribute <EventDataAttribute>(dataType); if (eventAttrib != null || Statics.GetCustomAttribute <CompilerGeneratedAttribute>(dataType) != null || IsGenericMatch(dataType, typeof(KeyValuePair <,>))) { var analysis = new TypeAnalysis(dataType, eventAttrib, recursionCheck); result = new InvokeTypeInfo(dataType, analysis); } else if (dataType.IsArray) { var elementType = dataType.GetElementType(); if (elementType == typeof(bool)) { result = ScalarArrayTypeInfo.Boolean(); } else if (elementType == typeof(byte)) { result = ScalarArrayTypeInfo.Byte(); } else if (elementType == typeof(sbyte)) { result = ScalarArrayTypeInfo.SByte(); } else if (elementType == typeof(short)) { result = ScalarArrayTypeInfo.Int16(); } else if (elementType == typeof(ushort)) { result = ScalarArrayTypeInfo.UInt16(); } else if (elementType == typeof(int)) { result = ScalarArrayTypeInfo.Int32(); } else if (elementType == typeof(uint)) { result = ScalarArrayTypeInfo.UInt32(); } else if (elementType == typeof(long)) { result = ScalarArrayTypeInfo.Int64(); } else if (elementType == typeof(ulong)) { result = ScalarArrayTypeInfo.UInt64(); } else if (elementType == typeof(char)) { result = ScalarArrayTypeInfo.Char(); } else if (elementType == typeof(double)) { result = ScalarArrayTypeInfo.Double(); } else if (elementType == typeof(float)) { result = ScalarArrayTypeInfo.Single(); } else if (elementType == typeof(IntPtr)) { result = ScalarArrayTypeInfo.IntPtr(); } else if (elementType == typeof(UIntPtr)) { result = ScalarArrayTypeInfo.UIntPtr(); } else if (elementType == typeof(Guid)) { result = ScalarArrayTypeInfo.Guid(); } else { result = new ArrayTypeInfo(dataType, TraceLoggingTypeInfo.GetInstance(elementType, recursionCheck)); } } else { if (Statics.IsEnum(dataType)) { dataType = Enum.GetUnderlyingType(dataType); } if (dataType == typeof(string)) { result = new StringTypeInfo(); } else if (dataType == typeof(bool)) { result = ScalarTypeInfo.Boolean(); } else if (dataType == typeof(byte)) { result = ScalarTypeInfo.Byte(); } else if (dataType == typeof(sbyte)) { result = ScalarTypeInfo.SByte(); } else if (dataType == typeof(short)) { result = ScalarTypeInfo.Int16(); } else if (dataType == typeof(ushort)) { result = ScalarTypeInfo.UInt16(); } else if (dataType == typeof(int)) { result = ScalarTypeInfo.Int32(); } else if (dataType == typeof(uint)) { result = ScalarTypeInfo.UInt32(); } else if (dataType == typeof(long)) { result = ScalarTypeInfo.Int64(); } else if (dataType == typeof(ulong)) { result = ScalarTypeInfo.UInt64(); } else if (dataType == typeof(char)) { result = ScalarTypeInfo.Char(); } else if (dataType == typeof(double)) { result = ScalarTypeInfo.Double(); } else if (dataType == typeof(float)) { result = ScalarTypeInfo.Single(); } else if (dataType == typeof(DateTime)) { result = new DateTimeTypeInfo(); } else if (dataType == typeof(decimal)) { result = new DecimalTypeInfo(); } else if (dataType == typeof(IntPtr)) { result = ScalarTypeInfo.IntPtr(); } else if (dataType == typeof(UIntPtr)) { result = ScalarTypeInfo.UIntPtr(); } else if (dataType == typeof(Guid)) { result = ScalarTypeInfo.Guid(); } else if (dataType == typeof(TimeSpan)) { result = new TimeSpanTypeInfo(); } else if (dataType == typeof(DateTimeOffset)) { result = new DateTimeOffsetTypeInfo(); } else if (dataType == typeof(EmptyStruct)) { result = new NullTypeInfo(); } else if (IsGenericMatch(dataType, typeof(Nullable <>))) { result = new NullableTypeInfo(dataType, recursionCheck); } else { var elementType = FindEnumerableElementType(dataType); if (elementType != null) { result = new EnumerableTypeInfo(dataType, TraceLoggingTypeInfo.GetInstance(elementType, recursionCheck)); } else { throw new ArgumentException(SR.Format(SR.EventSource_NonCompliantTypeError, dataType.Name)); } } } return(result); }
public ParseStatus ParseTypeSymbol( ITypeSymbol typeSymbol, bool isReadonly, out ITypeInfo typeInfo) { this.ParseNullable_(ref typeSymbol, out var isNullable); var primitiveType = SchemaPrimitiveTypesUtil.GetPrimitiveTypeFromTypeSymbol( typeSymbol); if (primitiveType != SchemaPrimitiveType.UNDEFINED) { switch (primitiveType) { case SchemaPrimitiveType.BOOLEAN: { typeInfo = new BoolTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.BYTE: case SchemaPrimitiveType.SBYTE: case SchemaPrimitiveType.INT16: case SchemaPrimitiveType.UINT16: case SchemaPrimitiveType.INT32: case SchemaPrimitiveType.UINT32: case SchemaPrimitiveType.INT64: case SchemaPrimitiveType.UINT64: { typeInfo = new IntegerTypeInfo( typeSymbol, SchemaTypeKind.INTEGER, SchemaPrimitiveTypesUtil.ConvertNumberToInt( SchemaPrimitiveTypesUtil .ConvertPrimitiveToNumber(primitiveType)), isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.SN8: case SchemaPrimitiveType.UN8: case SchemaPrimitiveType.SN16: case SchemaPrimitiveType.UN16: case SchemaPrimitiveType.SINGLE: case SchemaPrimitiveType.DOUBLE: { typeInfo = new FloatTypeInfo( typeSymbol, SchemaTypeKind.FLOAT, SchemaPrimitiveTypesUtil .ConvertPrimitiveToNumber(primitiveType), isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.CHAR: { typeInfo = new CharTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.ENUM: { typeInfo = new EnumTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } default: throw new ArgumentOutOfRangeException(); } } if (typeSymbol.SpecialType == SpecialType.System_String) { typeInfo = new StringTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } if (typeSymbol.SpecialType is SpecialType .System_Collections_Generic_IReadOnlyList_T) { var listTypeSymbol = typeSymbol as INamedTypeSymbol; var containedTypeSymbol = listTypeSymbol.TypeArguments[0]; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, true, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, false, isReadonly, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol.TypeKind is TypeKind.Array) { var arrayTypeSymbol = typeSymbol as IArrayTypeSymbol; var containedTypeSymbol = arrayTypeSymbol.ElementType; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, false, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, true, isReadonly, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol.SpecialType is SpecialType .System_Collections_Generic_IList_T) { var listTypeSymbol = typeSymbol as INamedTypeSymbol; var containedTypeSymbol = listTypeSymbol.TypeArguments[0]; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, false, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, false, false, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol is INamedTypeSymbol namedTypeSymbol) { typeInfo = new StructureTypeInfo( namedTypeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } if (typeSymbol is ITypeParameterSymbol typeParameterSymbol) { var constraintTypeInfos = typeParameterSymbol .ConstraintTypes .Select(constraintType => { var parseStatus = this.ParseTypeSymbol( constraintType, isReadonly, out var constraintTypeInfo); Asserts.Equal(ParseStatus.SUCCESS, parseStatus); return(constraintTypeInfo); }) .ToArray(); typeInfo = new GenericTypeInfo( constraintTypeInfos, typeParameterSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } typeInfo = default; return(ParseStatus.NOT_IMPLEMENTED); }