Konfigurationsklasse - Was GENAU macht sie?

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Bluespide.

    Konfigurationsklasse - Was GENAU macht sie?

    Hey,
    ich reverse gerade das Spiel osu! (https://www.github.com/ppy/osu) und ich bin nun auf folgende Klasse gestoßen (ich kopiere gleich den ganzen Namespace):

    C#-Quellcode

    1. // Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
    2. // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE
    3. using System;
    4. using System.Collections.Generic;
    5. namespace osu.Framework.Configuration
    6. {
    7. public class Bindable<T> : IBindable
    8. {
    9. private T value;
    10. public T Default;
    11. public bool Disabled;
    12. public delegate void BindableValueChanged<in TValue>(TValue newValue);
    13. public virtual bool IsDefault => Equals(value, Default);
    14. public event BindableValueChanged<T> ValueChanged;
    15. public virtual T Value
    16. {
    17. get { return value; }
    18. set
    19. {
    20. if (EqualityComparer<T>.Default.Equals(this.value, value)) return;
    21. if (Disabled)
    22. {
    23. TriggerChange();
    24. return;
    25. }
    26. this.value = value;
    27. TriggerChange();
    28. }
    29. }
    30. public Bindable(T value = default(T))
    31. {
    32. this.value = value;
    33. }
    34. public static implicit operator T(Bindable<T> value)
    35. {
    36. return value.Value;
    37. }
    38. private List<WeakReference<Bindable<T>>> bindings = new List<WeakReference<Bindable<T>>>();
    39. public WeakReference<Bindable<T>> WeakReference => new WeakReference<Bindable<T>>(this);
    40. /// <summary>
    41. /// Binds outselves to another bindable such that they receive bi-directional updates.
    42. /// We will take on any value limitations of the bindable we bind width.
    43. /// </summary>
    44. /// <param name="them">The foreign bindable. This should always be the most permanent end of the bind (ie. a ConfigManager)</param>
    45. public virtual void BindTo(Bindable<T> them)
    46. {
    47. Value = them.Value;
    48. AddWeakReference(them.WeakReference);
    49. them.AddWeakReference(WeakReference);
    50. }
    51. protected void AddWeakReference(WeakReference<Bindable<T>> weakReference) => bindings.Add(weakReference);
    52. public virtual bool Parse(object s)
    53. {
    54. if (s is T)
    55. Value = (T)s;
    56. else if (typeof(T).IsEnum && s is string)
    57. Value = (T)Enum.Parse(typeof(T), (string)s);
    58. else
    59. return false;
    60. return true;
    61. }
    62. public void TriggerChange()
    63. {
    64. ValueChanged?.Invoke(value);
    65. foreach (var w in bindings.ToArray())
    66. {
    67. Bindable<T> b;
    68. if (w.TryGetTarget(out b))
    69. b.Value = value;
    70. else
    71. bindings.Remove(w);
    72. }
    73. }
    74. public void UnbindAll()
    75. {
    76. ValueChanged = null;
    77. }
    78. public string Description { get; set; }
    79. public override string ToString()
    80. {
    81. return value?.ToString() ?? string.Empty;
    82. }
    83. internal void Reset()
    84. {
    85. Value = Default;
    86. }
    87. /// <summary>
    88. /// Retrieve a new bindable instance weakly bound to the configuration backing.
    89. /// If you are further binding to events of a bindable retrieved using this method, ensure to hold
    90. /// a local reference.
    91. /// </summary>
    92. /// <returns>A weakly bound copy of the specified bindable.</returns>
    93. public Bindable<T> GetBoundCopy()
    94. {
    95. var copy = (Bindable<T>)MemberwiseClone();
    96. copy.bindings = new List<WeakReference<Bindable<T>>>();
    97. copy.BindTo(this);
    98. return copy;
    99. }
    100. }
    101. }


    Nun verstehe ich nicht, was die Klasse Bindable genau konfiguriert.
    Kann mir irgendjemand helfen?
    Danke im vorraus.

    ~blaze~: Thema verschoben

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „~blaze~“ ()

    Hast du schon mal mit WPF gearbeitet? Diese Klasse bildet entsprechend ihrem Namen Datenbindungen ab. Du kannst also damit angelegte Properties oder Member mit Oberflächenelementen verknüpfen, die sich dann automatisch aktualisieren. Das gehört zum MVVM-Pattern bzw. ist für die strikte Trennung von Daten und GUI nützlich.