summaryrefslogtreecommitdiff
path: root/Ivy/IvyBindingAttribute.cs
blob: 1380f0fee2d953aa120d5e1914c5c8a563b31cd1 (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


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; }
        }
        
        public ReadOnlyCollection<string> Args
        {
            get { return new ReadOnlyCollection<string>(args); }
        }
        
        public IvyBindingAttribute(string expression, params string[] args)
        {
            this.expression = expression;
            this.args = args;
        }

        // translate part of expression to object property
        public string GetFormattedExpression(object target)
        {
            if (target == 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);
        }
    }
}