[corlib] Type from reference sources
[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                 public string full_name;
55                 public MonoCMethod default_ctor;
56         }
57
58         [Serializable]
59         [StructLayout (LayoutKind.Sequential)]
60         class MonoType : RuntimeType, ISerializable
61         {
62                 [NonSerialized]
63                 MonoTypeInfo type_info;
64
65                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
66                 private static extern void type_from_obj (MonoType type, Object obj);
67                 
68                 internal MonoType (Object obj)
69                 {
70                         // this should not be used - lupus
71                         type_from_obj (this, obj);
72                         
73                         throw new NotImplementedException ();
74                 }
75
76                 internal override MonoCMethod GetDefaultConstructor ()
77                 {
78                         MonoCMethod ctor = null;
79                         
80                         if (type_info == null)
81                                 type_info = new MonoTypeInfo ();
82                         else
83                                 ctor = type_info.default_ctor;
84
85                         if (ctor == null) {
86                                 var ctors = GetConstructors (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
87
88                                 for (int i = 0; i < ctors.Length; ++i) {
89                                         if (ctors [i].GetParametersCount () == 0) {
90                                                 type_info.default_ctor = ctor = (MonoCMethod) ctors [i];
91                                                 break;
92                                         }
93                                 }
94                         }
95
96                         return ctor;
97                 }
98
99                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
100                 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
101
102                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103                 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
104
105                 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
106                 {
107                         if (fromNoninstanciated == null)
108                                 throw new ArgumentNullException ("fromNoninstanciated");
109                         return GetCorrespondingInflatedMethod (fromNoninstanciated);
110                 }
111
112                 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
113                 {
114                         if (fromNoninstanciated == null)
115                                 throw new ArgumentNullException ("fromNoninstanciated");
116                         return GetCorrespondingInflatedConstructor (fromNoninstanciated);
117                 }
118
119                 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
120                 {
121                         /* create sensible flags from given FieldInfo */
122                         BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
123                         flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
124                         return GetField (fromNoninstanciated.Name, flags);
125                 }
126
127                 public override int GetHashCode()
128                 {
129                         Type t = UnderlyingSystemType;
130                         if (t != null && t != this)
131                                 return t.GetHashCode ();
132                         return (int)_impl.Value;
133                 }
134
135                 public override string FullName {
136                         get {
137                                 string fullName;
138                                 // This doesn't need locking
139                                 if (type_info == null)
140                                         type_info = new MonoTypeInfo ();
141                                 if ((fullName = type_info.full_name) == null)
142                                         fullName = type_info.full_name = getFullName (true, false);
143
144                                 return fullName;
145                         }
146                 }
147
148                 internal override bool IsUserType {
149                         get {
150                                 return false;
151                         }
152                 }
153         }
154 }