示例#1
0
文件: Statics.cs 项目: KotovN/coreclr
        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);
        }
示例#2
0
        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);
        }