SharpLibEar/Object.cs
author StephaneLenclud
Fri, 19 Aug 2016 19:18:54 +0200
changeset 244 2e4d2558bb21
parent 243 cc2251d065db
child 246 30a221eecc06
permissions -rw-r--r--
Adding non functional generic EAR HID event.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 
     8 using System;
     9 using System.Collections.Generic;
    10 using System.Runtime.Serialization;
    11 using System.ComponentModel;
    12 
    13 namespace SharpLib.Ear
    14 {
    15 
    16     /// <summary>
    17     /// EAR object provides serialization support.
    18     /// It assumes most derived class is decorated with AttributeObject.
    19     /// </summary>
    20     [DataContract]
    21     [KnownType("DerivedTypes")]
    22     public abstract class Object: IComparable, INotifyPropertyChanged
    23     {
    24         private bool iConstructed = false;
    25 
    26         protected Object()
    27         {
    28             Construct();
    29         }
    30 
    31         /// <summary>
    32         /// Needed as our constructor is not called following internalization.
    33         /// </summary>
    34         public void Construct()
    35         {
    36             if (!iConstructed)
    37             {
    38                 DoConstruct();
    39                 iConstructed = true;
    40             }
    41         }
    42 
    43         /// <summary>
    44         /// 
    45         /// </summary>
    46         protected virtual void DoConstruct()
    47         {
    48 
    49         }
    50 
    51         /// <summary>
    52         /// Static object name.
    53         /// </summary>
    54         public string Name
    55         {
    56             //Get the name of this object attribute
    57             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
    58             private set { }
    59         }
    60 
    61         /// <summary>
    62         /// Static object description.
    63         /// </summary>
    64         public string Description
    65         {
    66             //Get the description of this object attribute
    67             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
    68             private set { }
    69         }
    70 
    71         /// <summary>
    72         /// Dynamic object description.
    73         /// </summary>
    74         /// <returns></returns>
    75         public virtual string Brief()
    76         {
    77             return Name;
    78         }
    79 
    80         /// <summary>
    81         /// Needed to make sure our sorting makes sense
    82         /// </summary>
    83         /// <param name="obj"></param>
    84         /// <returns></returns>
    85         public int CompareTo(object obj)
    86         {
    87             //Sort by object name
    88             return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
    89         }
    90 
    91         /// <summary>
    92         /// Tells whether the current object configuration is valid.
    93         /// </summary>
    94         /// <returns></returns>
    95         public virtual bool IsValid()
    96         {
    97             return true;
    98         }
    99 
   100         /// <summary>
   101         /// So that data contract knows all our types.
   102         /// </summary>
   103         /// <returns></returns>
   104         private static IEnumerable<Type> DerivedTypes()
   105         {
   106             return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
   107         }
   108 
   109         /// <summary>
   110         /// 
   111         /// </summary>
   112         public event PropertyChangedEventHandler PropertyChanged;
   113 
   114         /// <summary>
   115         /// Invoke our event.
   116         /// </summary>
   117         /// <param name="name"></param>
   118         protected void OnPropertyChanged(string name)
   119         {
   120             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
   121         }
   122 
   123 
   124     }
   125 }