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.
30 using System.Reflection;
31 using System.Reflection.Emit;
32 using System.Collections;
33 using System.Runtime.CompilerServices;
34 using System.Globalization;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
40 namespace System.Reflection.Emit
42 internal enum TypeKind : int {
47 [StructLayout (LayoutKind.Sequential)]
48 internal abstract class DerivedType : Type
50 internal Type elementType;
52 [MethodImplAttribute(MethodImplOptions.InternalCall)]
53 internal static extern void create_unmanaged_type (Type type);
55 internal DerivedType (Type elementType)
57 this.elementType = elementType;
60 internal abstract String FormatName (string elementName);
62 public override Type GetInterface (string name, bool ignoreCase)
64 throw new NotSupportedException ();
67 public override Type[] GetInterfaces ()
69 throw new NotSupportedException ();
72 public override Type GetElementType ()
77 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
79 throw new NotSupportedException ();
82 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
84 throw new NotSupportedException ();
87 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
89 throw new NotSupportedException ();
92 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
94 throw new NotSupportedException ();
97 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
99 throw new NotSupportedException ();
102 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
103 CallingConventions callConvention, Type[] types,
104 ParameterModifier[] modifiers)
106 throw new NotSupportedException ();
109 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
111 throw new NotSupportedException ();
114 public override Type GetNestedType (string name, BindingFlags bindingAttr)
116 throw new NotSupportedException ();
119 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
121 throw new NotSupportedException ();
124 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
126 throw new NotSupportedException ();
129 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
130 Type returnType, Type[] types, ParameterModifier[] modifiers)
132 throw new NotSupportedException ();
135 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
137 CallingConventions callConvention,
139 ParameterModifier[] modifiers)
141 throw new NotSupportedException ();
145 protected override TypeAttributes GetAttributeFlagsImpl ()
147 /*LAMEIMPL MS just return the elementType.Attributes*/
148 return elementType.Attributes;
151 protected override bool HasElementTypeImpl ()
156 protected override bool IsArrayImpl ()
161 protected override bool IsByRefImpl ()
166 protected override bool IsCOMObjectImpl ()
171 protected override bool IsPointerImpl ()
176 protected override bool IsPrimitiveImpl ()
182 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
184 throw new NotSupportedException ();
187 public override object InvokeMember (string name, BindingFlags invokeAttr,
188 Binder binder, object target, object[] args,
189 ParameterModifier[] modifiers,
190 CultureInfo culture, string[] namedParameters)
192 throw new NotSupportedException ();
195 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
197 throw new NotSupportedException ();
200 public override bool IsInstanceOfType (object o)
205 public override bool IsAssignableFrom (Type c)
210 public override bool ContainsGenericParameters {
211 get { return elementType.ContainsGenericParameters; }
214 //FIXME this should be handled by System.Type
215 public override Type MakeGenericType (params Type[] typeArguments)
217 throw new NotSupportedException ();
220 public override Type MakeArrayType ()
222 return new ArrayType (this, 0);
225 public override Type MakeArrayType (int rank)
228 throw new IndexOutOfRangeException ();
229 return new ArrayType (this, rank);
232 public override Type MakeByRefType ()
234 return new ByRefType (this);
237 public override Type MakePointerType ()
239 return new PointerType (this);
242 public override string ToString ()
244 return FormatName (elementType.ToString ());
247 public override GenericParameterAttributes GenericParameterAttributes {
248 get { throw new NotSupportedException (); }
251 public override StructLayoutAttribute StructLayoutAttribute {
252 get { throw new NotSupportedException (); }
255 public override Assembly Assembly {
256 get { return elementType.Assembly; }
259 public override string AssemblyQualifiedName {
261 string fullName = FormatName (elementType.FullName);
262 if (fullName == null)
264 return fullName + ", " + elementType.Assembly.FullName;
269 public override string FullName {
271 return FormatName (elementType.FullName);
275 public override string Name {
277 return FormatName (elementType.Name);
281 public override Guid GUID {
282 get { throw new NotSupportedException (); }
285 public override Module Module {
286 get { return elementType.Module; }
289 public override string Namespace {
290 get { return elementType.Namespace; }
293 public override RuntimeTypeHandle TypeHandle {
294 get { throw new NotSupportedException (); }
297 public override Type UnderlyingSystemType {
299 create_unmanaged_type (this);
305 public override bool IsDefined (Type attributeType, bool inherit)
307 throw new NotSupportedException ();
310 public override object [] GetCustomAttributes (bool inherit)
312 throw new NotSupportedException ();
315 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
317 throw new NotSupportedException ();
320 internal override bool IsUserType {
322 return elementType.IsUserType;
327 [StructLayout (LayoutKind.Sequential)]
328 internal class ArrayType : DerivedType
332 internal ArrayType (Type elementType, int rank) : base (elementType)
337 internal int GetEffectiveRank ()
342 internal override Type InternalResolve ()
344 Type et = elementType.InternalResolve ();
346 return et.MakeArrayType ();
347 return et.MakeArrayType (rank);
350 protected override bool IsArrayImpl ()
355 public override int GetArrayRank ()
357 return (rank == 0) ? 1 : rank;
360 public override Type BaseType {
361 get { return typeof (System.Array); }
364 protected override TypeAttributes GetAttributeFlagsImpl ()
366 return elementType.Attributes;
369 internal override String FormatName (string elementName)
371 if (elementName == null)
373 StringBuilder sb = new StringBuilder (elementName);
375 for (int i = 1; i < rank; ++i)
380 return sb.ToString ();
384 [StructLayout (LayoutKind.Sequential)]
385 internal class ByRefType : DerivedType
387 internal ByRefType (Type elementType) : base (elementType)
391 internal override Type InternalResolve ()
393 return elementType.InternalResolve ().MakeByRefType ();
396 protected override bool IsByRefImpl ()
401 public override Type BaseType {
402 get { return typeof (Array); }
405 internal override String FormatName (string elementName)
407 if (elementName == null)
409 return elementName + "&";
412 public override Type MakeArrayType ()
414 throw new ArgumentException ("Cannot create an array type of a byref type");
417 public override Type MakeArrayType (int rank)
419 throw new ArgumentException ("Cannot create an array type of a byref type");
422 public override Type MakeByRefType ()
424 throw new ArgumentException ("Cannot create a byref type of an already byref type");
427 public override Type MakePointerType ()
429 throw new ArgumentException ("Cannot create a pointer type of a byref type");
433 [StructLayout (LayoutKind.Sequential)]
434 internal class PointerType : DerivedType
436 internal PointerType (Type elementType) : base (elementType)
440 internal override Type InternalResolve ()
442 return elementType.InternalResolve ().MakePointerType ();
445 protected override bool IsPointerImpl ()
450 public override Type BaseType {
451 get { return typeof(Array); }
454 internal override String FormatName (string elementName)
456 if (elementName == null)
458 return elementName + "*";