コード例 #1
0
        public override bool TryGetValue(Type type, out SyncWorkerEntry syncWorkerEntry)
        {
            explicitEntries.TryGetValue(type, out syncWorkerEntry);

            if (syncWorkerEntry != null)
            {
                return(true);
            }

            foreach (var e in implicitEntries)
            {
                syncWorkerEntry = e.GetClosest(type);

                if (syncWorkerEntry != null)
                {
                    return(true);
                }
            }

            foreach (var e in interfaceEntries)
            {
                syncWorkerEntry = e.GetClosest(type);

                if (syncWorkerEntry != null)
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #2
0
 public SyncWorkerEntry(SyncWorkerEntry other)
 {
     type            = other.type;
     syncWorkers     = other.syncWorkers;
     subclasses      = other.subclasses;
     shouldConstruct = other.shouldConstruct;
 }
コード例 #3
0
        public SyncWorkerEntry GetClosest(Type type)
        {
            if (this.type.IsAssignableFrom(type))
            {
                if (subclasses == null)
                {
                    return(this);
                }

                int len = subclasses.Count;

                if (len == 0)
                {
                    return(this);
                }

                for (int i = 0; i < len; i++)
                {
                    SyncWorkerEntry res = subclasses[i].GetClosest(type);

                    if (res != null)
                    {
                        return(res);
                    }
                }

                return(this);
            }

            return(null);
        }
コード例 #4
0
        public SyncWorkerEntry Add(SyncWorkerEntry other)
        {
            SyncWorkerEntry newEntry = Add(other.type, other, other.shouldConstruct);

            newEntry.subclasses = other.subclasses;

            return(newEntry);
        }
コード例 #5
0
        public SyncWorkerEntry GetOrAddEntry(Type type, bool isImplicit = false, bool shouldConstruct = false)
        {
            if (explicitEntries.TryGetValue(type, out SyncWorkerEntry explicitEntry))
            {
                return(explicitEntry);
            }

            if (!isImplicit)
            {
                return(AddExplicit(type, shouldConstruct));
            }

            if (type.IsInterface)
            {
                var interfaceEntry = interfaceEntries.FirstOrDefault(i => i.type == type);

                if (interfaceEntry == null)
                {
                    interfaceEntry = new SyncWorkerEntry(type, shouldConstruct);

                    interfaceEntries.Add(interfaceEntry);
                }

                return(interfaceEntry);
            }

            var entry = implicitEntries.FirstOrDefault(i => i.type == type);

            Stack <SyncWorkerEntry> toRemove = new Stack <SyncWorkerEntry>();

            foreach (var e in implicitEntries)
            {
                if (type.IsAssignableFrom(e.type) || e.type.IsAssignableFrom(type))
                {
                    if (entry != null)
                    {
                        entry.Add(e);
                        toRemove.Push(e);
                        continue;
                    }
                    entry = e.Add(type, shouldConstruct);
                }
            }

            if (entry == null)
            {
                entry = new SyncWorkerEntry(type, shouldConstruct);
                implicitEntries.Add(entry);
                return(entry);
            }

            foreach (var e in toRemove)
            {
                implicitEntries.Remove(e);
            }

            return(entry);
        }
コード例 #6
0
        private SyncWorkerEntry Add(Type type, SyncWorkerEntry parent, bool shouldConstruct)
        {
            if (type == this.type) {
                if (shouldConstruct) {
                    this.shouldConstruct = true;
                }

                return this;
            }

            if (type.IsAssignableFrom(this.type))   // Is parent
            {
                SyncWorkerEntry newEntry;

                if (parent != null) {
                    List<SyncWorkerEntry> ps = parent.subclasses;
                    newEntry = new SyncWorkerEntry(type, shouldConstruct);

                    newEntry.subclasses.Add(this);

                    ps[ps.IndexOf(this)] = newEntry;
                    return newEntry;
                } else {
                    newEntry = new SyncWorkerEntry(this);

                    this.type = type;

                    this.shouldConstruct = shouldConstruct;

                    syncWorkers = new List<SyncWorkerDelegate>();
                    subclasses = new List<SyncWorkerEntry>() { newEntry };
                    return this;
                }


            }

            if (this.type.IsAssignableFrom(type)) // Is child
            {
                if (subclasses != null) {
                    for (int i = 0; i < subclasses.Count; i++) {
                        SyncWorkerEntry res = subclasses[i].Add(type, this, shouldConstruct);
                        if (res != null)
                            return res;
                    }
                } else {
                    subclasses = new List<SyncWorkerEntry>();
                }

                var newEntry = new SyncWorkerEntry(type, shouldConstruct);
                newEntry.parent = this;
                subclasses.Add(newEntry);

                return newEntry;
            }

            return null;
        }