Beispiel #1
0
 public static TypeDesc GetOwningType(this TypeSystemEntity entity)
 {
     return(entity switch
     {
         MethodDesc method => method.OwningType,
         FieldDesc field => field.OwningType,
         MetadataType type => type.ContainingType,
         PropertyPseudoDesc property => property.OwningType,
         EventPseudoDesc @event => @event.OwningType,
         _ => throw new NotImplementedException("Unexpected type system entity")
     });
        private static bool TryLogSingleWarning(Logger context, int code, MessageOrigin origin, string subcategory)
        {
            if (subcategory != MessageSubCategory.AotAnalysis && subcategory != MessageSubCategory.TrimAnalysis)
            {
                return(false);
            }

            var declaringType = origin.MemberDefinition switch
            {
                TypeDesc type => type,
                MethodDesc method => method.OwningType,
                FieldDesc field => field.OwningType,
#if !READYTORUN
                PropertyPseudoDesc property => property.OwningType,
                EventPseudoDesc @event => @event.OwningType,
#endif
                _ => null,
            };

            ModuleDesc declaringAssembly = (declaringType as MetadataType)?.Module;

            Debug.Assert(declaringAssembly != null);
            if (declaringAssembly == null)
            {
                return(false);
            }

            // Any IL2026 warnings left in an assembly with an IsTrimmable attribute are considered intentional
            // and should not be collapsed, so that the user-visible RUC message gets printed.
            if (code == 2026 && IsTrimmableAssembly(declaringAssembly))
            {
                return(false);
            }

            if (context.IsSingleWarn(declaringAssembly, subcategory))
            {
                return(true);
            }

            return(false);
        }
        public static IEnumerable <EventPseudoDesc> GetEventsOnTypeHierarchy(this TypeDesc type, Func <EventPseudoDesc, bool> filter, BindingFlags?bindingFlags = BindingFlags.Default)
        {
            bool onBaseType = false;

            if (type.IsArray)
            {
                type       = type.BaseType;
                onBaseType = true;
            }

            while (type != null)
            {
                if (type.GetTypeDefinition() is not EcmaType ecmaType)
                {
                    yield break;
                }

                foreach (var eventHandle in ecmaType.MetadataReader.GetTypeDefinition(ecmaType.Handle).GetEvents())
                {
                    var @event = new EventPseudoDesc(ecmaType, eventHandle);

                    // Ignore private properties on a base type - those are completely ignored by reflection
                    // (anything private on the base type is not visible via the derived type)
                    // Note that properties themselves are not actually private, their accessors are
                    if (onBaseType &&
                        (@event.AddMethod == null || @event.AddMethod.IsPrivate()) &&
                        (@event.RemoveMethod == null || @event.RemoveMethod.IsPrivate()))
                    {
                        continue;
                    }

                    if (filter != null && !filter(@event))
                    {
                        continue;
                    }

                    if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Static)
                    {
                        if ((@event.AddMethod != null) && [email protected])
                        {
                            continue;
                        }
                        if ((@event.RemoveMethod != null) && [email protected])
                        {
                            continue;
                        }
                    }

                    if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Instance)
                    {
                        if ((@event.AddMethod != null) && @event.AddMethod.Signature.IsStatic)
                        {
                            continue;
                        }
                        if ((@event.RemoveMethod != null) && @event.RemoveMethod.Signature.IsStatic)
                        {
                            continue;
                        }
                    }

                    if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.Public)
                    {
                        if ((@event.AddMethod == null || [email protected]()) &&
                            (@event.RemoveMethod == null || [email protected]()))
                        {
                            continue;
                        }
                    }

                    if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.NonPublic)
                    {
                        if ((@event.AddMethod != null) && @event.AddMethod.IsPublic())
                        {
                            continue;
                        }
                        if ((@event.RemoveMethod != null) && @event.RemoveMethod.IsPublic())
                        {
                            continue;
                        }
                    }

                    yield return(@event);
                }

                if ((bindingFlags & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly)
                {
                    yield break;
                }

                type       = type.TryGetBaseType();
                onBaseType = true;
            }
        }