SharpLibEar/Action.cs
author StephaneLenclud
Mon, 15 Aug 2016 12:11:26 +0200
changeset 233 2b9541e54f7d
parent 229 7c631055b94b
child 238 c92587ddabcd
permissions -rw-r--r--
Adding Harmony tab.
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@231
    17
        /// <summary>
StephaneLenclud@231
    18
        /// Allows testing from generic edit dialog.
StephaneLenclud@231
    19
        /// </summary>
StephaneLenclud@231
    20
        public void Test()
StephaneLenclud@231
    21
        {
StephaneLenclud@231
    22
            Console.WriteLine("Action test");
StephaneLenclud@231
    23
            Execute();
StephaneLenclud@231
    24
        }
StephaneLenclud@231
    25
StephaneLenclud@223
    26
        public void Execute()
StephaneLenclud@223
    27
        {
StephaneLenclud@229
    28
            Console.WriteLine("Action executing: " + Brief());
StephaneLenclud@223
    29
            DoExecute();
StephaneLenclud@223
    30
        }
StephaneLenclud@210
    31
StephaneLenclud@211
    32
        public string Name {
StephaneLenclud@211
    33
            //Get the name of this object action attribute
StephaneLenclud@231
    34
            get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
StephaneLenclud@211
    35
            private set { }
StephaneLenclud@211
    36
        }
StephaneLenclud@211
    37
StephaneLenclud@220
    38
        public virtual string Brief()
StephaneLenclud@220
    39
        {
StephaneLenclud@220
    40
            return Name;
StephaneLenclud@220
    41
        }
StephaneLenclud@220
    42
StephaneLenclud@210
    43
        public int CompareTo(object obj)
StephaneLenclud@210
    44
        {
StephaneLenclud@210
    45
            //Sort by action name
StephaneLenclud@231
    46
            return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());            
StephaneLenclud@210
    47
        }
Stephane@212
    48
Stephane@212
    49
        private static IEnumerable<Type> DerivedTypes()
Stephane@212
    50
        {
Stephane@212
    51
            return SharpLib.Utils.Reflection.GetDerivedTypes<Action>();
Stephane@212
    52
        }
Stephane@212
    53
StephaneLenclud@210
    54
    }
StephaneLenclud@210
    55
StephaneLenclud@210
    56
StephaneLenclud@210
    57
}