Zetbox.API.InterfaceType.GetRootType C# (CSharp) Method

GetRootType() public method

Returns the root of the specified InterfaceType's data model. The root is the top-most interface in this interface's parentage that inherits only from one of the base interfaces. Interfaces that do not inherit from one of the base interfaces are excluded from all considerations.
public GetRootType ( ) : InterfaceType
return InterfaceType
        public InterfaceType GetRootType()
        {
            lock (_cacheLock)
            {
                if (_rootTypeCache.ContainsKey(this)) return _rootTypeCache[this];

                var self = this.Type.IsGenericType ? this.Type.GetGenericTypeDefinition() : this.Type;
                // the base of the interface we're looking for
                var baseInterface = BaseInterfaces.Where(t => t.IsAssignableFrom(self)).First();
                var allInherited = GetInterestingInterfaces(baseInterface, this.Type);
                var candidates = allInherited
                    .Select(intf => new { Interface = intf, Inherited = GetInterestingInterfaces(baseInterface, intf) })
                    .ToList();
                candidates.Add(new { Interface = this.Type, Inherited = allInherited });

                return _rootTypeCache[this] = Create(candidates.OrderBy(i => i.Inherited.Length).First().Interface, _typeChecker);
            }
        }