summaryrefslogtreecommitdiff
path: root/Ivy/IvyBindingAttribute.cs
blob: f431275fba92944fdcb8c1491280b79e432a9aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56


namespace IvyBus
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.Globalization;

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public sealed class IvyBindingAttribute : Attribute
    {
        private string expression;
        private string[] args;

        public string Expression
        {
            get { return this.expression; }
            set { this.expression = value; }
        }
        
        public ReadOnlyCollection<string> Args
        {
            get { return new ReadOnlyCollection<string>(args); }
        }
        
        public IvyBindingAttribute(string expression, params string[] args)
        {
            this.expression = expression;
            this.args = args;
        }
        public IvyBindingAttribute()
        {
            this.expression = "";
            this.args = null;
        }

        // translate part of expression to object property
        public string GetFormattedExpression(object target)
        {
            if (target == null || args == null )
            {
                return string.Format(this.expression);
            }

            object[] values = new object[this.args.Length];
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = target.GetType().GetProperty(args[i]).GetValue(target, null);
            }

            return string.Format(this.expression, values);
        }
    }
}