0fd8fa7ec75a560f46db6323a72c2fb252bb92d3
[mono.git] / mcs / class / corlib / System / MonoType.cs
1 //
2 // System.MonoType
3 //
4 // Authors: 
5 //      Sean MacIsaac (macisaac@ximian.com)
6 //      Paolo Molaro (lupus@ximian.com)
7 //      Patrik Torstensson (patrik.torstensson@labs2.com)
8 //      Gonzalo Paniagua (gonzalo@ximian.com)
9 //  Marek Safar (marek.safar@gmail.com)
10 //
11 // (c) 2001-2003 Ximian, Inc.
12 // Copyright (C) 2003-2005 Novell, Inc (http://www.novell.com)
13 // Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections.Generic;
36 using System.Globalization;
37 using System.Reflection;
38 using System.Runtime.InteropServices;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.Serialization;
41 using System.Security;
42 using System.Diagnostics.Contracts;
43 using System.Threading;
44 using System.Diagnostics;
45 using System.Security.Permissions;
46 using System.Runtime.Remoting.Activation;
47 using System.Runtime;
48
49 namespace System
50 {
51         // Contains information about the type which is expensive to compute
52         [StructLayout (LayoutKind.Sequential)]
53         internal class MonoTypeInfo {
54                 // this is the displayed form: special characters
55                 // ,+*&*[]\ in the identifier portions of the names
56                 // have been escaped with a leading backslash (\)
57                 public string full_name;
58                 public MonoCMethod default_ctor;
59         }
60
61         [Serializable]
62         [StructLayout (LayoutKind.Sequential)]
63         class MonoType : RuntimeType, ISerializable
64         {
65                 [NonSerialized]
66                 MonoTypeInfo type_info;
67
68                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
69                 private static extern void type_from_obj (MonoType type, Object obj);
70                 
71                 internal MonoType (Object obj)
72                 {
73                         // this should not be used - lupus
74                         type_from_obj (this, obj);
75                         
76                         throw new NotImplementedException ();
77                 }
78
79                 internal override MonoCMethod GetDefaultConstructor ()
80                 {
81                         MonoCMethod ctor = null;
82                         
83                         if (type_info == null)
84                                 type_info = new MonoTypeInfo ();
85                         else
86                                 ctor = type_info.default_ctor;
87
88                         if (ctor == null) {
89                                 var ctors = GetConstructors (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
90
91                                 for (int i = 0; i < ctors.Length; ++i) {
92                                         if (ctors [i].GetParametersCount () == 0) {
93                                                 type_info.default_ctor = ctor = (MonoCMethod) ctors [i];
94                                                 break;
95                                         }
96                                 }
97                         }
98
99                         return ctor;
100                 }
101
102                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103                 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
104
105                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
106                 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
107
108                 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
109                 {
110                         if (fromNoninstanciated == null)
111                                 throw new ArgumentNullException ("fromNoninstanciated");
112                         return GetCorrespondingInflatedMethod (fromNoninstanciated);
113                 }
114
115                 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
116                 {
117                         if (fromNoninstanciated == null)
118                                 throw new ArgumentNullException ("fromNoninstanciated");
119                         return GetCorrespondingInflatedConstructor (fromNoninstanciated);
120                 }
121
122                 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
123                 {
124                         /* create sensible flags from given FieldInfo */
125                         BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
126                         flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
127                         return GetField (fromNoninstanciated.Name, flags);
128                 }
129
130                 public override int GetHashCode()
131                 {
132                         Type t = UnderlyingSystemType;
133                         if (t != null && t != this)
134                                 return t.GetHashCode ();
135                         return (int)_impl.Value;
136                 }
137
138                 public override string FullName {
139                         get {
140                                 string fullName;
141                                 // This doesn't need locking
142                                 if (type_info == null)
143                                         type_info = new MonoTypeInfo ();
144                                 if ((fullName = type_info.full_name) == null)
145                                         fullName = type_info.full_name = getFullName (true, false);
146
147                                 return fullName;
148                         }
149                 }
150
151                 internal override bool IsUserType {
152                         get {
153                                 return false;
154                         }
155                 }
156         }
157 }