2 // System.Reflection.Emit.DerivedTypes.cs
5 // Rodrigo Kumpera <rkumpera@novell.com>
8 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Reflection;
32 using System.Reflection.Emit;
33 using System.Collections;
34 using System.Runtime.CompilerServices;
35 using System.Globalization;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
41 namespace System.Reflection.Emit
43 internal enum TypeKind : int {
48 [StructLayout (LayoutKind.Sequential)]
49 internal abstract class DerivedType : Type
51 internal Type elementType;
53 [MethodImplAttribute(MethodImplOptions.InternalCall)]
54 internal static extern void create_unmanaged_type (Type type);
56 internal DerivedType (Type elementType)
58 this.elementType = elementType;
61 internal abstract String FormatName (string elementName);
63 public override Type GetInterface (string name, bool ignoreCase)
65 throw new NotSupportedException ();
68 public override Type[] GetInterfaces ()
70 throw new NotSupportedException ();
73 public override Type GetElementType ()
78 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
80 throw new NotSupportedException ();
83 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
85 throw new NotSupportedException ();
88 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
90 throw new NotSupportedException ();
93 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
95 throw new NotSupportedException ();
98 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
100 throw new NotSupportedException ();
103 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
104 CallingConventions callConvention, Type[] types,
105 ParameterModifier[] modifiers)
107 throw new NotSupportedException ();
110 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
112 throw new NotSupportedException ();
115 public override Type GetNestedType (string name, BindingFlags bindingAttr)
117 throw new NotSupportedException ();
120 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
122 throw new NotSupportedException ();
125 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
127 throw new NotSupportedException ();
130 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
131 Type returnType, Type[] types, ParameterModifier[] modifiers)
133 throw new NotSupportedException ();
136 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
138 CallingConventions callConvention,
140 ParameterModifier[] modifiers)
142 throw new NotSupportedException ();
146 protected override TypeAttributes GetAttributeFlagsImpl ()
148 /*LAMEIMPL MS just return the elementType.Attributes*/
149 return elementType.Attributes;
152 protected override bool HasElementTypeImpl ()
157 protected override bool IsArrayImpl ()
162 protected override bool IsByRefImpl ()
167 protected override bool IsCOMObjectImpl ()
172 protected override bool IsPointerImpl ()
177 protected override bool IsPrimitiveImpl ()
183 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
185 throw new NotSupportedException ();
188 public override object InvokeMember (string name, BindingFlags invokeAttr,
189 Binder binder, object target, object[] args,
190 ParameterModifier[] modifiers,
191 CultureInfo culture, string[] namedParameters)
193 throw new NotSupportedException ();
196 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
198 throw new NotSupportedException ();
201 public override bool IsInstanceOfType (object o)
206 public override bool IsAssignableFrom (Type c)
211 public override bool ContainsGenericParameters {
212 get { return elementType.ContainsGenericParameters; }
215 //FIXME this should be handled by System.Type
216 public override Type MakeGenericType (params Type[] typeArguments)
218 throw new NotSupportedException ();
221 public override Type MakeArrayType ()
223 return new ArrayType (this, 0);
226 public override Type MakeArrayType (int rank)
229 throw new IndexOutOfRangeException ();
230 return new ArrayType (this, rank);
233 public override Type MakeByRefType ()
235 return new ByRefType (this);
238 public override Type MakePointerType ()
240 return new PointerType (this);
243 public override string ToString ()
245 return FormatName (elementType.ToString ());
248 public override GenericParameterAttributes GenericParameterAttributes {
249 get { throw new NotSupportedException (); }
252 public override StructLayoutAttribute StructLayoutAttribute {
253 get { throw new NotSupportedException (); }
256 public override Assembly Assembly {
257 get { return elementType.Assembly; }
260 public override string AssemblyQualifiedName {
262 string fullName = FormatName (elementType.FullName);
263 if (fullName == null)
265 return fullName + ", " + elementType.Assembly.FullName;
270 public override string FullName {
272 return FormatName (elementType.FullName);
276 public override string Name {
278 return FormatName (elementType.Name);
282 public override Guid GUID {
283 get { throw new NotSupportedException (); }
286 public override Module Module {
287 get { return elementType.Module; }
290 public override string Namespace {
291 get { return elementType.Namespace; }
294 public override RuntimeTypeHandle TypeHandle {
295 get { throw new NotSupportedException (); }
298 public override Type UnderlyingSystemType {
300 create_unmanaged_type (this);
306 public override bool IsDefined (Type attributeType, bool inherit)
308 throw new NotSupportedException ();
311 public override object [] GetCustomAttributes (bool inherit)
313 throw new NotSupportedException ();
316 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
318 throw new NotSupportedException ();
321 internal override bool IsUserType {
323 return elementType.IsUserType;
328 [StructLayout (LayoutKind.Sequential)]
329 internal class ArrayType : DerivedType
333 internal ArrayType (Type elementType, int rank) : base (elementType)
338 internal int GetEffectiveRank ()
343 internal override Type InternalResolve ()
345 Type et = elementType.InternalResolve ();
347 return et.MakeArrayType ();
348 return et.MakeArrayType (rank);
351 protected override bool IsArrayImpl ()
356 public override int GetArrayRank ()
358 return (rank == 0) ? 1 : rank;
361 public override Type BaseType {
362 get { return typeof (System.Array); }
365 protected override TypeAttributes GetAttributeFlagsImpl ()
367 return elementType.Attributes;
370 internal override String FormatName (string elementName)
372 if (elementName == null)
374 StringBuilder sb = new StringBuilder (elementName);
376 for (int i = 1; i < rank; ++i)
381 return sb.ToString ();
385 [StructLayout (LayoutKind.Sequential)]
386 internal class ByRefType : DerivedType
388 internal ByRefType (Type elementType) : base (elementType)
392 internal override Type InternalResolve ()
394 return elementType.InternalResolve ().MakeByRefType ();
397 protected override bool IsByRefImpl ()
402 public override Type BaseType {
403 get { return typeof (Array); }
406 internal override String FormatName (string elementName)
408 if (elementName == null)
410 return elementName + "&";
413 public override Type MakeArrayType ()
415 throw new ArgumentException ("Cannot create an array type of a byref type");
418 public override Type MakeArrayType (int rank)
420 throw new ArgumentException ("Cannot create an array type of a byref type");
423 public override Type MakeByRefType ()
425 throw new ArgumentException ("Cannot create a byref type of an already byref type");
428 public override Type MakePointerType ()
430 throw new ArgumentException ("Cannot create a pointer type of a byref type");
434 [StructLayout (LayoutKind.Sequential)]
435 internal class PointerType : DerivedType
437 internal PointerType (Type elementType) : base (elementType)
441 internal override Type InternalResolve ()
443 return elementType.InternalResolve ().MakePointerType ();
446 protected override bool IsPointerImpl ()
451 public override Type BaseType {
452 get { return typeof(Array); }
455 internal override String FormatName (string elementName)
457 if (elementName == null)
459 return elementName + "*";