Esempio n. 1
0
        private void GenerateMember(TypeBuilder typeBuilder, ColumnImplementation columnInfo)
        {
            var nodeType = columnInfo.Definition.Type;

            if (columnInfo.Size.HasValue)
            {
                nodeType = nodeType.AdjustBitCount(columnInfo.Size.Value);
            }

            if (columnInfo.Cardinality.HasValue && columnInfo.Cardinality.Value > 1)
            {
                nodeType = nodeType.MakeArrayType();
            }

            var backingField = typeBuilder.DefineField(columnInfo.Definition.Name + "_backingField", nodeType,
                                                       FieldAttributes.Private | FieldAttributes.SpecialName);
            var memberBuilder = typeBuilder.DefineProperty(columnInfo.Definition.Name, PropertyAttributes.None, nodeType, Type.EmptyTypes);

            var getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

            var getBuilder   = typeBuilder.DefineMethod($"get_{columnInfo.Definition.Name}", getSetAttr, nodeType, Type.EmptyTypes);
            var getGenerator = getBuilder.GetILGenerator();

            getGenerator.Emit(OpCodes.Ldarg_0);
            getGenerator.Emit(OpCodes.Ldfld, backingField);
            getGenerator.Emit(OpCodes.Ret);

            var setBuilder   = typeBuilder.DefineMethod($"set_{columnInfo.Definition.Name}", getSetAttr, null, new[] { nodeType });
            var setGenerator = setBuilder.GetILGenerator();

            setGenerator.Emit(OpCodes.Ldarg_0);
            setGenerator.Emit(OpCodes.Ldarg_1);
            setGenerator.Emit(OpCodes.Stfld, backingField);
            setGenerator.Emit(OpCodes.Ret);

            memberBuilder.SetSetMethod(setBuilder);
            memberBuilder.SetGetMethod(getBuilder);

            CustomAttributeBuilder arraySize = null;

            if (columnInfo.Cardinality.HasValue)
            {
                arraySize = new CustomAttributeBuilder(
                    typeof(CardinalityAttribute).GetConstructor(Type.EmptyTypes),
                    new object[] { },
                    new [] { typeof(CardinalityAttribute).GetProperty("SizeConst") },
                    new object[] { columnInfo.Cardinality.Value });
            }

            if (arraySize != null)
            {
                memberBuilder.SetCustomAttribute(arraySize);
            }

            if (columnInfo.Annotations.Contains("id"))
            {
                memberBuilder.SetCustomAttribute(new CustomAttributeBuilder(
                                                     typeof(IndexAttribute).GetConstructor(Type.EmptyTypes),
                                                     new object[] { }
                                                     ));
            }

            if (!string.IsNullOrEmpty(columnInfo.Comment))
            {
                memberBuilder.SetCustomAttribute(new CustomAttributeBuilder(
                                                     typeof(CommentAttribute).GetConstructor(new[] { typeof(string) }),
                                                     new object[] { columnInfo.Comment }));
            }

            if (!string.IsNullOrEmpty(columnInfo.Definition.Comments))
            {
                memberBuilder.SetCustomAttribute(new CustomAttributeBuilder(
                                                     typeof(CommentAttribute).GetConstructor(new[] { typeof(string) }),
                                                     new object[] { columnInfo.Definition.Comments }));
            }

            // info about relationship isnt needed for DBCF.NET
            // same for fks

            if (!columnInfo.Definition.Verified)
            {
                memberBuilder.SetCustomAttribute(new CustomAttributeBuilder(
                                                     typeof(UnverifiedAttribute).GetConstructor(Type.EmptyTypes),
                                                     new object[0]));
            }

            memberBuilder.SetCustomAttribute(
                new CustomAttributeBuilder(typeof(OrderAttribute).GetConstructor(new[] { typeof(int) }),
                                           new object[] { _memberIndex++ }));
        }
Esempio n. 2
0
        private void ProduceType(string line)
        {
            var columns = new List <ColumnImplementation>();

            _memberIndex = 0;

            while (line != null)
            {
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                var impl = new ColumnImplementation();

                var regexMatch = _colUsageRegex.Match(line);
                impl.Definition = _columnDefinitions.First(d => d.Name == regexMatch.Groups["name"].Value);

                if (regexMatch.Groups["size"].Success)
                {
                    impl.Size = int.Parse(regexMatch.Groups["size"].Value);
                }

                if (regexMatch.Groups["array"].Success)
                {
                    impl.Cardinality = int.Parse(regexMatch.Groups["array"].Value);
                }

                if (regexMatch.Groups["comments"].Success)
                {
                    impl.Comment = regexMatch.Groups["comments"].Value;
                }

                if (regexMatch.Groups["annotations"].Success)
                {
                    impl.Annotations = regexMatch.Groups["annotations"].Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(v => v.Trim()).ToList();
                }

                columns.Add(impl);

                line = _streamReader.ReadLine();
            }

            var type = _module.DefineType($"{_fileName}_{Math.Abs(Path.GetRandomFileName().GetHashCode())}");

            foreach (var layoutAttr in _layoutAttributes)
            {
                var attrBuilder = new CustomAttributeBuilder(typeof(LayoutAttribute).GetConstructor(new[] { typeof(uint) }), new object[] { layoutAttr });
                type.SetCustomAttribute(attrBuilder);
            }

            foreach (var buildAttr in _buildAttributes)
            {
                var attrBuilder = new CustomAttributeBuilder(typeof(BuildAttribute).GetConstructor(new[] { typeof(string) }), new object[] { buildAttr });
                type.SetCustomAttribute(attrBuilder);
            }

            foreach (var buildAttr in _buildRangeAttributes)
            {
                var attrBuilder = new CustomAttributeBuilder(typeof(BuildRangeAttribute).GetConstructor(new[] { typeof(string) }), new object[] { buildAttr });
                type.SetCustomAttribute(attrBuilder);
            }

            if (!string.IsNullOrEmpty(_comment))
            {
                var attrBuilder = new CustomAttributeBuilder(typeof(CommentAttribute).GetConstructor(new[] { typeof(string) }), new object[] { _comment });
                type.SetCustomAttribute(attrBuilder);
            }

            foreach (var colInfo in columns)
            {
                GenerateMember(type, colInfo);
            }

            _createdTypes.Add(type.CreateType());

            // Type produced, cleanup
            _layoutAttributes.Clear();
            _buildAttributes.Clear();
            _buildRangeAttributes.Clear();
            _comment = null;
        }