public Type GetType(string name, bool throwOnError)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     this.EnsureCurrentTypes();
     bool flag = false;
     Type type = null;
     string typeName = string.Empty;
     string[] parameters = null;
     string elemantDecorator = string.Empty;
     if (ParseHelpers.ParseTypeName(name, ParseHelpers.ParseTypeNameLanguage.NetFramework, out typeName, out parameters, out elemantDecorator))
     {
         if ((parameters != null) && (parameters.Length > 0))
         {
             Type type2 = this.GetType(typeName, throwOnError);
             if ((type2 == null) || !type2.IsGenericTypeDefinition)
             {
                 return null;
             }
             Type[] typeArguments = new Type[parameters.Length];
             for (int i = 0; i < parameters.Length; i++)
             {
                 Type type3 = this.GetType(parameters[i], throwOnError);
                 if (type3 == null)
                 {
                     return null;
                 }
                 typeArguments[i] = type3;
             }
             return type2.MakeGenericType(typeArguments);
         }
         if (elemantDecorator != string.Empty)
         {
             Type type4 = this.GetType(typeName);
             if (type4 != null)
             {
                 StringBuilder builder = new StringBuilder(type4.FullName);
                 for (int j = 0; j < elemantDecorator.Length; j++)
                 {
                     if (elemantDecorator[j] != ' ')
                     {
                         builder.Append(elemantDecorator[j]);
                     }
                 }
                 name = builder.ToString();
                 if (type4.Assembly != null)
                 {
                     type = type4.Assembly.GetType(name, false);
                 }
                 if (type == null)
                 {
                     if (this.hashOfDTTypes.Contains(name))
                     {
                         return (this.hashOfDTTypes[name] as Type);
                     }
                     type = new DesignTimeType(null, name, this);
                     this.hashOfDTTypes.Add(name, type);
                     return type;
                 }
             }
         }
         else
         {
             string thatName = string.Empty;
             int index = name.IndexOf(',');
             if (index != -1)
             {
                 typeName = name.Substring(0, index);
                 thatName = name.Substring(index + 1).Trim();
             }
             typeName = typeName.Trim();
             if (typeName.Length > 0)
             {
                 type = this.designTimeTypes[typeName] as Type;
                 if (type == null)
                 {
                     foreach (DictionaryEntry entry in this.rawAssemblyLoaders)
                     {
                         AssemblyLoader loader = entry.Value as AssemblyLoader;
                         if ((thatName.Length == 0) || ParseHelpers.AssemblyNameEquals(loader.AssemblyName, thatName))
                         {
                             try
                             {
                                 type = loader.GetType(typeName);
                             }
                             catch (Exception exception)
                             {
                                 if (!this.typeLoadErrors.Contains(entry.Key))
                                 {
                                     this.typeLoadErrors[entry.Key] = exception;
                                     flag = true;
                                 }
                                 if (throwOnError)
                                 {
                                     throw exception;
                                 }
                             }
                             if (type != null)
                             {
                                 break;
                             }
                         }
                     }
                 }
                 if (type == null)
                 {
                     foreach (DictionaryEntry entry2 in this.assemblyLoaders)
                     {
                         AssemblyLoader loader2 = entry2.Value as AssemblyLoader;
                         if ((thatName.Length == 0) || ParseHelpers.AssemblyNameEquals(loader2.AssemblyName, thatName))
                         {
                             try
                             {
                                 type = loader2.GetType(typeName);
                             }
                             catch (Exception exception2)
                             {
                                 if (!this.typeLoadErrors.Contains(entry2.Key))
                                 {
                                     this.typeLoadErrors[entry2.Key] = exception2;
                                     flag = true;
                                 }
                                 if (throwOnError)
                                 {
                                     throw exception2;
                                 }
                             }
                             if (type != null)
                             {
                                 break;
                             }
                         }
                     }
                 }
                 if (flag && (this.TypeLoadErrorsChanged != null))
                 {
                     FireEventsNoThrow(this.TypeLoadErrorsChanged, new object[] { this, EventArgs.Empty });
                 }
                 if (((type == null) && (this.localAssembly != null)) && (thatName == this.localAssembly.FullName))
                 {
                     type = this.localAssembly.GetType(typeName);
                 }
             }
         }
     }
     if (type == null)
     {
         if (throwOnError)
         {
             throw new Exception(TypeSystemSR.GetString(CultureInfo.CurrentCulture, "Error_TypeResolution", new object[] { name }));
         }
         return null;
     }
     if (((this.designTimeTypes == null) || (this.designTimeTypes.Count <= 0)) || ((type.Assembly == null) || !type.IsGenericTypeDefinition))
     {
         return type;
     }
     if (this.hashOfRTTypes.Contains(type))
     {
         return (Type) this.hashOfRTTypes[type];
     }
     Type type5 = new RTTypeWrapper(this, type);
     this.hashOfRTTypes.Add(type, type5);
     return type5;
 }
 public RTPropertyInfoWrapper(RTTypeWrapper rtTypeWrapper, PropertyInfo propertyInfo)
 {
     this.rtTypeWrapper = rtTypeWrapper;
     this.propertyInfo = propertyInfo;
 }
