aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Service/StubReflection.cs
blob: 2b0f237ea88d005815de2a35a80382d262de7e45 (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
using FastMember;
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR;
using Tango.PMR.Common;

namespace Tango.StubsUtils.Service
{
    public class StubReflection
    {
        public Type Type { get; set; }
        public MessageType MessageType { get; set; }
        public List<PropertyInfo> Properties { get; set; }
        public TypeAccessor Accesor { get; set; }
        public MessageParser Parser { get; set; }

        public StubReflection()
        {
            Properties = new List<PropertyInfo>();
        }

        public static StubReflection FromStubName(String stubName, List<Type> stubTypes)
        {
            StubReflection reflection = new StubReflection();

            var stubType = stubTypes.SingleOrDefault(x => x.Name.ToLower() == stubName.ToLower() || x.Name.Replace("Request", "").ToLower() == stubName.ToLower());

            if (stubType == null)
            {
                throw new InvalidOperationException($"Invalid stub name '{stubName}'.");
            }

            reflection.Type = stubType;
            reflection.MessageType = MessageFactory.ParseMessageType(reflection.Type.Name);
            reflection.Properties = stubType.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
            reflection.Accesor = TypeAccessor.Create(stubType);

            var instance = Activator.CreateInstance(stubType);
            reflection.Parser = stubType.GetProperty("Parser").GetValue(instance) as MessageParser;

            return reflection;
        }
    }
}