1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 16:27:43 +08:00

Inline condition

This commit is contained in:
Dan Balasescu 2024-09-25 22:46:53 +09:00
parent fd4891cf31
commit 2fe229d620
No known key found for this signature in database

View File

@ -16,7 +16,7 @@ namespace osu.Game.Utils
public static object GetValue(IBindable bindable) public static object GetValue(IBindable bindable)
{ {
Type? bindableWithValueType = bindable.GetType().GetInterfaces().FirstOrDefault(isBindableT); Type? bindableWithValueType = bindable.GetType().GetInterfaces().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IBindable<>));
if (bindableWithValueType == null) if (bindableWithValueType == null)
return bindable; return bindable;
@ -25,18 +25,13 @@ namespace osu.Game.Utils
public static void SetValue(IBindable bindable, object value) public static void SetValue(IBindable bindable, object value)
{ {
Type? bindableWithValueType = bindable.GetType().EnumerateBaseTypes().FirstOrDefault(isBindableT); Type? bindableWithValueType = bindable.GetType().EnumerateBaseTypes().SingleOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Bindable<>));
if (bindableWithValueType == null) if (bindableWithValueType == null)
return; return;
set_method.MakeGenericMethod(bindableWithValueType.GenericTypeArguments[0]).Invoke(null, [bindable, value]); set_method.MakeGenericMethod(bindableWithValueType.GenericTypeArguments[0]).Invoke(null, [bindable, value]);
} }
private static bool isBindableT(Type type)
=> type.IsGenericType
&& (type.GetGenericTypeDefinition() == typeof(Bindable<>)
|| type.GetGenericTypeDefinition() == typeof(IBindable<>));
private static object getValue<T>(object bindable) => ((IBindable<T>)bindable).Value!; private static object getValue<T>(object bindable) => ((IBindable<T>)bindable).Value!;
private static object setValue<T>(object bindable, object value) => ((Bindable<T>)bindable).Value = (T)value; private static object setValue<T>(object bindable, object value) => ((Bindable<T>)bindable).Value = (T)value;