Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 28 additions & 35 deletions Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2007-2020 Xtensive LLC.
// Copyright (C) 2007-2021 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Dmitri Maximov
Expand Down Expand Up @@ -34,14 +34,9 @@ public sealed class TypeInfoCollection
/// An indexer that provides access to collection items.
/// </summary>
/// <exception cref="ArgumentException">Item was not found.</exception>
public TypeInfo this[Type key] {
get {
TypeInfo result;
if (!TryGetValue(key, out result))
throw new KeyNotFoundException(string.Format(Strings.TypeXIsNotRegistered, key.GetShortName()));
return result;
}
}
public TypeInfo this[Type key] => TryGetValue(key, out var result)
? result
: throw new KeyNotFoundException(string.Format(Strings.TypeXIsNotRegistered, key.GetShortName()));

/// <summary>
/// An indexer that provides access to collection items by their <see cref="TypeInfo.TypeId"/>.
Expand Down Expand Up @@ -217,15 +212,13 @@ public IEnumerable<TypeInfo> FindDescendants(TypeInfo item, bool recursive)
{
ArgumentValidator.EnsureArgumentNotNull(item, "item");

HashSet<TypeInfo> result;
if (!descendantTable.TryGetValue(item, out result))
result = new HashSet<TypeInfo>();

foreach (var item1 in result) {
yield return item1;
if (recursive)
foreach (var item2 in FindDescendants(item1, true))
yield return item2;
if (descendantTable.TryGetValue(item, out var result)) {
foreach (var item1 in result) {
yield return item1;
if (recursive)
foreach (var item2 in FindDescendants(item1, true))
yield return item2;
}
}
}

Expand All @@ -251,12 +244,10 @@ public IEnumerable<TypeInfo> FindInterfaces(TypeInfo item, bool recursive)
{
ArgumentValidator.EnsureArgumentNotNull(item, "item");

HashSet<TypeInfo> result;
if (!interfaceTable.TryGetValue(item, out result))
result = new HashSet<TypeInfo>();

foreach (var item1 in result)
yield return item1;
if (interfaceTable.TryGetValue(item, out var result)) {
foreach (var item1 in result)
yield return item1;
}

if (!recursive || item.IsInterface)
yield break;
Expand Down Expand Up @@ -293,15 +284,13 @@ public IEnumerable<TypeInfo> FindImplementors(TypeInfo item, bool recursive)
{
ArgumentValidator.EnsureArgumentNotNull(item, "item");

HashSet<TypeInfo> result;
if (!implementorTable.TryGetValue(item, out result))
result = new HashSet<TypeInfo>();

foreach (var item1 in result) {
yield return item1;
if (recursive && !item1.IsInterface)
foreach (var item2 in FindDescendants(item1, true))
yield return item2;
if (implementorTable.TryGetValue(item, out var result)) {
foreach (var item1 in result) {
yield return item1;
if (recursive && !item1.IsInterface)
foreach (var item2 in FindDescendants(item1, true))
yield return item2;
}
}
}

Expand Down Expand Up @@ -336,9 +325,13 @@ public TypeInfo FindRoot(TypeInfo item)
/// <exception cref="ArgumentNullException">When <paramref name="type"/> is <see langword="null"/>.</exception>
private TypeInfo FindAncestor(Type type)
{
if (type == WellKnownTypes.Object || type.BaseType == null)
if (type == WellKnownTypes.Object) {
return null;
return Contains(type.BaseType) ? this[type.BaseType] : FindAncestor(type.BaseType);
}
return type.BaseType switch {
null => null,
var baseType => TryGetValue(baseType, out var typeInfo) ? typeInfo : FindAncestor(baseType)
};
}

#endregion
Expand Down