public TypeAnalysis(Type dataType, EventDataAttribute eventAttrib, List <Type> recursionCheck) { IEnumerable <PropertyInfo> properties = Statics.GetProperties(dataType); List <PropertyAnalysis> propertyAnalysisList = new List <PropertyAnalysis>(); foreach (PropertyInfo propInfo in properties) { if (!Statics.HasCustomAttribute(propInfo, typeof(EventIgnoreAttribute)) && propInfo.CanRead && propInfo.GetIndexParameters().Length == 0) { MethodInfo getMethod = Statics.GetGetMethod(propInfo); if (!(getMethod == (MethodInfo)null) && !getMethod.IsStatic && getMethod.IsPublic) { TraceLoggingTypeInfo typeInfoInstance = Statics.GetTypeInfoInstance(propInfo.PropertyType, recursionCheck); EventFieldAttribute customAttribute = Statics.GetCustomAttribute <EventFieldAttribute>(propInfo); string name = customAttribute == null || customAttribute.Name == null ? (Statics.ShouldOverrideFieldName(propInfo.Name) ? typeInfoInstance.Name : propInfo.Name) : customAttribute.Name; propertyAnalysisList.Add(new PropertyAnalysis(name, getMethod, typeInfoInstance, customAttribute)); } } } this.properties = propertyAnalysisList.ToArray(); foreach (PropertyAnalysis property in this.properties) { TraceLoggingTypeInfo traceLoggingTypeInfo = property.typeInfo; this.level = (EventLevel)Statics.Combine((int)traceLoggingTypeInfo.Level, (int)this.level); this.opcode = (EventOpcode)Statics.Combine((int)traceLoggingTypeInfo.Opcode, (int)this.opcode); this.keywords = this.keywords | traceLoggingTypeInfo.Keywords; this.tags = this.tags | traceLoggingTypeInfo.Tags; } if (eventAttrib != null) { this.level = (EventLevel)Statics.Combine((int)eventAttrib.Level, (int)this.level); this.opcode = (EventOpcode)Statics.Combine((int)eventAttrib.Opcode, (int)this.opcode); this.keywords = this.keywords | eventAttrib.Keywords; this.tags = this.tags | eventAttrib.Tags; this.name = eventAttrib.Name; } if (this.name != null) { return; } this.name = dataType.Name; }
public TypeAnalysis( Type dataType, EventDataAttribute?eventAttrib, List <Type> recursionCheck) { var propertyList = new List <PropertyAnalysis>(); foreach (PropertyInfo propertyInfo in dataType.GetProperties()) { if (Statics.HasCustomAttribute(propertyInfo, typeof(EventIgnoreAttribute))) { continue; } if (!propertyInfo.CanRead || propertyInfo.GetIndexParameters().Length != 0) { continue; } MethodInfo?getterInfo = propertyInfo.GetGetMethod(); if (getterInfo == null) { continue; } if (getterInfo.IsStatic || !getterInfo.IsPublic) { continue; } Type propertyType = propertyInfo.PropertyType; var propertyTypeInfo = TraceLoggingTypeInfo.GetInstance(propertyType, recursionCheck); EventFieldAttribute?fieldAttribute = Statics.GetCustomAttribute <EventFieldAttribute>(propertyInfo); string propertyName = fieldAttribute != null && fieldAttribute.Name != null ? fieldAttribute.Name : Statics.ShouldOverrideFieldName(propertyInfo.Name) ? propertyTypeInfo.Name : propertyInfo.Name; propertyList.Add(new PropertyAnalysis( propertyName, propertyInfo, propertyTypeInfo, fieldAttribute)); } this.properties = propertyList.ToArray(); foreach (PropertyAnalysis property in this.properties) { TraceLoggingTypeInfo typeInfo = property.typeInfo; this.level = (EventLevel)Statics.Combine((int)typeInfo.Level, (int)this.level); this.opcode = (EventOpcode)Statics.Combine((int)typeInfo.Opcode, (int)this.opcode); this.keywords |= typeInfo.Keywords; this.tags |= typeInfo.Tags; } if (eventAttrib != null) { this.level = (EventLevel)Statics.Combine((int)eventAttrib.Level, (int)this.level); this.opcode = (EventOpcode)Statics.Combine((int)eventAttrib.Opcode, (int)this.opcode); this.keywords |= eventAttrib.Keywords; this.tags |= eventAttrib.Tags; this.name = eventAttrib.Name; } this.name ??= dataType.Name; }
public static TraceLoggingTypeInfo <DataType> CreateDefaultTypeInfo <DataType>(List <Type> recursionCheck) { Type type = typeof(DataType); if (recursionCheck.Contains(type)) { throw new NotSupportedException(Environment.GetResourceString("EventSource_RecursiveTypeDefinition")); } recursionCheck.Add(type); EventDataAttribute customAttribute = Statics.GetCustomAttribute <EventDataAttribute>(type); TraceLoggingTypeInfo traceLoggingTypeInfo; if (customAttribute != null || Statics.GetCustomAttribute <CompilerGeneratedAttribute>(type) != null) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new InvokeTypeInfo <DataType>(new TypeAnalysis(type, customAttribute, recursionCheck)); } else if (type.IsArray) { Type elementType = type.GetElementType(); if (elementType == typeof(bool)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new BooleanArrayTypeInfo(); } else if (elementType == typeof(byte)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new ByteArrayTypeInfo(); } else if (elementType == typeof(sbyte)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new SByteArrayTypeInfo(); } else if (elementType == typeof(short)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new Int16ArrayTypeInfo(); } else if (elementType == typeof(ushort)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new UInt16ArrayTypeInfo(); } else if (elementType == typeof(int)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new Int32ArrayTypeInfo(); } else if (elementType == typeof(uint)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new UInt32ArrayTypeInfo(); } else if (elementType == typeof(long)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new Int64ArrayTypeInfo(); } else if (elementType == typeof(ulong)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new UInt64ArrayTypeInfo(); } else if (elementType == typeof(char)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new CharArrayTypeInfo(); } else if (elementType == typeof(double)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new DoubleArrayTypeInfo(); } else if (elementType == typeof(float)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new SingleArrayTypeInfo(); } else if (elementType == typeof(IntPtr)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new IntPtrArrayTypeInfo(); } else if (elementType == typeof(UIntPtr)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new UIntPtrArrayTypeInfo(); } else if (elementType == typeof(Guid)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new GuidArrayTypeInfo(); } else { traceLoggingTypeInfo = (TraceLoggingTypeInfo)Statics.CreateInstance(typeof(ArrayTypeInfo <>).MakeGenericType(elementType), (object)Statics.GetTypeInfoInstance(elementType, recursionCheck)); } } else if (Statics.IsEnum(type)) { Type underlyingType = Enum.GetUnderlyingType(type); if (underlyingType == typeof(int)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new EnumInt32TypeInfo <DataType>(); } else if (underlyingType == typeof(uint)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new EnumUInt32TypeInfo <DataType>(); } else if (underlyingType == typeof(byte)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new EnumByteTypeInfo <DataType>(); } else if (underlyingType == typeof(sbyte)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new EnumSByteTypeInfo <DataType>(); } else if (underlyingType == typeof(short)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new EnumInt16TypeInfo <DataType>(); } else if (underlyingType == typeof(ushort)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new EnumUInt16TypeInfo <DataType>(); } else if (underlyingType == typeof(long)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new EnumInt64TypeInfo <DataType>(); } else if (underlyingType == typeof(ulong)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new EnumUInt64TypeInfo <DataType>(); } else { throw new NotSupportedException(Environment.GetResourceString("EventSource_NotSupportedEnumType", (object)type.Name, (object)underlyingType.Name)); } } else if (type == typeof(string)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new StringTypeInfo(); } else if (type == typeof(bool)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new BooleanTypeInfo(); } else if (type == typeof(byte)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new ByteTypeInfo(); } else if (type == typeof(sbyte)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new SByteTypeInfo(); } else if (type == typeof(short)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new Int16TypeInfo(); } else if (type == typeof(ushort)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new UInt16TypeInfo(); } else if (type == typeof(int)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new Int32TypeInfo(); } else if (type == typeof(uint)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new UInt32TypeInfo(); } else if (type == typeof(long)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new Int64TypeInfo(); } else if (type == typeof(ulong)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new UInt64TypeInfo(); } else if (type == typeof(char)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new CharTypeInfo(); } else if (type == typeof(double)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new DoubleTypeInfo(); } else if (type == typeof(float)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new SingleTypeInfo(); } else if (type == typeof(DateTime)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new DateTimeTypeInfo(); } else if (type == typeof(Decimal)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new DecimalTypeInfo(); } else if (type == typeof(IntPtr)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new IntPtrTypeInfo(); } else if (type == typeof(UIntPtr)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new UIntPtrTypeInfo(); } else if (type == typeof(Guid)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new GuidTypeInfo(); } else if (type == typeof(TimeSpan)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new TimeSpanTypeInfo(); } else if (type == typeof(DateTimeOffset)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new DateTimeOffsetTypeInfo(); } else if (type == typeof(EmptyStruct)) { traceLoggingTypeInfo = (TraceLoggingTypeInfo) new NullTypeInfo <EmptyStruct>(); } else if (Statics.IsGenericMatch(type, (object)typeof(KeyValuePair <,>))) { Type[] genericArguments = Statics.GetGenericArguments(type); traceLoggingTypeInfo = (TraceLoggingTypeInfo)Statics.CreateInstance(typeof(KeyValuePairTypeInfo <,>).MakeGenericType(genericArguments[0], genericArguments[1]), (object)recursionCheck); } else if (Statics.IsGenericMatch(type, (object)typeof(Nullable <>))) { traceLoggingTypeInfo = (TraceLoggingTypeInfo)Statics.CreateInstance(typeof(NullableTypeInfo <>).MakeGenericType(Statics.GetGenericArguments(type)[0]), (object)recursionCheck); } else { Type enumerableElementType = Statics.FindEnumerableElementType(type); if (enumerableElementType != (Type)null) { traceLoggingTypeInfo = (TraceLoggingTypeInfo)Statics.CreateInstance(typeof(EnumerableTypeInfo <,>).MakeGenericType(type, enumerableElementType), (object)Statics.GetTypeInfoInstance(enumerableElementType, recursionCheck)); } else { throw new ArgumentException(Environment.GetResourceString("EventSource_NonCompliantTypeError", (object)type.Name)); } } return((TraceLoggingTypeInfo <DataType>)traceLoggingTypeInfo); }