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 internal abstract class DerivedType : Type
49 internal Type elementType;
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 internal static extern void create_unmanaged_type (Type type);
54 internal DerivedType (Type elementType)
56 this.elementType = elementType;
59 internal abstract String FormatName (string elementName);
61 internal override bool IsCompilerContext {
63 return elementType.IsCompilerContext;
67 public override Type GetInterface (string name, bool ignoreCase)
69 throw new NotSupportedException ();
72 public override Type[] GetInterfaces ()
74 throw new NotSupportedException ();
77 public override Type GetElementType ()
82 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
84 throw new NotSupportedException ();
87 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
89 throw new NotSupportedException ();
92 public override FieldInfo GetField( string name, BindingFlags bindingAttr)
94 throw new NotSupportedException ();
97 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
99 throw new NotSupportedException ();
102 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
104 throw new NotSupportedException ();
107 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
108 CallingConventions callConvention, Type[] types,
109 ParameterModifier[] modifiers)
111 throw new NotSupportedException ();
114 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
116 throw new NotSupportedException ();
119 public override Type GetNestedType (string name, BindingFlags bindingAttr)
121 throw new NotSupportedException ();
124 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
126 throw new NotSupportedException ();
129 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
131 throw new NotSupportedException ();
134 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
135 Type returnType, Type[] types, ParameterModifier[] modifiers)
137 throw new NotSupportedException ();
140 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
142 CallingConventions callConvention,
144 ParameterModifier[] modifiers)
146 throw new NotSupportedException ();
150 protected override TypeAttributes GetAttributeFlagsImpl ()
152 /*LAMEIMPL MS just return the elementType.Attributes*/
153 return elementType.Attributes;
156 protected override bool HasElementTypeImpl ()
161 protected override bool IsArrayImpl ()
166 protected override bool IsByRefImpl ()
171 protected override bool IsCOMObjectImpl ()
176 protected override bool IsPointerImpl ()
181 protected override bool IsPrimitiveImpl ()
187 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
189 throw new NotSupportedException ();
192 public override object InvokeMember (string name, BindingFlags invokeAttr,
193 Binder binder, object target, object[] args,
194 ParameterModifier[] modifiers,
195 CultureInfo culture, string[] namedParameters)
197 throw new NotSupportedException ();
200 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
202 throw new NotSupportedException ();
205 public override bool IsInstanceOfType (object o)
210 public override bool IsAssignableFrom (Type c)
215 public override bool ContainsGenericParameters {
216 get { return elementType.ContainsGenericParameters; }
219 //FIXME this should be handled by System.Type
220 public override Type MakeGenericType (params Type[] typeArguments)
222 throw new NotSupportedException ();
225 public override Type MakeArrayType ()
227 return new ArrayType (this, 0);
230 public override Type MakeArrayType (int rank)
233 throw new IndexOutOfRangeException ();
234 return new ArrayType (this, rank);
237 public override Type MakeByRefType ()
239 return new ByRefType (this);
242 public override Type MakePointerType ()
244 return new PointerType (this);
247 public override string ToString ()
249 return FormatName (elementType.ToString ());
252 public override GenericParameterAttributes GenericParameterAttributes {
253 get { throw new NotSupportedException (); }
256 public override StructLayoutAttribute StructLayoutAttribute {
257 get { throw new NotSupportedException (); }
260 public override Assembly Assembly {
261 get { return elementType.Assembly; }
264 public override string AssemblyQualifiedName {
266 string fullName = FormatName (elementType.FullName);
267 if (fullName == null)
269 return fullName + ", " + elementType.Assembly.FullName;
274 public override string FullName {
276 return FormatName (elementType.FullName);
280 public override string Name {
282 return FormatName (elementType.Name);
286 public override Guid GUID {
287 get { throw new NotSupportedException (); }
290 public override Module Module {
291 get { return elementType.Module; }
294 public override string Namespace {
295 get { return elementType.Namespace; }
298 public override RuntimeTypeHandle TypeHandle {
299 get { throw new NotSupportedException (); }
302 public override Type UnderlyingSystemType {
304 create_unmanaged_type (this);
310 public override bool IsDefined (Type attributeType, bool inherit)
312 throw new NotSupportedException ();
315 public override object [] GetCustomAttributes (bool inherit)
317 throw new NotSupportedException ();
320 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
322 throw new NotSupportedException ();
326 internal class ArrayType : DerivedType
330 internal ArrayType (Type elementType, int rank) : base (elementType)
335 internal int GetEffectiveRank ()
340 internal override Type InternalResolve ()
342 Type et = elementType.InternalResolve ();
344 return et.MakeArrayType ();
345 return et.MakeArrayType (rank);
348 protected override bool IsArrayImpl ()
353 public override int GetArrayRank ()
355 return (rank == 0) ? 1 : rank;
358 public override Type BaseType {
359 get { return typeof (System.Array); }
362 protected override TypeAttributes GetAttributeFlagsImpl ()
364 if (IsCompilerContext)
365 return (elementType.Attributes & TypeAttributes.VisibilityMask) | TypeAttributes.Sealed | TypeAttributes.Serializable;
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 ();
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");
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 + "*";