Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 19 additions & 8 deletions dotnet/src/webdriver/DevTools/DevToolsDomains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace OpenQA.Selenium.DevTools
Expand All @@ -33,14 +34,23 @@ public abstract class DevToolsDomains
// This is the list of known supported DevTools version implementation.
// When new versions are implemented for support, new types must be
// added to this dictionary.
private static readonly Dictionary<int, Type> SupportedDevToolsVersions = new Dictionary<int, Type>()
private static readonly Dictionary<int, DomainType> SupportedDevToolsVersions = new Dictionary<int, DomainType>()
{
{ 127, typeof(V127.V127Domains) },
{ 129, typeof(V129.V129Domains) },
{ 128, typeof(V128.V128Domains) },
{ 85, typeof(V85.V85Domains) }
{ 127, new DomainType(typeof(V127.V127Domains)) },
{ 129, new DomainType(typeof(V129.V129Domains) )},
{ 128, new DomainType(typeof(V128.V128Domains) )},
{ 85, new DomainType(typeof(V85.V85Domains)) },
};

/// <summary>Workaround for trimming.</summary>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the kind of workaround necessary when using a Type as a generic argument, which requires trimming annotations. Hope this is acceptable.

struct DomainType
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public Type Type;

public DomainType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type t) => Type = t;
}

/// <summary>
/// Gets the version-specific domains for the DevTools session. This value must be cast to a version specific type to be at all useful.
/// </summary>
Expand Down Expand Up @@ -102,12 +112,13 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes
return domains;
}

[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
private static Type MatchDomainsVersion(int desiredVersion, int versionRange)
{
// Return fast on an exact match
if (SupportedDevToolsVersions.ContainsKey(desiredVersion))
if (SupportedDevToolsVersions.TryGetValue(desiredVersion, out DomainType type))
{
return SupportedDevToolsVersions[desiredVersion];
return type.Type;
}

// Get the list of supported versions and sort descending
Expand All @@ -121,7 +132,7 @@ private static Type MatchDomainsVersion(int desiredVersion, int versionRange)
// (that is, closest without going over).
if (desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange)
{
return SupportedDevToolsVersions[supportedVersion];
return SupportedDevToolsVersions[supportedVersion].Type;
}
}

Expand Down
12 changes: 9 additions & 3 deletions dotnet/src/webdriver/DevTools/Json/JsonEnumMemberConverter.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.DevTools.Json
{
internal class JsonEnumMemberConverter<TEnum> : JsonConverter<TEnum> where TEnum : Enum
internal class JsonEnumMemberConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>
: JsonConverter<TEnum> where TEnum : struct, Enum
{
private readonly Dictionary<TEnum, string> _enumToString = new Dictionary<TEnum, string>();
private readonly Dictionary<string, TEnum> _stringToEnum = new Dictionary<string, TEnum>();

public JsonEnumMemberConverter()
{
var type = typeof(TEnum);
var values = Enum.GetValues(type);
#if NET5_0_OR_GREATER
TEnum[] values = Enum.GetValues<TEnum>();
#else
Array values = Enum.GetValues(type);
#endif

foreach (var value in values)
{
var enumMember = type.GetMember(value.ToString())[0];
var enumMember = type.GetField(value.ToString());
var attr = enumMember.GetCustomAttributes(typeof(EnumMemberAttribute), false)
.Cast<EnumMemberAttribute>()
.FirstOrDefault();
Expand Down
Loading