Skip to content

Commit 00b6af5

Browse files
committed
Change converters initialization.
1 parent 4381a37 commit 00b6af5

File tree

6 files changed

+42
-100
lines changed

6 files changed

+42
-100
lines changed

src/UnityMvvmToolkit.Core/Internal/ObjectProviders/CommandProvider.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,12 @@ namespace UnityMvvmToolkit.Core.Internal.ObjectProviders
99
{
1010
internal class CommandProvider<TBindingContext> : ObjectProvider<TBindingContext>
1111
{
12-
private HashSet<IParameterConverter> _parameterConverters;
12+
private readonly HashSet<IParameterConverter> _parameterConverters;
1313

1414
internal CommandProvider(TBindingContext bindingContext, IEnumerable<IConverter> converters)
1515
: base(bindingContext)
1616
{
17-
InitializeConverters(converters);
18-
}
19-
20-
private void InitializeConverters(IEnumerable<IConverter> converters)
21-
{
22-
if (converters == null)
23-
{
24-
return;
25-
}
26-
27-
_parameterConverters = new HashSet<IParameterConverter>();
28-
29-
foreach (var converter in converters)
30-
{
31-
if (converter is IParameterConverter parameterConverter)
32-
{
33-
_parameterConverters.Add(parameterConverter);
34-
}
35-
}
17+
_parameterConverters = GetConverters<IParameterConverter>(converters);
3618
}
3719

3820
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -116,11 +98,6 @@ public ICommandWrapper GetCommandWrapper(string propertyName, ReadOnlyMemory<cha
11698
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11799
private IParameterConverter GetParameterConverter(Type targetType, ReadOnlySpan<char> converterName)
118100
{
119-
if (_parameterConverters == null)
120-
{
121-
throw new NullReferenceException(nameof(_parameterConverters));
122-
}
123-
124101
var parameterConverter = converterName.IsEmpty
125102
? GetConverter(targetType)
126103
: GetConverter(targetType, converterName);

src/UnityMvvmToolkit.Core/Internal/ObjectProviders/ObjectProvider.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Reflection;
44
using System.Runtime.CompilerServices;
5+
using UnityMvvmToolkit.Core.Interfaces;
56

67
namespace UnityMvvmToolkit.Core.Internal.ObjectProviders
78
{
@@ -18,6 +19,22 @@ internal ObjectProvider(TBindingContext bindingContext)
1819

1920
protected TBindingContext BindingContext => _bindingContext;
2021

22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
protected HashSet<T> GetConverters<T>(IEnumerable<IConverter> converters) where T : IConverter
24+
{
25+
var result = new HashSet<T>();
26+
27+
foreach (var converter in converters)
28+
{
29+
if (converter is T valueConverter)
30+
{
31+
result.Add(valueConverter);
32+
}
33+
}
34+
35+
return result;
36+
}
37+
2138
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2239
protected void AssurePropertyExist(string propertyName, out PropertyInfo propertyInfo)
2340
{

src/UnityMvvmToolkit.Core/Internal/ObjectProviders/PropertyProvider.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,12 @@ namespace UnityMvvmToolkit.Core.Internal.ObjectProviders
99
{
1010
internal class PropertyProvider<TBindingContext> : ObjectProvider<TBindingContext>
1111
{
12-
private HashSet<IValueConverter> _valueConverters;
12+
private readonly HashSet<IValueConverter> _valueConverters;
1313

1414
internal PropertyProvider(TBindingContext bindingContext, IEnumerable<IConverter> converters)
1515
: base(bindingContext)
1616
{
17-
InitializeConverters(converters);
18-
}
19-
20-
private void InitializeConverters(IEnumerable<IConverter> converters)
21-
{
22-
if (converters == null)
23-
{
24-
return;
25-
}
26-
27-
_valueConverters = new HashSet<IValueConverter>();
28-
29-
foreach (var converter in converters)
30-
{
31-
if (converter is IValueConverter valueConverter)
32-
{
33-
_valueConverters.Add(valueConverter);
34-
}
35-
}
17+
_valueConverters = GetConverters<IValueConverter>(converters);
3618
}
3719

3820
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -102,11 +84,6 @@ private TProperty CreateProperty<TProperty, TValueType>(string propertyName, Rea
10284
[MethodImpl(MethodImplOptions.AggressiveInlining)]
10385
private IValueConverter GetValueConverter<TValueType>(Type sourceType, ReadOnlySpan<char> converterName)
10486
{
105-
if (_valueConverters == null)
106-
{
107-
throw new NullReferenceException(nameof(_valueConverters));
108-
}
109-
11087
var valueConverter = converterName.IsEmpty
11188
? GetConverter(sourceType, typeof(TValueType))
11289
: GetConverter(sourceType, typeof(TValueType), converterName);

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Internal/ObjectProviders/CommandProvider.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,12 @@ namespace UnityMvvmToolkit.Core.Internal.ObjectProviders
99
{
1010
internal class CommandProvider<TBindingContext> : ObjectProvider<TBindingContext>
1111
{
12-
private HashSet<IParameterConverter> _parameterConverters;
12+
private readonly HashSet<IParameterConverter> _parameterConverters;
1313

1414
internal CommandProvider(TBindingContext bindingContext, IEnumerable<IConverter> converters)
1515
: base(bindingContext)
1616
{
17-
InitializeConverters(converters);
18-
}
19-
20-
private void InitializeConverters(IEnumerable<IConverter> converters)
21-
{
22-
if (converters == null)
23-
{
24-
return;
25-
}
26-
27-
_parameterConverters = new HashSet<IParameterConverter>();
28-
29-
foreach (var converter in converters)
30-
{
31-
if (converter is IParameterConverter parameterConverter)
32-
{
33-
_parameterConverters.Add(parameterConverter);
34-
}
35-
}
17+
_parameterConverters = GetConverters<IParameterConverter>(converters);
3618
}
3719

3820
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -116,11 +98,6 @@ public ICommandWrapper GetCommandWrapper(string propertyName, ReadOnlyMemory<cha
11698
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11799
private IParameterConverter GetParameterConverter(Type targetType, ReadOnlySpan<char> converterName)
118100
{
119-
if (_parameterConverters == null)
120-
{
121-
throw new NullReferenceException(nameof(_parameterConverters));
122-
}
123-
124101
var parameterConverter = converterName.IsEmpty
125102
? GetConverter(targetType)
126103
: GetConverter(targetType, converterName);

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Internal/ObjectProviders/ObjectProvider.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Reflection;
44
using System.Runtime.CompilerServices;
5+
using UnityMvvmToolkit.Core.Interfaces;
56

67
namespace UnityMvvmToolkit.Core.Internal.ObjectProviders
78
{
@@ -18,6 +19,22 @@ internal ObjectProvider(TBindingContext bindingContext)
1819

1920
protected TBindingContext BindingContext => _bindingContext;
2021

22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
protected HashSet<T> GetConverters<T>(IEnumerable<IConverter> converters) where T : IConverter
24+
{
25+
var result = new HashSet<T>();
26+
27+
foreach (var converter in converters)
28+
{
29+
if (converter is T valueConverter)
30+
{
31+
result.Add(valueConverter);
32+
}
33+
}
34+
35+
return result;
36+
}
37+
2138
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2239
protected void AssurePropertyExist(string propertyName, out PropertyInfo propertyInfo)
2340
{

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Internal/ObjectProviders/PropertyProvider.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,12 @@ namespace UnityMvvmToolkit.Core.Internal.ObjectProviders
99
{
1010
internal class PropertyProvider<TBindingContext> : ObjectProvider<TBindingContext>
1111
{
12-
private HashSet<IValueConverter> _valueConverters;
12+
private readonly HashSet<IValueConverter> _valueConverters;
1313

1414
internal PropertyProvider(TBindingContext bindingContext, IEnumerable<IConverter> converters)
1515
: base(bindingContext)
1616
{
17-
InitializeConverters(converters);
18-
}
19-
20-
private void InitializeConverters(IEnumerable<IConverter> converters)
21-
{
22-
if (converters == null)
23-
{
24-
return;
25-
}
26-
27-
_valueConverters = new HashSet<IValueConverter>();
28-
29-
foreach (var converter in converters)
30-
{
31-
if (converter is IValueConverter valueConverter)
32-
{
33-
_valueConverters.Add(valueConverter);
34-
}
35-
}
17+
_valueConverters = GetConverters<IValueConverter>(converters);
3618
}
3719

3820
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -102,11 +84,6 @@ private TProperty CreateProperty<TProperty, TValueType>(string propertyName, Rea
10284
[MethodImpl(MethodImplOptions.AggressiveInlining)]
10385
private IValueConverter GetValueConverter<TValueType>(Type sourceType, ReadOnlySpan<char> converterName)
10486
{
105-
if (_valueConverters == null)
106-
{
107-
throw new NullReferenceException(nameof(_valueConverters));
108-
}
109-
11087
var valueConverter = converterName.IsEmpty
11188
? GetConverter(sourceType, typeof(TValueType))
11289
: GetConverter(sourceType, typeof(TValueType), converterName);

0 commit comments

Comments
 (0)