Esempio n. 1
0
    protected static IEnumerable <FieldInfo> GetFieldsWithAttribute <T>()
        where T : Attribute
    {
    #if !UNITY_EDITOR || !UNITY_2020_1_OR_NEWER
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            foreach (var type in assembly.GetTypes())
            {
                foreach (var field in type.GetFields(AllMemberFlags))
                {
                    var attribute = field.GetCustomAttribute <T>();

                    if (attribute == null)
                    {
                        continue;
                    }

                    yield return(field);
                }
            }
        }
    #else
        return(TypeCache.GetFieldsWithAttribute <T>());
    #endif
    }
        protected void Cache <ItemType, Attr>()
            where ItemType : MemberInfo
            where Attr : Attribute
        {
            Type iType = typeof(ItemType);

            Debug.Assert(typeof(FieldInfo).IsAssignableFrom(iType) ||
                         typeof(MethodInfo).IsAssignableFrom(iType));

            if (typeof(FieldInfo).IsAssignableFrom(iType))
            {
                Cache <Attr>(
                    TypeCache.
                    GetFieldsWithAttribute <Attr>().
                    ToList()
                    );
            }
            else if (typeof(MethodInfo).IsAssignableFrom(iType))
            {
                Cache <Attr>(
                    TypeCache.
                    GetMethodsWithAttribute <Attr>().
                    ToList()
                    );
            }
        }
Esempio n. 3
0
        static IEnumerable <FieldInfo> GetFieldsWithAttribute <T>()
            where T : Attribute
        {
        #if UNITY_2020_1_OR_NEWER
            return(TypeCache.GetFieldsWithAttribute <T>());
        #else
            const BindingFlags AllMemberFlags = 0
                                                | BindingFlags.Public | BindingFlags.NonPublic
                                                | BindingFlags.Static | BindingFlags.Instance;

            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type     in assembly.GetTypes())
                {
                    foreach (var field    in type.GetFields(AllMemberFlags))
                    {
                        var attribute = GetCustomAttribute <T>(field);

                        if (attribute == null)
                        {
                            continue;
                        }

                        yield return(field);
                    }
                }
            }
        #endif
        }
Esempio n. 4
0
        static void OnSubsystemRegistration()
        {
            foreach (FieldInfo field in TypeCache.GetFieldsWithAttribute <DefaultInitializeOnLoadAttribute>())
            {
                // Only static fields can be marked with [DefaultInitializeOnLoad]. There is unfortunately no way to
                // specify that the compiler should validate this statically, so should we display an error for usages
                // of the attribute on non-static fields?

                if (field.IsStatic)
                {
                    if (field.FieldType.IsValueType)
                    {
                        field.SetValue(null, Activator.CreateInstance(field.FieldType));
                    }
                    else
                    {
                        field.SetValue(null, null);
                    }
                }
            }
        }