public Type DefineType(Type type)
        {
            Type type1;
            Type fromCache = ControlEditingDesignTypeGenerator.GetFromCache(type);

            if (fromCache != null)
            {
                return(fromCache);
            }
            if (type == null || type.IsArray || type.IsGenericType || type.IsInterface || type.IsNested || !PlatformTypes.Control.IsAssignableFrom(this.typeResolver.GetType(type)))
            {
                return(type);
            }
            try
            {
                Type            baseType    = type;
                ConstructorInfo constructor = null;
                while (baseType != null)
                {
                    if (!baseType.IsSealed)
                    {
                        constructor = baseType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
                        if (constructor != null && (constructor.IsPublic || constructor.IsFamily))
                        {
                            break;
                        }
                    }
                    baseType = baseType.BaseType;
                }
                TypeAttributes attributes = TypeAttributes.Public | type.Attributes & (TypeAttributes.VisibilityMask | TypeAttributes.Public | TypeAttributes.NestedPublic | TypeAttributes.NestedPrivate | TypeAttributes.NestedFamily | TypeAttributes.NestedAssembly | TypeAttributes.NestedFamANDAssem | TypeAttributes.NestedFamORAssem | TypeAttributes.LayoutMask | TypeAttributes.SequentialLayout | TypeAttributes.ExplicitLayout | TypeAttributes.ClassSemanticsMask | TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.SpecialName | TypeAttributes.Import | TypeAttributes.Serializable | TypeAttributes.WindowsRuntime | TypeAttributes.StringFormatMask | TypeAttributes.UnicodeClass | TypeAttributes.AutoClass | TypeAttributes.CustomFormatClass | TypeAttributes.CustomFormatMask | TypeAttributes.BeforeFieldInit) & (TypeAttributes.VisibilityMask | TypeAttributes.Public | TypeAttributes.NestedPublic | TypeAttributes.NestedPrivate | TypeAttributes.NestedFamily | TypeAttributes.NestedAssembly | TypeAttributes.NestedFamANDAssem | TypeAttributes.NestedFamORAssem | TypeAttributes.LayoutMask | TypeAttributes.SequentialLayout | TypeAttributes.ExplicitLayout | TypeAttributes.ClassSemanticsMask | TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.SpecialName | TypeAttributes.Import | TypeAttributes.Serializable | TypeAttributes.WindowsRuntime | TypeAttributes.StringFormatMask | TypeAttributes.UnicodeClass | TypeAttributes.AutoClass | TypeAttributes.CustomFormatClass | TypeAttributes.CustomFormatMask | TypeAttributes.BeforeFieldInit | TypeAttributes.ReservedMask | TypeAttributes.RTSpecialName | TypeAttributes.HasSecurity) & (TypeAttributes.VisibilityMask | TypeAttributes.Public | TypeAttributes.NestedPublic | TypeAttributes.NestedPrivate | TypeAttributes.NestedFamily | TypeAttributes.NestedAssembly | TypeAttributes.NestedFamANDAssem | TypeAttributes.NestedFamORAssem | TypeAttributes.LayoutMask | TypeAttributes.SequentialLayout | TypeAttributes.ExplicitLayout | TypeAttributes.ClassSemanticsMask | TypeAttributes.Interface | TypeAttributes.Sealed | TypeAttributes.SpecialName | TypeAttributes.Import | TypeAttributes.Serializable | TypeAttributes.WindowsRuntime | TypeAttributes.StringFormatMask | TypeAttributes.UnicodeClass | TypeAttributes.AutoClass | TypeAttributes.CustomFormatClass | TypeAttributes.CustomFormatMask | TypeAttributes.BeforeFieldInit | TypeAttributes.ReservedMask | TypeAttributes.RTSpecialName | TypeAttributes.HasSecurity);
                string         str        = this.CreateUniqueTypeName(type);
                RuntimeGeneratedTypesHelper.EnsureControlEditingDesignTypeAssembly(this.typeResolver.PlatformMetadata as IPlatformTypes);
                TypeBuilder      typeBuilder      = RuntimeGeneratedTypesHelper.ControlEditingDesignTypeAssembly.DefineType(str, attributes, baseType);
                BuildingTypeInfo buildingTypeInfo = new BuildingTypeInfo()
                {
                    SourceType  = type,
                    DesignType  = typeBuilder,
                    Constructor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes)
                };
                ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, Type.EmptyTypes);
                buildingTypeInfo.IsReplacement = true;
                buildingTypeInfo.BaseTypeInfo  = new BuildingTypeInfo()
                {
                    SourceType    = baseType,
                    DesignType    = baseType,
                    IsReplacement = false
                };
                DesignTypeGenerator.DefineAbstractMethods(buildingTypeInfo);
                ILGenerator lGenerator = ((ConstructorBuilder)buildingTypeInfo.Constructor).GetILGenerator();
                lGenerator.Emit(OpCodes.Ldarg_0);
                lGenerator.Emit(OpCodes.Call, constructor);
                ILGenerator    lGenerator1 = constructorBuilder.GetILGenerator();
                BindingFlags   bindingFlag = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
                PropertyInfo[] properties  = type.GetProperties(bindingFlag);
                for (int i = 0; i < (int)properties.Length; i++)
                {
                    PropertyInfo propertyInfo = properties[i];
                    PropertyInfo property     = baseType.GetProperty(propertyInfo.Name, bindingFlag);
                    if (!(property != null) || !(property.PropertyType == propertyInfo.PropertyType))
                    {
                        this.DefineProperty(propertyInfo, typeBuilder, type, lGenerator1);
                    }
                }
                lGenerator1.Emit(OpCodes.Ret);
                lGenerator.Emit(OpCodes.Ret);
                Type type2 = typeBuilder.CreateType();
                ControlEditingDesignTypeGenerator.AddToCache(type, type2);
                type1 = type2;
            }
            catch (Exception exception)
            {
                type1 = type;
            }
            return(type1);
        }
