Exemplo n.º 1
0
        public Assembly Compile(AssemblyName assemblyName)
        {
            var assemblyBuilder =
                AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

            foreach (var attribute in Attributes)
            {
                assemblyBuilder.SetCustomAttribute(AnnotationAttributeBuilder.Create(attribute));
            }

            var moduleName = string.Format("DynamicEntitiesModule{0}.dll", _assemblyCount);
            var module     = assemblyBuilder.DefineDynamicModule(moduleName);

            _assemblyCount++;

            foreach (var enumTypeInfo in DynamicTypes.OfType <DynamicEnumType>())
            {
                _types.Add(enumTypeInfo.Name, DefineEnumType(module, enumTypeInfo));
            }

            foreach (var typeInfo in DynamicTypes.OfType <DynamicStructuralType>())
            {
                DefineStructuralType(module, typeInfo);
            }

            foreach (var typeInfo in DynamicTypes.OfType <DynamicStructuralType>())
            {
                var typeBuilder = _typeBuilders[typeInfo.Name];

                foreach (var fieldInfo in typeInfo.Fields)
                {
                    DefineField(typeBuilder.Item1, typeBuilder.Item2, fieldInfo);
                }

                foreach (var propertyInfo in typeInfo.Properties)
                {
                    DefineProperty(typeBuilder.Item1, propertyInfo);
                }
            }

            foreach (var t in _typeBuilders)
            {
                _types.Add(t.Key, t.Value.Item1.CreateType());
            }

            return(assemblyBuilder);
        }
Exemplo n.º 2
0
        private Type DefineEnumType(ModuleBuilder module, DynamicEnumType enumTypeInfo)
        {
            var enumBuilder =
                module.DefineEnum(
                    enumTypeInfo.Name,
                    GetTypeAccess(enumTypeInfo.TypeAccess),
                    enumTypeInfo.UnderlyingType);

            foreach (var a in enumTypeInfo.Attributes)
            {
                enumBuilder.SetCustomAttribute(AnnotationAttributeBuilder.Create(a));
            }

            foreach (var enumMember in enumTypeInfo.Members)
            {
                enumBuilder.DefineLiteral(
                    enumMember.Key,
                    Convert.ChangeType(enumMember.Value, enumTypeInfo.UnderlyingType));
            }

            return(enumBuilder.CreateType());
        }
Exemplo n.º 3
0
        private void DefineStructuralType(ModuleBuilder module, DynamicStructuralType typeInfo)
        {
            var typeAttributes = TypeAttributes.Class | GetTypeAccess(typeInfo.TypeAccess);

            if (typeInfo.IsAbstract)
            {
                typeAttributes |= TypeAttributes.Abstract;
            }
            if (typeInfo.IsSealed)
            {
                typeAttributes |= TypeAttributes.Sealed;
            }

            Type baseClass = null;

            if (typeInfo.BaseClass is Type)
            {
                baseClass = typeInfo.BaseClass as Type;
            }
            else if (typeInfo.BaseClass is DynamicStructuralType)
            {
                baseClass = _typeBuilders[((DynamicStructuralType)typeInfo.BaseClass).Name].Item1;
            }

            var typeBuilder = module.DefineType(typeInfo.Name, typeAttributes, baseClass);

            foreach (var a in typeInfo.Attributes)
            {
                typeBuilder.SetCustomAttribute(AnnotationAttributeBuilder.Create(a));
            }

            // Define the Ctor
            var constructorBuilder = typeInfo.CtorAccess == MemberAccess.None
                                         ? null
                                         : typeBuilder.DefineDefaultConstructor(GetMethodAttributes(false, typeInfo.CtorAccess));

            _typeBuilders.Add(typeInfo.Name, Tuple.Create(typeBuilder, constructorBuilder));
        }
Exemplo n.º 4
0
        private void DefineProperty(TypeBuilder typeBuilder, DynamicProperty propertyInfo)
        {
            var getterAccess = propertyInfo.GetterAccess;
            var setterAccess = propertyInfo.SetterAccess;

            Type propertyType;

            if (propertyInfo.EnumType != null)
            {
                propertyType = _types[propertyInfo.EnumType.Item1.Name];

                if (propertyInfo.EnumType.Item2)
                {
                    propertyType = typeof(Nullable <>).MakeGenericType(propertyType);
                }
            }
            else if (propertyInfo.CollectionType != null)
            {
                if (propertyInfo.ReferenceType != null)
                {
                    propertyType =
                        propertyInfo.CollectionType.MakeGenericType(_typeBuilders[propertyInfo.ReferenceType.Name].Item1);
                }
                else
                {
                    propertyType = propertyInfo.CollectionType.MakeGenericType(propertyInfo.PropertyType);
                }
            }
            else if (propertyInfo.ReferenceType != null)
            {
                propertyType = _typeBuilders[propertyInfo.ReferenceType.Name].Item1;
                switch (_dynamicTypes[propertyInfo.ReferenceType.Name].TypeAccess)
                {
                case MemberAccess.Private:
                    getterAccess = MemberAccess.Private;
                    setterAccess = MemberAccess.Private;
                    break;

                case MemberAccess.Internal:
                    getterAccess = MemberAccess.Internal;
                    setterAccess = MemberAccess.Internal;
                    break;
                }
                if (propertyInfo.PropertyType != null &&
                    propertyInfo.PropertyType.IsGenericTypeDefinition())
                {
                    propertyType = propertyInfo.PropertyType.MakeGenericType(propertyType);
                }
            }
            else
            {
                propertyType = propertyInfo.PropertyType;
            }

            var fieldBuilder = typeBuilder.DefineField(
                "_" + propertyInfo.PropertyName, propertyType,
                FieldAttributes.Private);

            var propertyBuilder = typeBuilder.DefineProperty(
                propertyInfo.PropertyName,
                PropertyAttributes.None,
                propertyType, Type.EmptyTypes);

            foreach (var a in propertyInfo.Attributes)
            {
                propertyBuilder.SetCustomAttribute(AnnotationAttributeBuilder.Create(a));
            }

            if (propertyInfo.GetterAccess
                != MemberAccess.None)
            {
                var getter = typeBuilder.DefineMethod(
                    "get_" + propertyInfo.PropertyName,
                    GetMethodAttributes(propertyInfo.IsVirtual, getterAccess),
                    propertyType,
                    Type.EmptyTypes);
                var generator = getter.GetILGenerator();
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldfld, fieldBuilder);
                generator.Emit(OpCodes.Ret);
                propertyBuilder.SetGetMethod(getter);
            }

            if (propertyInfo.SetterAccess
                != MemberAccess.None)
            {
                var setter = typeBuilder.DefineMethod(
                    "set_" + propertyInfo.PropertyName,
                    GetMethodAttributes(propertyInfo.IsVirtual, setterAccess),
                    null,
                    new[] { propertyType });
                var generator = setter.GetILGenerator();
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldarg_1);
                generator.Emit(OpCodes.Stfld, fieldBuilder);
                generator.Emit(OpCodes.Ret);
                propertyBuilder.SetSetMethod(setter);
            }
        }