Beispiel #3
0
        public Type GetType(string name, bool throwOnError)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            EnsureCurrentTypes();

            bool hasTypeLoadErrors = false;
            Type returnType = null;
            string typeName = string.Empty;
            string[] parameters = null;
            string elementDecorator = string.Empty;

            if (ParseHelpers.ParseTypeName(name, ParseHelpers.ParseTypeNameLanguage.NetFramework, out typeName, out parameters, out elementDecorator))
            {
                if ((parameters != null) && (parameters.Length > 0))
                {
                    //Generic type
                    Type templateType = GetType(typeName, throwOnError);
                    if ((templateType == null) || (!templateType.IsGenericTypeDefinition))
                        return null;
                    Type[] templateParamTypes = new Type[parameters.Length];
                    for (int index = 0; index < parameters.Length; index++)
                    {
                        Type templateParameter = GetType(parameters[index], throwOnError);
                        if (templateParameter == null)
                            return null;
                        templateParamTypes[index] = templateParameter;
                    }
                    return templateType.MakeGenericType(templateParamTypes);
                }
                else if (elementDecorator != string.Empty)
                {
                    //type with element (Array, ByRef, Pointer)
                    Type elementType = this.GetType(typeName);
                    if (elementType != null)
                    {
                        // first we verify the name is formated well (AssemblyQualifiedName for generic
                        // parameters + no spaces in array brackets)
                        System.Text.StringBuilder nameBuilder = new System.Text.StringBuilder(elementType.FullName);
                        for (int loop = 0; loop < elementDecorator.Length; loop++)
                            if (elementDecorator[loop] != ' ')
                                nameBuilder.Append(elementDecorator[loop]);

                        name = nameBuilder.ToString();

                        // let tha assembly of the element type a chance to find a type (will fail only
                        // if element contains parameter from external assembly
                        if (elementType.Assembly != null)
                            returnType = elementType.Assembly.GetType(name, false);

                        if (returnType == null)
                        {
                            // now we can fetch or create the type
                            if (this.hashOfDTTypes.Contains(name))
                            {
                                returnType = this.hashOfDTTypes[name] as Type;
                            }
                            else
                            {
                                returnType = new DesignTimeType(null, name, this);
                                this.hashOfDTTypes.Add(name, returnType);
                            }
                            return returnType;
                        }
                    }
                }
                else
                {
                    // regular type, get the type name
                    string assemblyName = string.Empty;
                    int indexOfComma = name.IndexOf(',');
                    if (indexOfComma != -1)
                    {
                        typeName = name.Substring(0, indexOfComma);
                        assemblyName = name.Substring(indexOfComma + 1).Trim();
                    }
                    typeName = typeName.Trim();
                    if (typeName.Length > 0)
                    {
                        returnType = this.designTimeTypes[typeName] as Type;
                        if (returnType == null)
                        {
                            foreach (DictionaryEntry dictionaryEntry in this.rawAssemblyLoaders)
                            {
                                AssemblyLoader assemblyLoader = dictionaryEntry.Value as AssemblyLoader;
                                if ((assemblyName.Length == 0) || (ParseHelpers.AssemblyNameEquals(assemblyLoader.AssemblyName, assemblyName)))
                                {
                                    try
                                    {
                                        returnType = assemblyLoader.GetType(typeName);
                                    }
                                    catch (Exception e)
                                    {
                                        if (!this.typeLoadErrors.Contains(dictionaryEntry.Key))
                                        {
                                            this.typeLoadErrors[dictionaryEntry.Key] = e;
                                            hasTypeLoadErrors = true;
                                        }
                                        // bubble up exceptions only when appropiate 
                                        if (throwOnError)
                                            throw e;
                                    }
                                    if (returnType != null)
                                        break;
                                }
                            }
                        }

                        if (returnType == null)
                        {
                            foreach (DictionaryEntry dictionaryEntry in this.assemblyLoaders)
                            {
                                AssemblyLoader assemblyLoader = dictionaryEntry.Value as AssemblyLoader;
                                if ((assemblyName.Length == 0) || (ParseHelpers.AssemblyNameEquals(assemblyLoader.AssemblyName, assemblyName)))
                                {
                                    try
                                    {
                                        returnType = assemblyLoader.GetType(typeName);
                                    }
                                    catch (Exception e)
                                    {
                                        if (!this.typeLoadErrors.Contains(dictionaryEntry.Key))
                                        {
                                            this.typeLoadErrors[dictionaryEntry.Key] = e;
                                            hasTypeLoadErrors = true;
                                        }
                                        // bubble up exceptions only when appropiate 
                                        if (throwOnError)
                                            throw e;
                                    }
                                    if (returnType != null)
                                        break;
                                }
                            }
                        }

                        if (hasTypeLoadErrors)
                        {
                            if (this.TypeLoadErrorsChanged != null)
                                FireEventsNoThrow(this.TypeLoadErrorsChanged, new object[] { this, EventArgs.Empty });
                        }

                        if (returnType == null && this.localAssembly != null && assemblyName == this.localAssembly.FullName)
                            returnType = this.localAssembly.GetType(typeName);
                    }
                }
            }

            if (returnType == null)
            {
                if (throwOnError)
                    throw new Exception(TypeSystemSR.GetString(CultureInfo.CurrentCulture, "Error_TypeResolution", name));
                else
                    return null;
            }

            // replace the System.Type with RTTypeWrapper for generic types.
            // WinOE 





            if (this.designTimeTypes != null && this.designTimeTypes.Count > 0 && returnType.Assembly != null && returnType.IsGenericTypeDefinition)
            {
                if (this.hashOfRTTypes.Contains(returnType))
                {
                    returnType = (Type)this.hashOfRTTypes[returnType];
                }
                else
                {
                    Type returnType2 = new RTTypeWrapper(this, returnType);
                    this.hashOfRTTypes.Add(returnType, returnType2);
                    returnType = returnType2;
                }
            }
            return returnType;
        }
 public RTConstructorInfoWrapper(RTTypeWrapper rtTypeWrapper, ConstructorInfo ctorInfo)
 {
     this.rtTypeWrapper = rtTypeWrapper;
     this.ctorInfo = ctorInfo;
 }
 public RTFieldInfoWrapper(RTTypeWrapper rtTypeWrapper, FieldInfo fieldInfo)
 {
     this.rtTypeWrapper = rtTypeWrapper;
     this.fieldInfo = fieldInfo;
 }
        public override Type MakeGenericType(params Type[] typeArgs)
        {
            if (typeArgs == null)
                throw new ArgumentNullException("typeArgs");

            Type[] types = new Type[typeArgs.Length];

            if (!IsGenericTypeDefinition)
                throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "typeArgs");

            for (int i = 0; i < typeArgs.Length; i++)
            {
                types[i] = typeArgs[i];
                if (types[i] == null)
                    throw new ArgumentException(SR.GetString(SR.Error_InvalidArgumentValue), "typeArgs");
            }

            Type returnType = this.boundedTypes[typeArgs] as Type;
            if (returnType == null)
            {
                // handle Nullable<T> specially
                if ((typeArgs.Length == 1) && (this.runtimeType == typeof(Nullable<>)) && !(typeArgs[0].IsEnum))
                {
                    switch (Type.GetTypeCode(typeArgs[0]))
                    {
                        case TypeCode.Boolean:
                            returnType = typeof(bool?);
                            break;
                        case TypeCode.Byte:
                            returnType = typeof(byte?);
                            break;
                        case TypeCode.Char:
                            returnType = typeof(char?);
                            break;
                        case TypeCode.DateTime:
                            returnType = typeof(DateTime?);
                            break;
                        case TypeCode.Decimal:
                            returnType = typeof(decimal?);
                            break;
                        case TypeCode.Double:
                            returnType = typeof(double?);
                            break;
                        case TypeCode.Int16:
                            returnType = typeof(short?);
                            break;
                        case TypeCode.Int32:
                            returnType = typeof(int?);
                            break;
                        case TypeCode.Int64:
                            returnType = typeof(long?);
                            break;
                        case TypeCode.SByte:
                            returnType = typeof(sbyte?);
                            break;
                        case TypeCode.Single:
                            returnType = typeof(float?);
                            break;
                        case TypeCode.UInt16:
                            returnType = typeof(ushort?);
                            break;
                        case TypeCode.UInt32:
                            returnType = typeof(uint?);
                            break;
                        case TypeCode.UInt64:
                            returnType = typeof(ulong?);
                            break;
                        default:
                            // no special handling, so make it as usual
                            returnType = new RTTypeWrapper(this.typeProvider, this.runtimeType, typeArgs);
                            break;
                    }
                }
                else
                {
                    returnType = new RTTypeWrapper(this.typeProvider, this.runtimeType, typeArgs);
                }
                this.boundedTypes[typeArgs] = returnType;
            }
            return returnType;
        }
 public RTParameterInfoWrapper(RTTypeWrapper rtTypeWrapper, MemberInfo parentMember, ParameterInfo paramInfo)
 {
     this.parentMember = parentMember;
     this.rtTypeWrapper = rtTypeWrapper;
     this.paramInfo = paramInfo;
 }
 public RTEventInfoWrapper(RTTypeWrapper rtTypeWrapper, EventInfo eventInfo)
 {
     this.rtTypeWrapper = rtTypeWrapper;
     this.eventInfo = eventInfo;
 }
 public RTMethodInfoWrapper(RTTypeWrapper rtTypeWrapper, MethodInfo methodInfo)
 {
     this.rtTypeWrapper = rtTypeWrapper;
     this.methodInfo = methodInfo;
 }
        public override Type MakeGenericType(params Type[] typeArgs)
        {
            if (typeArgs == null)
            {
                throw new ArgumentNullException("typeArgs");
            }
            Type[] typeArray = new Type[typeArgs.Length];
            if (!this.IsGenericTypeDefinition)
            {
                throw new ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "typeArgs");
            }
            for (int i = 0; i < typeArgs.Length; i++)
            {
                typeArray[i] = typeArgs[i];
                if (typeArray[i] == null)
                {
                    throw new ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "typeArgs");
                }
            }
            Type type = this.boundedTypes[typeArgs] as Type;
            if (type != null)
            {
                return type;
            }
            if (((typeArgs.Length == 1) && (this.runtimeType == typeof(Nullable<>))) && !typeArgs[0].IsEnum)
            {
                switch (Type.GetTypeCode(typeArgs[0]))
                {
                    case TypeCode.Boolean:
                        type = typeof(bool?);
                        goto Label_01F7;

                    case TypeCode.Char:
                        type = typeof(char?);
                        goto Label_01F7;

                    case TypeCode.SByte:
                        type = typeof(sbyte?);
                        goto Label_01F7;

                    case TypeCode.Byte:
                        type = typeof(byte?);
                        goto Label_01F7;

                    case TypeCode.Int16:
                        type = typeof(short?);
                        goto Label_01F7;

                    case TypeCode.UInt16:
                        type = typeof(ushort?);
                        goto Label_01F7;

                    case TypeCode.Int32:
                        type = typeof(int?);
                        goto Label_01F7;

                    case TypeCode.UInt32:
                        type = typeof(uint?);
                        goto Label_01F7;

                    case TypeCode.Int64:
                        type = typeof(long?);
                        goto Label_01F7;

                    case TypeCode.UInt64:
                        type = typeof(ulong?);
                        goto Label_01F7;

                    case TypeCode.Single:
                        type = typeof(float?);
                        goto Label_01F7;

                    case TypeCode.Double:
                        type = typeof(double?);
                        goto Label_01F7;

                    case TypeCode.Decimal:
                        type = typeof(decimal?);
                        goto Label_01F7;

                    case TypeCode.DateTime:
                        type = typeof(DateTime?);
                        goto Label_01F7;
                }
                type = new RTTypeWrapper(this.typeProvider, this.runtimeType, typeArgs);
            }
            else
            {
                type = new RTTypeWrapper(this.typeProvider, this.runtimeType, typeArgs);
            }
        Label_01F7:
            this.boundedTypes[typeArgs] = type;
            return type;
        }