SharpLibEar/Object.cs
author StephaneLenclud
Thu, 18 Aug 2016 18:49:03 +0200
changeset 241 3b5a94f31400
parent 238 c92587ddabcd
child 243 cc2251d065db
permissions -rw-r--r--
Adding support for HID Keyboard events.
     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 
    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
    23     {
    24         /// <summary>
    25         /// Static object name.
    26         /// </summary>
    27         public string Name
    28         {
    29             //Get the name of this object attribute
    30             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
    31             private set { }
    32         }
    33 
    34         /// <summary>
    35         /// Static object description.
    36         /// </summary>
    37         public string Description
    38         {
    39             //Get the description of this object attribute
    40             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
    41             private set { }
    42         }
    43 
    44         /// <summary>
    45         /// Dynamic object description.
    46         /// </summary>
    47         /// <returns></returns>
    48         public virtual string Brief()
    49         {
    50             return Name;
    51         }
    52 
    53         /// <summary>
    54         /// Needed to make sure our sorting makes sense
    55         /// </summary>
    56         /// <param name="obj"></param>
    57         /// <returns></returns>
    58         public int CompareTo(object obj)
    59         {
    60             //Sort by object name
    61             return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
    62         }
    63 
    64         /// <summary>
    65         /// Tells whether the current object configuration is valid.
    66         /// </summary>
    67         /// <returns></returns>
    68         public virtual bool IsValid()
    69         {
    70             return true;
    71         }
    72 
    73         /// <summary>
    74         /// So that data contract knows all our types.
    75         /// </summary>
    76         /// <returns></returns>
    77         private static IEnumerable<Type> DerivedTypes()
    78         {
    79             return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
    80         }
    81 
    82     }
    83 }