MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / ConvertTo

Method ConvertTo

dotnet/src/webdriver/BiDi/Script/RemoteValue.cs:65–111  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

63 public static implicit operator string?(RemoteValue remoteValue) => remoteValue.ConvertTo<string>();
64
65 public TResult? ConvertTo<TResult>()
66 => (this, typeof(TResult)) switch
67 {
68 (_, Type t) when t.IsAssignableFrom(GetType())
69 => (TResult)(object)this,
70 (BooleanRemoteValue b, Type t) when t == typeof(bool)
71 => (TResult)(object)b.Value,
72 (NullRemoteValue, Type t) when !t.IsValueType || Nullable.GetUnderlyingType(t) is not null
73 => default,
74 (NumberRemoteValue n, Type t) when t == typeof(byte)
75 => (TResult)(object)Convert.ToByte(n.Value),
76 (NumberRemoteValue n, Type t) when t == typeof(sbyte)
77 => (TResult)(object)Convert.ToSByte(n.Value),
78 (NumberRemoteValue n, Type t) when t == typeof(short)
79 => (TResult)(object)Convert.ToInt16(n.Value),
80 (NumberRemoteValue n, Type t) when t == typeof(ushort)
81 => (TResult)(object)Convert.ToUInt16(n.Value),
82 (NumberRemoteValue n, Type t) when t == typeof(int)
83 => (TResult)(object)Convert.ToInt32(n.Value),
84 (NumberRemoteValue n, Type t) when t == typeof(uint)
85 => (TResult)(object)Convert.ToUInt32(n.Value),
86 (NumberRemoteValue n, Type t) when t == typeof(long)
87 => (TResult)(object)Convert.ToInt64(n.Value),
88 (NumberRemoteValue n, Type t) when t == typeof(ulong)
89 => (TResult)(object)Convert.ToUInt64(n.Value),
90 (NumberRemoteValue n, Type t) when t == typeof(double)
91 => (TResult)(object)n.Value,
92 (NumberRemoteValue n, Type t) when t == typeof(float)
93 => (TResult)(object)Convert.ToSingle(n.Value),
94 (NumberRemoteValue n, Type t) when t == typeof(decimal)
95 => (TResult)(object)Convert.ToDecimal(n.Value),
96 (StringRemoteValue s, Type t) when t == typeof(string)
97 => (TResult)(object)s.Value,
98 (ArrayRemoteValue a, Type t) when t.IsArray
99 => ConvertRemoteValuesToArray<TResult>(a.Value, t.GetElementType()!),
100 (ArrayRemoteValue a, Type t) when t.IsGenericType && t.IsAssignableFrom(typeof(List<>).MakeGenericType(t.GetGenericArguments()[0]))
101 => ConvertRemoteValuesToGenericList<TResult>(a.Value, typeof(List<>).MakeGenericType(t.GetGenericArguments()[0])),
102 (MapRemoteValue m, Type t) when t.IsGenericType && t.GetGenericArguments().Length == 2 && t.IsAssignableFrom(typeof(Dictionary<,>).MakeGenericType(t.GetGenericArguments()))
103 => ConvertRemoteValuesToDictionary<TResult>(m.Value, typeof(Dictionary<,>).MakeGenericType(t.GetGenericArguments())),
104 (ObjectRemoteValue o, Type t) when t.IsGenericType && t.GetGenericArguments().Length == 2 && t.IsAssignableFrom(typeof(Dictionary<,>).MakeGenericType(t.GetGenericArguments()))
105 => ConvertRemoteValuesToDictionary<TResult>(o.Value, typeof(Dictionary<,>).MakeGenericType(t.GetGenericArguments())),
106
107 (_, Type t) when Nullable.GetUnderlyingType(t) is { } underlying
108 => ConvertToNullable<TResult>(underlying),
109
110 _ => throw new InvalidCastException($"Cannot convert {GetType().Name} to {typeof(TResult).FullName}")
111 };
112
113 private TResult ConvertToNullable<TResult>(Type underlyingType)
114 {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected