3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
9 namespace System.Reflection.Emit
11 using System.Runtime.InteropServices;
13 using System.Reflection;
14 using System.Diagnostics.Contracts;
15 using CultureInfo = System.Globalization.CultureInfo;
18 internal enum TypeKind
25 // This is a kind of Type object that will represent the compound expression of a parameter type or field type.
26 internal sealed class SymbolType : TypeInfo
28 public override bool IsAssignableFrom(System.Reflection.TypeInfo typeInfo){
29 if(typeInfo==null) return false;
30 return IsAssignableFrom(typeInfo.AsType());
33 #region Static Members
34 internal static Type FormCompoundType(char[] bFormat, Type baseType, int curIndex)
36 // This function takes a string to describe the compound type, such as "[,][]", and a baseType.
38 // Example: [2..4] - one dimension array with lower bound 2 and size of 3
39 // Example: [3, 5, 6] - three dimension array with lower bound 3, 5, 6
40 // Example: [-3, ] [] - one dimensional array of two dimensional array (with lower bound -3 for
41 // the first dimension)
42 // Example: []* - pointer to a one dimensional array
43 // Example: *[] - one dimensional array. The element type is a pointer to the baseType
44 // Example: []& - ByRef of a single dimensional array. Only one & is allowed and it must appear the last!
45 // Example: [?] - Array with unknown bound
47 SymbolType symbolType;
51 if (bFormat == null || curIndex == bFormat.Length)
53 // we have consumed all of the format string
60 if (bFormat[curIndex] == '&')
64 symbolType = new SymbolType(TypeKind.IsByRef);
65 symbolType.SetFormat(bFormat, curIndex, 1);
68 if (curIndex != bFormat.Length)
69 // ByRef has to be the last char!!
70 throw new ArgumentException(Environment.GetResourceString("Argument_BadSigFormat"));
72 symbolType.SetElementType(baseType);
76 if (bFormat[curIndex] == '[')
79 symbolType = new SymbolType(TypeKind.IsArray);
80 int startIndex = curIndex;
86 // Example: [2..4] - one dimension array with lower bound 2 and size of 3
87 // Example: [3, 5, 6] - three dimension array with lower bound 3, 5, 6
88 // Example: [-3, ] [] - one dimensional array of two dimensional array (with lower bound -3 sepcified)
90 while (bFormat[curIndex] != ']')
92 if (bFormat[curIndex] == '*')
94 symbolType.m_isSzArray = false;
97 // consume, one dimension at a time
98 if ((bFormat[curIndex] >= '0' && bFormat[curIndex] <= '9') || bFormat[curIndex] == '-')
100 bool isNegative = false;
101 if (bFormat[curIndex] == '-')
107 // lower bound is specified. Consume the low bound
108 while (bFormat[curIndex] >= '0' && bFormat[curIndex] <= '9')
110 iLowerBound = iLowerBound * 10;
111 iLowerBound += bFormat[curIndex] - '0';
117 iLowerBound = 0 - iLowerBound;
120 // set the upper bound to be less than LowerBound to indicate that upper bound it not specified yet!
121 iUpperBound = iLowerBound - 1;
124 if (bFormat[curIndex] == '.')
126 // upper bound is specified
130 if (bFormat[curIndex] != '.')
132 // bad format!! Throw exception
133 throw new ArgumentException(Environment.GetResourceString("Argument_BadSigFormat"));
137 // consume the upper bound
138 if ((bFormat[curIndex] >= '0' && bFormat[curIndex] <= '9') || bFormat[curIndex] == '-')
140 bool isNegative = false;
142 if (bFormat[curIndex] == '-')
148 // lower bound is specified. Consume the low bound
149 while (bFormat[curIndex] >= '0' && bFormat[curIndex] <= '9')
151 iUpperBound = iUpperBound * 10;
152 iUpperBound += bFormat[curIndex] - '0';
157 iUpperBound = 0 - iUpperBound;
159 if (iUpperBound < iLowerBound)
161 // User specified upper bound less than lower bound, this is an error.
162 // Throw error exception.
163 throw new ArgumentException(Environment.GetResourceString("Argument_BadSigFormat"));
168 if (bFormat[curIndex] == ',')
170 // We have more dimension to deal with.
171 // now set the lower bound, the size, and increase the dimension count!
173 symbolType.SetBounds(iLowerBound, iUpperBound);
175 // clear the lower and upper bound information for next dimension
179 else if (bFormat[curIndex] != ']')
181 throw new ArgumentException(Environment.GetResourceString("Argument_BadSigFormat"));
185 // The last dimension information
186 symbolType.SetBounds(iLowerBound, iUpperBound);
191 symbolType.SetFormat(bFormat, startIndex, curIndex - startIndex);
193 // set the base type of array
194 symbolType.SetElementType(baseType);
195 return FormCompoundType(bFormat, symbolType, curIndex);
197 else if (bFormat[curIndex] == '*')
201 symbolType = new SymbolType(TypeKind.IsPointer);
202 symbolType.SetFormat(bFormat, curIndex, 1);
204 symbolType.SetElementType(baseType);
205 return FormCompoundType(bFormat, symbolType, curIndex);
214 internal TypeKind m_typeKind;
215 internal Type m_baseType;
216 internal int m_cRank; // count of dimension
217 // If LowerBound and UpperBound is equal, that means one element.
218 // If UpperBound is less than LowerBound, then the size is not specified.
219 internal int[] m_iaLowerBound;
220 internal int[] m_iaUpperBound; // count of dimension
221 private char[] m_bFormat; // format string to form the full name.
222 private bool m_isSzArray = true;
226 internal SymbolType(TypeKind typeKind)
228 m_typeKind = typeKind;
229 m_iaLowerBound = new int[4];
230 m_iaUpperBound = new int[4];
235 #region Internal Members
236 internal void SetElementType(Type baseType)
238 if (baseType == null)
239 throw new ArgumentNullException("baseType");
240 Contract.EndContractBlock();
242 m_baseType = baseType;
245 private void SetBounds(int lower, int upper)
247 // Increase the rank, set lower and upper bound
249 if (lower != 0 || upper != -1)
252 if (m_iaLowerBound.Length <= m_cRank)
254 // resize the bound array
255 int[] iaTemp = new int[m_cRank * 2];
256 Array.Copy(m_iaLowerBound, iaTemp, m_cRank);
257 m_iaLowerBound = iaTemp;
258 Array.Copy(m_iaUpperBound, iaTemp, m_cRank);
259 m_iaUpperBound = iaTemp;
262 m_iaLowerBound[m_cRank] = lower;
263 m_iaUpperBound[m_cRank] = upper;
267 internal void SetFormat(char[] bFormat, int curIndex, int length)
269 // Cache the text display format for this SymbolType
271 char[] bFormatTemp = new char[length];
272 Array.Copy(bFormat, curIndex, bFormatTemp, 0, length);
273 m_bFormat = bFormatTemp;
277 #region Type Overrides
278 internal override bool IsSzArray
289 public override Type MakePointerType()
291 return SymbolType.FormCompoundType((new String(m_bFormat) + "*").ToCharArray(), m_baseType, 0);
294 public override Type MakeByRefType()
296 return SymbolType.FormCompoundType((new String(m_bFormat) + "&").ToCharArray(), m_baseType, 0);
299 public override Type MakeArrayType()
301 return SymbolType.FormCompoundType((new String(m_bFormat) + "[]").ToCharArray(), m_baseType, 0);
304 public override Type MakeArrayType(int rank)
307 throw new IndexOutOfRangeException();
308 Contract.EndContractBlock();
317 for(int i = 1; i < rank; i++)
321 string s = String.Format(CultureInfo.InvariantCulture, "[{0}]", szrank); // [,,]
322 SymbolType st = SymbolType.FormCompoundType((new String(m_bFormat) + s).ToCharArray(), m_baseType, 0) as SymbolType;
326 public override int GetArrayRank()
329 throw new NotSupportedException(Environment.GetResourceString("NotSupported_SubclassOverride"));
330 Contract.EndContractBlock();
335 public override Guid GUID
337 get { throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType")); }
340 public override Object InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target,
341 Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
343 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
346 public override Module Module
352 for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType) baseType).m_baseType);
354 return baseType.Module;
357 public override Assembly Assembly
363 for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType) baseType).m_baseType);
365 return baseType.Assembly;
369 public override RuntimeTypeHandle TypeHandle
371 get { throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType")); }
374 public override String Name
379 String sFormat = new String(m_bFormat);
381 for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType)
382 sFormat = new String(((SymbolType)baseType).m_bFormat) + sFormat;
384 return baseType.Name + sFormat;
388 public override String FullName
392 return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.FullName);
396 public override String AssemblyQualifiedName
400 return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.AssemblyQualifiedName);
404 public override String ToString()
406 return TypeNameBuilder.ToString(this, TypeNameBuilder.Format.ToString);
409 public override String Namespace
411 get { return m_baseType.Namespace; }
414 public override Type BaseType
417 get { return typeof(System.Array); }
420 protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr,Binder binder,
421 CallingConventions callConvention, Type[] types,ParameterModifier[] modifiers)
423 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
426 [System.Runtime.InteropServices.ComVisible(true)]
427 public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
429 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
432 protected override MethodInfo GetMethodImpl(String name,BindingFlags bindingAttr,Binder binder,
433 CallingConventions callConvention, Type[] types,ParameterModifier[] modifiers)
435 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
438 public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
440 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
443 public override FieldInfo GetField(String name, BindingFlags bindingAttr)
445 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
448 public override FieldInfo[] GetFields(BindingFlags bindingAttr)
450 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
453 public override Type GetInterface(String name,bool ignoreCase)
455 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
458 public override Type[] GetInterfaces()
460 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
463 public override EventInfo GetEvent(String name,BindingFlags bindingAttr)
465 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
468 public override EventInfo[] GetEvents()
470 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
473 protected override PropertyInfo GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder,
474 Type returnType, Type[] types, ParameterModifier[] modifiers)
476 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
479 public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
481 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
484 public override Type[] GetNestedTypes(BindingFlags bindingAttr)
486 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
489 public override Type GetNestedType(String name, BindingFlags bindingAttr)
491 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
494 public override MemberInfo[] GetMember(String name, MemberTypes type, BindingFlags bindingAttr)
496 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
499 public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
501 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
504 [System.Runtime.InteropServices.ComVisible(true)]
505 public override InterfaceMapping GetInterfaceMap(Type interfaceType)
507 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
510 public override EventInfo[] GetEvents(BindingFlags bindingAttr)
512 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
515 protected override TypeAttributes GetAttributeFlagsImpl()
517 // Return the attribute flags of the base type?
519 for (baseType = m_baseType; baseType is SymbolType; baseType = ((SymbolType)baseType).m_baseType);
520 return baseType.Attributes;
523 protected override bool IsArrayImpl()
525 return m_typeKind == TypeKind.IsArray;
528 protected override bool IsPointerImpl()
530 return m_typeKind == TypeKind.IsPointer;
533 protected override bool IsByRefImpl()
535 return m_typeKind == TypeKind.IsByRef;
538 protected override bool IsPrimitiveImpl()
543 protected override bool IsValueTypeImpl()
548 protected override bool IsCOMObjectImpl()
553 public override bool IsConstructedGenericType
561 public override Type GetElementType()
566 protected override bool HasElementTypeImpl()
568 return m_baseType != null;
571 public override Type UnderlyingSystemType
577 public override Object[] GetCustomAttributes(bool inherit)
579 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
582 public override Object[] GetCustomAttributes(Type attributeType, bool inherit)
584 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));
587 public override bool IsDefined (Type attributeType, bool inherit)
589 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonReflectedType"));