Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Add trimming attributes, address some trim warnings #14637

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
26 changes: 18 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,22 @@ 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

@RenderMichael RenderMichael Oct 23, 2024

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, and in a way 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 +111,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 +131,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
13 changes: 9 additions & 4 deletions dotnet/src/webdriver/DevTools/Json/JsonEnumMemberConverter.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
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)
Copy link
Contributor Author

@RenderMichael RenderMichael Oct 23, 2024

Choose a reason for hiding this comment

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

var here is actually TEnum in .NET, but object in .NET Standard. All use of value is either cast or are ToString()-ed, so there's no runtime difference.

{
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
Loading