Ejemplo n.º 1
0
 static void loadOrReset(bool reset)
 {
     lock (settingsFieldFullNames2SettingsFieldInfo)
     {
         set_settingsFieldFullNames2SettingsFieldInfo();
         IEnumerable <SettingsFieldInfo> settingsFieldInfos = settingsFieldFullNames2SettingsFieldInfo.Values;
         if (InitializationOrderedSettingsTypes != null)
         {
             SettingsTypeComparer settingsTypeComparer = new SettingsTypeComparer(InitializationOrderedSettingsTypes);
             settingsFieldInfos = settingsFieldInfos.OrderBy(a => a.Type, settingsTypeComparer);
         }
         HashSet <string> requiredOptionalFieldFullNames = RequiredOptionalFieldFullNames == null ? null : new HashSet <string>(RequiredOptionalFieldFullNames);
         foreach (SettingsFieldInfo settingsFieldInfo in settingsFieldInfos)
         {
             if (!settingsFieldInfo.Optional /*|| RequiredOptionalSettingsTypes?.Contains(settingsFieldInfo.Type) == true*/ || requiredOptionalFieldFullNames?.Remove(settingsFieldInfo.FullName) == true)
             {
                 settingsFieldInfo.SetObject(Settings.Create(settingsFieldInfo, reset));
             }
         }
         if (requiredOptionalFieldFullNames?.Count > 0)
         {
             throw new Exception("RequiredOptionalFieldFullNames contains name which was not found: '" + RequiredOptionalFieldFullNames[0] + "'");
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Enumerates through all the Settings fields available in the application.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <SettingsFieldInfo> EnumSettingsFieldInfos()
        {
            Assembly   configAssembly  = Assembly.GetExecutingAssembly();
            StackTrace stackTrace      = new StackTrace();
            Assembly   callingAssembly = stackTrace.GetFrames().Select(f => f.GetMethod().DeclaringType.Assembly).Where(a => a != configAssembly).FirstOrDefault();

            if (callingAssembly == null)
            {
                callingAssembly = Assembly.GetEntryAssembly();
            }
            List <Assembly> assemblies = new List <Assembly> {
                callingAssembly
            };
            string configAssemblyFullName = configAssembly.FullName;

            foreach (AssemblyName assemblyName in callingAssembly.GetReferencedAssemblies())
            {
                Assembly a = Assembly.Load(assemblyName);
                if (null != a.GetReferencedAssemblies().Where(an => an.FullName == configAssemblyFullName).FirstOrDefault())
                {
                    assemblies.Add(a);
                }
            }
            foreach (Assembly assembly in assemblies)
            {
                Type[]             types         = assembly.GetTypes();
                IEnumerable <Type> settingsTypes = types.Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(Settings))).Distinct();
                if (InitialzingOrderedSettingsTypes != null)
                {
                    SettingsTypeComparer settingsTypeComparer = new SettingsTypeComparer(InitialzingOrderedSettingsTypes);
                    settingsTypes = settingsTypes.OrderBy(t => t, settingsTypeComparer);
                }
                foreach (Type type in types)
                {
                    foreach (FieldInfo settingsTypeFieldInfo in type.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).Where(f => settingsTypes.Contains(f.FieldType) /* && f.FieldType.IsAssignableFrom(settingsType)*/))
                    {
                        yield return(new SettingsFieldInfo(settingsTypeFieldInfo));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Enumerates through all the Settings fields available in the application.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <SettingsMemberInfo> EnumSettingsFieldInfos()
        {
            List <Assembly> assemblies;

            if (ExplicitlyTrackedAssemblies != null)
            {
                assemblies = new List <Assembly>(ExplicitlyTrackedAssemblies);
            }
            else
            {
                Assembly   configAssembly  = Assembly.GetExecutingAssembly();
                StackTrace stackTrace      = new StackTrace();
                Assembly   callingAssembly = stackTrace.GetFrames().Select(f => f.GetMethod().DeclaringType.Assembly).Where(a => a != configAssembly).FirstOrDefault();
                if (callingAssembly == null)
                {
                    callingAssembly = Assembly.GetEntryAssembly();
                }
                assemblies = new List <Assembly> {
                    callingAssembly
                };
                string configAssemblyFullName = configAssembly.FullName;
                //!!!The search scope is limited as it is in order not to load more assemblies which is unavoidable while enumerating them.
                foreach (AssemblyName assemblyName in callingAssembly.GetReferencedAssemblies())
                {
                    Assembly a = Assembly.Load(assemblyName);
                    if (null != a.GetReferencedAssemblies().Where(an => an.FullName == configAssemblyFullName).FirstOrDefault())
                    {
                        assemblies.Add(a);
                    }
                }
            }
            assemblies.RemoveAll(a => a == null);
            assemblies.Distinct();
            foreach (Assembly assembly in assemblies)
            {
                Type[]             types         = assembly.GetTypes();
                IEnumerable <Type> settingsTypes = types.Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(Settings))).Distinct();
                if (InitializationOrderedSettingsTypes != null)
                {
                    SettingsTypeComparer settingsTypeComparer = new SettingsTypeComparer(InitializationOrderedSettingsTypes);
                    settingsTypes = settingsTypes.OrderBy(t => t, settingsTypeComparer);
                }
                foreach (Type type in types)
                {
                    //!!!while FieldInfo can see property, it loses its attributes if any. So we need to retrieve by GetMembers().
                    foreach (MemberInfo settingsTypeMemberInfo in type.GetMembers(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public) /*.Where(f => settingsTypes.Contains(f.Member)*/ /* && f.FieldType.IsAssignableFrom(settingsType)*/)
                    {
                        FieldInfo fi = settingsTypeMemberInfo as FieldInfo;
                        if (fi != null)
                        {
                            if (!fi.Name.StartsWith("<") /*it is a compiled property*/ && settingsTypes.Contains(fi.FieldType))
                            {
                                yield return(new SettingsFieldInfo(fi));
                            }
                        }
                        else
                        {
                            PropertyInfo pi = settingsTypeMemberInfo as PropertyInfo;
                            if (pi != null && settingsTypes.Contains(pi.PropertyType))
                            {
                                yield return(new SettingsPropertyInfo(pi));
                            }
                        }
                    }
                }
            }
        }