Esempio n. 1
0
        public void Register(Type type)
        {
            if (!typeof(IIdentifiedComponent).IsAssignableFrom(type) || type.GetConstructor(Type.EmptyTypes) == null || !type.GetTypeInfo().IsClass)
            {
                return;
            }

            IReadOnlyList <Type> registerFor = type.GetTypeInfo().ImplementedInterfaces.Where(x => x != typeof(IIdentifiedComponent) && typeof(IIdentifiedComponent).IsAssignableFrom(x)).ToList();

            if (registerFor.Count == 0)
            {
                return;
            }

            IIdentifiedComponent instance = (IIdentifiedComponent)Activator.CreateInstance(type);

            foreach (Type t in registerFor)
            {
                FieldInfo instanceField = typeof(Cache <>).MakeGenericType(t).GetField("Instance", BindingFlags.Public | BindingFlags.Static);
                ICache    cache         = (ICache)instanceField.GetValue(null);
                cache.AddPart(instance);
                _componentIdToAssemblyQualifiedTypeName[instance.Id] = type.AssemblyQualifiedName;
                _settings.ComponentGuidToAssemblyQualifiedName[instance.Id.ToString()] = type.AssemblyQualifiedName;

                if (!_componentIdsByType.TryGetValue(t, out HashSet <Guid> ids))
                {
                    _componentIdsByType[t] = ids = new HashSet <Guid>();
                }

                ids.Add(instance.Id);

                if (!_settings.ComponentTypeToGuidList.TryGetValue(t.FullName, out ids))
                {
                    _settings.ComponentTypeToGuidList[t.FullName] = ids = new HashSet <Guid>();
                }

                ids.Add(instance.Id);
            }

            bool      successfulWrite = false;
            const int maxAttempts     = 10;
            int       attemptCount    = 0;

            while (!successfulWrite && attemptCount++ < maxAttempts)
            {
                try
                {
                    _loader.Save();
                    successfulWrite = true;
                }
                catch (IOException)
                {
                    Thread.Sleep(10);
                }
            }
        }
        private void Save()
        {
            var flag = false;
            var num  = 0;

            while (!flag && num++ < 10)
            {
                try
                {
                    _loader.Save();
                    flag = true;
                }
                catch (IOException)
                {
                    Task.Delay(10).Wait();
                }
            }
        }
        private void Save()
        {
            bool      successfulWrite = false;
            const int maxAttempts     = 10;
            int       attemptCount    = 0;

            while (!successfulWrite && attemptCount++ < maxAttempts)
            {
                try
                {
                    _loader.Save();
                    successfulWrite = true;
                }
                catch (IOException)
                {
                    Task.Delay(10).Wait();
                }
            }
        }