Skip to content

Commit c497ca7

Browse files
committed
Remove reflection usage in AttributeSelectorFactory
1 parent d9258c2 commit c497ca7

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/ExCSS/Extensions/PortableExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Linq;
34
using System.Reflection;
45

@@ -13,7 +14,11 @@ public static string ConvertFromUtf32(this int utf32)
1314
return char.ConvertFromUtf32(utf32);
1415
}
1516

16-
public static PropertyInfo[] GetProperties(this Type type)
17+
public static PropertyInfo[] GetProperties(
18+
#if NET6_0_OR_GREATER
19+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
20+
#endif
21+
this Type type)
1722
{
1823
return type.GetRuntimeProperties().ToArray();
1924
}

src/ExCSS/Factories/AttributeSelectorFactory.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
using System;
2-
using System.Collections.Generic;
32

43
namespace ExCSS
54
{
65
public sealed class AttributeSelectorFactory
76
{
87
private static readonly Lazy<AttributeSelectorFactory> Lazy = new(() => new AttributeSelectorFactory());
98

10-
private readonly Dictionary<string, Type> _types = new()
11-
{
12-
{ Combinators.Exactly, typeof(AttrMatchSelector) },
13-
{ Combinators.InList, typeof(AttrListSelector) },
14-
{ Combinators.InToken, typeof(AttrHyphenSelector) },
15-
{ Combinators.Begins, typeof(AttrBeginsSelector) },
16-
{ Combinators.Ends, typeof(AttrEndsSelector) },
17-
{ Combinators.InText, typeof(AttrContainsSelector) },
18-
{ Combinators.Unlike, typeof(AttrNotMatchSelector) },
19-
};
20-
219
private AttributeSelectorFactory()
2210
{
2311
}
@@ -34,11 +22,23 @@ public IAttrSelector Create(string combinator, string match, string value, strin
3422
_ = AttributeSelectorFactory.FormMatch(prefix, match);
3523
}
3624

37-
return _types.TryGetValue(combinator, out var type)
38-
? (IAttrSelector)Activator.CreateInstance(type, name, value)
39-
: new AttrAvailableSelector(name, value);
25+
if (combinator == Combinators.Exactly)
26+
return new AttrMatchSelector(name, value);
27+
if (combinator == Combinators.InList)
28+
return new AttrListSelector(name, value);
29+
if (combinator == Combinators.InToken)
30+
return new AttrHyphenSelector(name, value);
31+
if (combinator == Combinators.Begins)
32+
return new AttrBeginsSelector(name, value);
33+
if (combinator == Combinators.Ends)
34+
return new AttrEndsSelector(name, value);
35+
if (combinator == Combinators.InText)
36+
return new AttrContainsSelector(name, value);
37+
if (combinator == Combinators.Unlike)
38+
return new AttrNotMatchSelector(name, value);
39+
return new AttrAvailableSelector(name, value);
4040
}
41-
41+
4242
private static string FormFront(string prefix, string match)
4343
{
4444
return string.Concat(prefix, Combinators.Pipe, match);

0 commit comments

Comments
 (0)