SharpLibEar/Action.cs
author Stephane Lenclud
Sun, 31 Jul 2016 12:03:52 +0200
changeset 230 8c5cf2228e9a
parent 228 6a84d8282226
child 231 4c706feaf706
permissions -rw-r--r--
Published v0.10.2.0.
StephaneLenclud@210
     1
//
StephaneLenclud@210
     2
StephaneLenclud@210
     3
StephaneLenclud@210
     4
using System;
Stephane@212
     5
using System.Collections.Generic;
StephaneLenclud@210
     6
using System.Runtime.Serialization;
StephaneLenclud@210
     7
using System.Threading;
StephaneLenclud@210
     8
StephaneLenclud@210
     9
namespace SharpLib.Ear
StephaneLenclud@210
    10
{
StephaneLenclud@210
    11
    [DataContract]
Stephane@212
    12
    [KnownType("DerivedTypes")]
StephaneLenclud@210
    13
    public abstract class Action: IComparable
StephaneLenclud@210
    14
    {
StephaneLenclud@228
    15
        protected abstract void DoExecute();
StephaneLenclud@223
    16
StephaneLenclud@223
    17
        public void Execute()
StephaneLenclud@223
    18
        {
StephaneLenclud@229
    19
            Console.WriteLine("Action executing: " + Brief());
StephaneLenclud@223
    20
            DoExecute();
StephaneLenclud@223
    21
        }
StephaneLenclud@210
    22
StephaneLenclud@211
    23
        public string Name {
StephaneLenclud@211
    24
            //Get the name of this object action attribute
StephaneLenclud@211
    25
            get { return Utils.Reflection.GetAttribute<AttributeAction>(GetType()).Name; }
StephaneLenclud@211
    26
            private set { }
StephaneLenclud@211
    27
        }
StephaneLenclud@211
    28
StephaneLenclud@220
    29
        public virtual string Brief()
StephaneLenclud@220
    30
        {
StephaneLenclud@220
    31
            return Name;
StephaneLenclud@220
    32
        }
StephaneLenclud@220
    33
StephaneLenclud@210
    34
        public int CompareTo(object obj)
StephaneLenclud@210
    35
        {
StephaneLenclud@210
    36
            //Sort by action name
StephaneLenclud@210
    37
            return Utils.Reflection.GetAttribute<AttributeAction>(GetType()).Name.CompareTo(obj.GetType());            
StephaneLenclud@210
    38
        }
Stephane@212
    39
Stephane@212
    40
        private static IEnumerable<Type> DerivedTypes()
Stephane@212
    41
        {
Stephane@212
    42
            return SharpLib.Utils.Reflection.GetDerivedTypes<Action>();
Stephane@212
    43
        }
Stephane@212
    44
StephaneLenclud@210
    45
    }
StephaneLenclud@210
    46
StephaneLenclud@210
    47
StephaneLenclud@210
    48
}