Exemplo n.º 2
0
        private Type GetRuntimeType(ITypeResolver typeResolver, ProjectContextType typeId)
        {
            Type type;

            if (RuntimeGeneratedTypesHelper.IsControlEditingAssembly(typeId.assembly))
            {
                Type sourceType = ControlEditingDesignTypeGenerator.GetSourceType(typeId.RuntimeType);
                if (sourceType != null)
                {
                    ProjectContextType projectContextType = typeResolver.GetType(sourceType) as ProjectContextType;
                    if (projectContextType != null)
                    {
                        Type runtimeType = projectContextType.GetRuntimeType();
                        if (runtimeType != sourceType && runtimeType != null)
                        {
                            Type type1 = (new ControlEditingDesignTypeGenerator(typeResolver)).DefineType(runtimeType);
                            if (type1 != typeId.RuntimeType)
                            {
                                this.assemblyName = AssemblyHelper.GetAssemblyName(type1.Assembly).Name;
                                this.assembly     = this.GetAssembly(this.typeResolver, this.assemblyName);
                                return(type1);
                            }
                        }
                    }
                }
            }
            if (typeId.arrayItemType == null)
            {
                IAssembly runtimeAssembly = typeId.RuntimeAssembly;
                if (runtimeAssembly != null)
                {
                    runtimeAssembly = this.GetAssembly(typeResolver, runtimeAssembly.Name);
                }
                if (runtimeAssembly != null)
                {
                    Type type2 = PlatformTypeHelper.GetType(runtimeAssembly, typeId.FullName);
                    if (type2 != null)
                    {
                        if (!typeId.IsGenericType)
                        {
                            return(type2);
                        }
                        IList <IType> genericTypeArguments = typeId.GetGenericTypeArguments();
                        int           count = genericTypeArguments.Count;
                        if (count > 0)
                        {
                            Type[] typeArray = new Type[count];
                            for (int i = 0; i < count; i++)
                            {
                                Type runtimeType1 = this.GetRuntimeType(genericTypeArguments[i]);
                                if (runtimeType1 == null)
                                {
                                    return(null);
                                }
                                typeArray[i] = runtimeType1;
                            }
                            try
                            {
                                type = type2.MakeGenericType(typeArray);
                            }
                            catch (ArgumentException argumentException)
                            {
                                return(null);
                            }
                            return(type);
                        }
                    }
                }
            }
            else
            {
                Type runtimeType2 = this.GetRuntimeType(typeId.arrayItemType);
                if (runtimeType2 != null)
                {
                    if (typeId.arrayRank <= 1)
                    {
                        return(runtimeType2.MakeArrayType());
                    }
                    return(runtimeType2.MakeArrayType(typeId.arrayRank));
                }
            }
            return(null);
        }