public Boolean SupportsType(TypeInfo typeInfo)
        {
            if (typeInfo == null)
            {
                throw Logger.Fatal.ArgumentNull(nameof(typeInfo));
            }

            if (typeInfo.IsNullable())
            {
                return(true);
            }

            if (GuidTypeInfo.Equals(typeInfo))
            {
                return(true);
            }

            if (IEnumerableTypeInfo.IsAssignableFrom(typeInfo))
            {
                return(true);
            }

            return(false);
        }
Example #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);
        }