1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using Mono.Cecil;
10 
11 namespace Antmicro.Renode.Utilities
12 {
13     public class TypeDescriptor
14     {
TypeDescriptor(Type t)15         public TypeDescriptor(Type t)
16         {
17             underlyingType = t;
18         }
19 
TypeDescriptor(TypeDefinition t)20         public TypeDescriptor(TypeDefinition t)
21         {
22             underlyingType = t;
23         }
24 
25         public string Name
26         {
27             get
28             {
29                 var type = underlyingType as Type;
30                 if (type != null)
31                 {
32                     return type.Name;
33                 }
34 
35                 var typeDefinition = underlyingType as TypeDefinition;
36                 if (typeDefinition != null)
37                 {
38                     return typeDefinition.Name;
39                 }
40 
41                 throw new ArgumentException("Unsupported underlying type: " + underlyingType.GetType().FullName);
42             }
43         }
44 
45         public string Namespace
46         {
47             get
48             {
49                 var type = underlyingType as Type;
50                 if (type != null)
51                 {
52                     return type.Namespace;
53                 }
54 
55                 var typeDefinition = underlyingType as TypeDefinition;
56                 if (typeDefinition != null)
57                 {
58                     return typeDefinition.Namespace;
59                 }
60 
61                 throw new ArgumentException("Unsupported underlying type: " + underlyingType.GetType().FullName);
62             }
63 
64         }
65 
ResolveType()66         public Type ResolveType()
67         {
68             var type = underlyingType as Type;
69             if (type != null)
70             {
71                 return type;
72             }
73 
74             var typeDefinition = underlyingType as TypeDefinition;
75             if (typeDefinition != null)
76             {
77                 return TypeResolver.ResolveType(typeDefinition.GetFullNameOfMember()) ??
78                     TypeManager.Instance.GetTypeByName(typeDefinition.GetFullNameOfMember());
79             }
80 
81             throw new ArgumentException("Unsupported underlying type: " + underlyingType.GetType().FullName);
82         }
83 
84         private readonly object underlyingType;
85     }
86 }
87 
88