aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.PMR/ExtensionMethods.cs
blob: 83667da72f89fe1004545ed2f50fd77c84d7df98 (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
57
58
59
using Google.Protobuf;
using Google.Protobuf.Reflection;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR.Common;
using Tango.PMR.Integration;

/// <summary>
/// Contains PMR extension methods.
/// </summary>
public static class ExtensionMethods
{
    /// <summary>
    /// Gets the protobuf <see cref="OriginalNameAttribute"/> attribute value from the message type.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns></returns>
    public static String ToOriginalName(this MessageType value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        OriginalNameAttribute[] attributes = (OriginalNameAttribute[])fi.GetCustomAttributes(typeof(OriginalNameAttribute), false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Name;
        else
            return value.ToString();
    }

    /// <summary>
    /// Gets the message parser.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <returns></returns>
    public static MessageParser GetParser(this IMessage message)
    {
        MessageParser parser = message.GetType().GetProperty("Parser").GetValue(message) as MessageParser;
        return parser;
    }

    /// <summary>
    /// Determines whether the specified intent requires a password.
    /// </summary>
    /// <param name="intent">The intent.</param>
    /// <returns></returns>
    public static bool RequiresPassword(this ExternalBridgeLoginIntent intent)
    {
        return 
            intent == ExternalBridgeLoginIntent.Diagnostics
            ||
            intent == ExternalBridgeLoginIntent.FullControl;
    }
}