Browse Source

Return IReadOnlyList from most of the AvaloniaPropertyRegistry calls so users are not forced into allocating enumerators.

pull/3669/head
Dariusz Komosinski 7 years ago
parent
commit
dc93d14a1b
  1. 6
      src/Avalonia.Base/AvaloniaObject.cs
  2. 60
      src/Avalonia.Base/AvaloniaPropertyRegistry.cs

6
src/Avalonia.Base/AvaloniaObject.cs

@ -80,8 +80,12 @@ namespace Avalonia
_inheritanceParent?.RemoveInheritanceChild(this);
_inheritanceParent = value;
foreach (var property in AvaloniaPropertyRegistry.Instance.GetRegisteredInherited(GetType()))
var properties = AvaloniaPropertyRegistry.Instance.GetRegisteredInherited(GetType());
var propertiesCount = properties.Count;
for (var i = 0; i < propertiesCount; i++)
{
var property = properties[i];
if (valuestore?.IsSet(property) == true)
{
// If local value set there can be no change.

60
src/Avalonia.Base/AvaloniaPropertyRegistry.cs

@ -3,9 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia.Data;
namespace Avalonia
{
@ -49,7 +47,7 @@ namespace Avalonia
/// </summary>
/// <param name="type">The type.</param>
/// <returns>A collection of <see cref="AvaloniaProperty"/> definitions.</returns>
public IEnumerable<AvaloniaProperty> GetRegistered(Type type)
public IReadOnlyList<AvaloniaProperty> GetRegistered(Type type)
{
Contract.Requires<ArgumentNullException>(type != null);
@ -83,7 +81,7 @@ namespace Avalonia
/// </summary>
/// <param name="type">The type.</param>
/// <returns>A collection of <see cref="AvaloniaProperty"/> definitions.</returns>
public IEnumerable<AvaloniaProperty> GetRegisteredAttached(Type type)
public IReadOnlyList<AvaloniaProperty> GetRegisteredAttached(Type type)
{
Contract.Requires<ArgumentNullException>(type != null);
@ -114,7 +112,7 @@ namespace Avalonia
/// </summary>
/// <param name="type">The type.</param>
/// <returns>A collection of <see cref="AvaloniaProperty"/> definitions.</returns>
public IEnumerable<AvaloniaProperty> GetRegisteredDirect(Type type)
public IReadOnlyList<AvaloniaProperty> GetRegisteredDirect(Type type)
{
Contract.Requires<ArgumentNullException>(type != null);
@ -145,7 +143,7 @@ namespace Avalonia
/// </summary>
/// <param name="type">The type.</param>
/// <returns>A collection of <see cref="AvaloniaProperty"/> definitions.</returns>
public IEnumerable<AvaloniaProperty> GetRegisteredInherited(Type type)
public IReadOnlyList<AvaloniaProperty> GetRegisteredInherited(Type type)
{
Contract.Requires<ArgumentNullException>(type != null);
@ -157,16 +155,27 @@ namespace Avalonia
result = new List<AvaloniaProperty>();
var visited = new HashSet<AvaloniaProperty>();
foreach (var property in GetRegistered(type))
var registered = GetRegistered(type);
var registeredCount = registered.Count;
for (var i = 0; i < registeredCount; i++)
{
var property = registered[i];
if (property.Inherits)
{
result.Add(property);
visited.Add(property);
}
}
foreach (var property in GetRegisteredAttached(type))
var registeredAttached = GetRegisteredAttached(type);
var registeredAttachedCount = registeredAttached.Count;
for (var i = 0; i < registeredAttachedCount; i++)
{
var property = registeredAttached[i];
if (property.Inherits)
{
if (!visited.Contains(property))
@ -185,7 +194,7 @@ namespace Avalonia
/// </summary>
/// <param name="o">The object.</param>
/// <returns>A collection of <see cref="AvaloniaProperty"/> definitions.</returns>
public IEnumerable<AvaloniaProperty> GetRegistered(IAvaloniaObject o)
public IReadOnlyList<AvaloniaProperty> GetRegistered(IAvaloniaObject o)
{
Contract.Requires<ArgumentNullException>(o != null);
@ -229,8 +238,13 @@ namespace Avalonia
throw new InvalidOperationException("Attached properties not supported.");
}
foreach (AvaloniaProperty x in GetRegistered(type))
var registered = GetRegistered(type);
var registeredCount = registered.Count;
for (var i = 0; i < registeredCount; i++)
{
AvaloniaProperty x = registered[i];
if (x.Name == name)
{
return x;
@ -276,8 +290,13 @@ namespace Avalonia
return property;
}
foreach (var p in GetRegisteredDirect(o.GetType()))
var registeredDirect = GetRegisteredDirect(o.GetType());
var registeredDirectCount = registeredDirect.Count;
for (var i = 0; i < registeredDirectCount; i++)
{
var p = registeredDirect[i];
if (p == property)
{
return (DirectPropertyBase<T>)p;
@ -308,8 +327,23 @@ namespace Avalonia
Contract.Requires<ArgumentNullException>(type != null);
Contract.Requires<ArgumentNullException>(property != null);
return Instance.GetRegistered(type).Any(x => x == property) ||
Instance.GetRegisteredAttached(type).Any(x => x == property);
static bool ContainsProperty(IReadOnlyList<AvaloniaProperty> properties, AvaloniaProperty property)
{
var propertiesCount = properties.Count;
for (var i = 0; i < propertiesCount; i++)
{
if (properties[i] == property)
{
return true;
}
}
return false;
}
return ContainsProperty(Instance.GetRegistered(type), property) ||
ContainsProperty(Instance.GetRegisteredAttached(type), property);
}
/// <summary>

Loading…
Cancel
Save