2 // typegen.cs: type generation
4 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
19 public class TypeManager {
20 static public Type object_type;
21 static public Type value_type;
22 static public Type string_type;
23 static public Type int32_type;
24 static public Type uint32_type;
25 static public Type int64_type;
26 static public Type uint64_type;
27 static public Type float_type;
28 static public Type double_type;
29 static public Type char_type;
30 static public Type short_type;
31 static public Type decimal_type;
32 static public Type bool_type;
33 static public Type sbyte_type;
34 static public Type byte_type;
35 static public Type ushort_type;
36 static public Type enum_type;
37 static public Type delegate_type;
38 static public Type void_type;
41 // Holds the Array of Assemblies that have been loaded
42 // (either because it is the default or the user used the
43 // -r command line option)
48 // This is the type_cache from the assemblies to avoid
49 // hitting System.Reflection on every lookup.
54 // This is used to hotld the corresponding TypeContainer objects
55 // since we need this in FindMembers
57 Hashtable typecontainers;
60 // Keeps track of those types that are defined by the
67 assemblies = new ArrayList ();
68 user_types = new ArrayList ();
69 types = new Hashtable ();
70 typecontainers = new Hashtable ();
73 public void AddUserType (string name, TypeBuilder t, TypeContainer tc)
75 types.Add (t.FullName, t);
77 typecontainers.Add (t.FullName, tc);
80 public void AddUserType (string name, TypeBuilder t)
82 this.AddUserType (name, t, null);
86 // Registers an assembly to load types from.
88 public void AddAssembly (Assembly a)
94 // Returns the Type associated with @name
96 public Type LookupType (string name)
101 // First lookup in user defined and cached values
104 t = (Type) types [name];
108 foreach (Assembly a in assemblies){
109 t = a.GetType (name);
121 // Returns the C# name of a type if possible, or the full type name otherwise
123 static public string CSharpName (Type t)
127 else if (t == uint32_type)
129 else if (t == int64_type)
131 else if (t == uint64_type)
133 else if (t == float_type)
135 else if (t == double_type)
137 else if (t == char_type)
139 else if (t == short_type)
141 else if (t == decimal_type)
143 else if (t == bool_type)
145 else if (t == sbyte_type)
147 else if (t == byte_type)
149 else if (t == short_type)
151 else if (t == ushort_type)
153 else if (t == System.Type.GetType ("System.String"))
159 Type CoreLookupType (string name)
161 Type t = LookupType (name);
164 throw new Exception ("Can not find core type " + name);
170 // The types have to be initialized after the initial
171 // population of the type has happened (for example, to
172 // bootstrap the corlib.dll
174 public void InitCoreTypes ()
176 object_type = CoreLookupType ("System.Object");
177 value_type = CoreLookupType ("System.ValueType");
178 string_type = CoreLookupType ("System.String");
179 int32_type = CoreLookupType ("System.Int32");
180 int64_type = CoreLookupType ("System.Int64");
181 uint32_type = CoreLookupType ("System.UInt32");
182 uint64_type = CoreLookupType ("System.UInt64");
183 float_type = CoreLookupType ("System.Single");
184 double_type = CoreLookupType ("System.Double");
185 byte_type = CoreLookupType ("System.Byte");
186 sbyte_type = CoreLookupType ("System.SByte");
187 char_type = CoreLookupType ("System.Char");
188 short_type = CoreLookupType ("System.Int16");
189 ushort_type = CoreLookupType ("System.UInt16");
190 decimal_type = CoreLookupType ("System.Decimal");
191 bool_type = CoreLookupType ("System.Boolean");
192 enum_type = CoreLookupType ("System.Enum");
193 delegate_type = CoreLookupType ("System.Delegate");
194 void_type = CoreLookupType ("System.Void");
197 public MemberInfo [] FindMembers (Type t, MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
201 tc = (TypeContainer) typecontainers [t.FullName];
204 return t.FindMembers (mt, bf, filter, criteria);
206 return tc.FindMembers (mt, bf, filter, criteria);
211 // Returns the User Defined Types
213 public ArrayList UserTypes {
219 public Hashtable TypeContainers {
221 return typecontainers;