Пример #1
0
        private void combineDynamicType(FigureCompiler compiler)
        {
            var fcvt = compiler;

            compiledType = fcvt.CompileFigureType(Name);
            Rubrics.KeyRubrics.Add(fcvt.Identities.Values);
            this.Type = compiledType.New().GetType();
            Size      = Marshal.SizeOf(this.Type);
            var rf = this.Type.GetRuntimeFields().ToArray();

            if (!Rubrics.AsValues().Where(m => m.Name == "SerialCode").Any())
            {
                var mr = new MemberRubric(rf[0]);
                mr.RubricName = "SerialCode";
                Rubrics.Insert(0, mr);
            }

            Rubrics.AsValues().Select((m, y) => m.FigureField = rf[y]).ToArray();
            Rubrics.AsValues().Where(m => m.FigureField != null)
            .Select((f, y) => new object[] {
                f.FieldId      = y - 1,
                f.RubricId     = y - 1,
                f.RubricOffset = (int)Marshal.OffsetOf(this.Type, f.FigureField.Name)
            })
            .ToArray();
        }
        /// <summary>
        /// The createField.
        /// </summary>
        /// <param name="tb">The tb<see cref="TypeBuilder"/>.</param>
        /// <param name="mr">The mr<see cref="MemberRubric"/>.</param>
        /// <param name="type">The type<see cref="Type"/>.</param>
        /// <param name="fieldName">The fieldName<see cref="string"/>.</param>
        /// <returns>The <see cref="FieldBuilder"/>.</returns>
        private FieldBuilder createField(TypeBuilder tb, MemberRubric mr, Type type, string fieldName)
        {
            if (type == typeof(string) || type.IsArray)
            {
                //Type _type = type;

                //FieldBuilder fb;

                //if (type == typeof(string))
                //    _type = typeof(char[]);

                //fb = tb.DefineField(fieldName, _type, FieldAttributes.Private | FieldAttributes.HasDefault | FieldAttributes.HasFieldMarshal);

                //ResolveMarshalAsAttributeForArray(fb, mr, _type);

                FieldBuilder fb = tb.DefineField(fieldName, type, FieldAttributes.Private | FieldAttributes.HasDefault | FieldAttributes.HasFieldMarshal);

                if (type == typeof(string))
                {
                    ResolveMarshalAsAttributeForString(fb, mr, type);
                }
                else
                {
                    ResolveMarshalAsAttributeForArray(fb, mr, type);
                }

                return(fb);
            }
            else
            {
                return(tb.DefineField(fieldName, type, FieldAttributes.Private));
            }
        }
Пример #3
0
        /// <summary>
        /// The CreateFieldsAndProperties.
        /// </summary>
        /// <param name="tb">The tb<see cref="TypeBuilder"/>.</param>
        /// <returns>The <see cref="FieldBuilder[]"/>.</returns>
        public override FieldBuilder[] CreateFieldsAndProperties(TypeBuilder tb)
        {
            for (int i = scode; i < length + scode; i++)
            {
                MemberRubric mr             = fieldRubrics[i - scode];
                Type         type           = null;
                string       name           = mr.RubricName;
                string       fieldName      = "_" + mr.RubricName;
                bool         isBackingField = false;

                if (mr.MemberType == MemberTypes.Field)
                {
                    var fieldRubric = (FieldRubric)mr.RubricInfo;
                    isBackingField        = fieldRubric.IsBackingField;
                    fieldRubric.FieldName = fieldName;

                    type = mr.RubricType;
                    if (type == null)
                    {
                        type = fieldRubric.FieldType;
                    }
                }
                else if (mr.MemberType == MemberTypes.Property)
                {
                    type = mr.RubricType;
                    if (type == null)
                    {
                        type = ((PropertyRubric)mr.RubricInfo).PropertyType;
                    }
                }

                if ((type.IsArray && !type.GetElementType().IsValueType) ||
                    (!type.IsArray && !type.IsValueType && type != typeof(string)))
                {
                    type = null;
                }

                if (type != null)
                {
                    var _mr = mr;
                    if (isBackingField)
                    {
                        var __mr = propertyRubrics[mr.RubricName];
                        if (__mr != null)
                        {
                            _mr = __mr;
                        }
                    }

                    ResolveFigureAttributes(null, _mr, mr);

                    derivedFields[i] = (FieldRubric)mr.RubricInfo;
                }
            }

            return(fields);
        }
        /// <summary>
        /// The Compute.
        /// </summary>
        /// <param name="figures">The figures<see cref="IFigures"/>.</param>
        /// <param name="rubric">The rubric<see cref="MemberRubric"/>.</param>
        /// <returns>The <see cref="IFigures"/>.</returns>
        public static IFigures Compute(this IFigures figures, MemberRubric rubric)
        {
            IComputation ic = figures.Computations.Where(c => ((Computation)c).ContainsFirst(rubric)).FirstOrDefault();

            if (ic != null)
            {
                ic.Compute();
            }
            return(figures);
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MemberRubric"/> class.
 /// </summary>
 /// <param name="member">The member<see cref="MemberRubric"/>.</param>
 public MemberRubric(MemberRubric member) : this(member.RubricInfo != null ? (IMemberRubric)member.RubricInfo : member)
 {
     FigureType      = member.FigureType;
     FigureField     = member.FigureField;
     FieldId         = member.FieldId;
     RubricOffset    = member.RubricOffset;
     IsKey           = member.IsKey;
     IsIdentity      = member.IsIdentity;
     IsAutoincrement = member.IsAutoincrement;
     IdentityOrder   = member.IdentityOrder;
     Required        = member.Required;
     DisplayName     = member.DisplayName;
 }
Пример #6
0
        public void ResolveFigureAttributes(FieldBuilder fb, MemberRubric mrwa, MemberRubric mr)
        {
            MemberInfo mi = mrwa.RubricInfo;

            resolveFigureKeyAttributes(fb, mi, mr);

            resolveFigureIdentityAttributes(fb, mi, mr);

            resolveFigureRquiredAttributes(fb, mi, mr);

            resolveFigureDisplayAttributes(fb, mi, mr);

            resolveFigureTreatmentAttributes(fb, mi, mr);
        }
Пример #7
0
        private void resolveFigureKeyAttributes(FieldBuilder fb, MemberInfo mi, MemberRubric mr)
        {
            object[] o = mi.GetCustomAttributes(typeof(FigureKeyAttribute), false);
            if (o != null && o.Any())
            {
                FigureKeyAttribute fka = (FigureKeyAttribute)o.First();
                mr.IsKey           = true;
                mr.IsIdentity      = true;
                mr.IsAutoincrement = fka.IsAutoincrement;

                if (Identities.ContainsKey(fka.Order))
                {
                    fka.Order = (short)(Identities.LastOrDefault().Key + 1);
                }

                mr.IdentityOrder = fka.Order;
                Identities.Add(mr.IdentityOrder, mr);
                mr.Required = true;

                if (fb != null)
                {
                    CreateFigureKeyAttribute(fb, fka);
                }
            }
            else if (mr.IsKey)
            {
                mr.IsIdentity = true;
                mr.Required   = true;

                if (Identities.ContainsKey(mr.IdentityOrder))
                {
                    mr.IdentityOrder += (short)(Identities.LastOrDefault().Key + 1);
                }

                Identities.Add(mr.IdentityOrder, mr);

                if (fb != null)
                {
                    CreateFigureKeyAttribute(fb, new FigureKeyAttribute()
                    {
                        IsAutoincrement = mr.IsAutoincrement, Order = mr.IdentityOrder
                    });
                }
            }
        }
Пример #8
0
        private void resolveFigureDisplayAttributes(FieldBuilder fb, MemberInfo mi, MemberRubric mr)
        {
            object[] o = mi.GetCustomAttributes(typeof(FigureDisplayAttribute), false);
            if (o != null && o.Any())
            {
                FigureDisplayAttribute fda = (FigureDisplayAttribute)o.First();;
                mr.DisplayName = fda.Name;

                if (fb != null)
                {
                    CreateFigureDisplayAttribute(fb, fda);
                }
            }
            else if (mr.DisplayName != null)
            {
                CreateFigureDisplayAttribute(fb, new FigureDisplayAttribute(mr.DisplayName));
            }
        }
Пример #9
0
        public void ResolveMarshalAsAttributeForString(FieldBuilder field, MemberRubric member, Type type)
        {
            MemberInfo _member = member.RubricInfo;

            if (member is MemberRubric && ((MemberRubric)member).FigureField != null)
            {
                _member = ((MemberRubric)member).FigureField;
            }

            object[] o = _member.GetCustomAttributes(typeof(MarshalAsAttribute), false);
            if (o == null || !o.Any())
            {
                o = _member.GetCustomAttributes(typeof(FigureAsAttribute), false);
                if (o != null && o.Any())
                {
                    FigureAsAttribute maa = (FigureAsAttribute)o.First();
                    CreateFigureAsAttribute(field, new FigureAsAttribute(UnmanagedType.ByValTStr)
                    {
                        SizeConst = maa.SizeConst
                    });
                }
                else
                {
                    int size = 64;
                    if (member.RubricSize > 0)
                    {
                        size = member.RubricSize;
                    }
                    CreateFigureAsAttribute(field, new FigureAsAttribute(UnmanagedType.ByValTStr)
                    {
                        SizeConst = size
                    });
                }
            }
            else
            {
                MarshalAsAttribute maa = (MarshalAsAttribute)o.First();
                CreateMarshaAslAttribute(field, new MarshalAsAttribute(UnmanagedType.ByValTStr)
                {
                    SizeConst = maa.SizeConst
                });
            }
        }
Пример #10
0
        private void resolveFigureRquiredAttributes(FieldBuilder fb, MemberInfo mi, MemberRubric mr)
        {
            object[] o = mi.GetCustomAttributes(typeof(FigureRequiredAttribute), false);
            if (o != null && o.Any())
            {
                mr.Required = true;

                if (fb != null)
                {
                    CreateFigureRequiredAttribute(fb);
                }
            }
            else if (mr.Required)
            {
                if (fb != null)
                {
                    CreateFigureRequiredAttribute(fb);
                }
            }
        }
Пример #11
0
        private void combineDerivedType(FigureCompiler compiler)
        {
            var fcdt = compiler;

            compiledType = fcdt.CompileFigureType(Name);
            Rubrics.KeyRubrics.Add(fcdt.Identities.Values);
            this.Type = compiledType.New().GetType();
            Size      = Marshal.SizeOf(this.Type);

            if (!Rubrics.AsValues().Where(m => m.Name == "SerialCode").Any())
            {
                var f  = this.Type.GetField("serialcode", BindingFlags.NonPublic | BindingFlags.Instance);
                var mr = new MemberRubric(f);
                mr.RubricName   = "SerialCode";
                mr.FigureField  = f;
                mr.RubricOffset = (int)Marshal.OffsetOf(this.Type, f.Name);
                Rubrics.Insert(0, mr);
            }

            var df = fcdt.derivedFields;

            Rubrics.AsValues().Select((m, y) => m.FigureField = df[y].RubricInfo).ToArray();
            Rubrics.AsValues().Where(m => m.FigureField != null)
            .Select((f, y) => new object[] {
                f.FieldId  = y - 1,
                f.RubricId = y - 1
            })
            .ToArray();

            foreach (var rubric in Rubrics.Skip(1).ToArray())
            {
                try
                {
                    rubric.RubricOffset = (int)Marshal.OffsetOf(BaseType, rubric.FigureField.Name);
                }
                catch (Exception ex)
                {
                    rubric.RubricOffset = -1;
                }
            }
        }
        /// <summary>
        /// The CreateFieldsAndProperties.
        /// </summary>
        /// <param name="tb">The tb<see cref="TypeBuilder"/>.</param>
        /// <returns>The <see cref="FieldBuilder[]"/>.</returns>
        public override FieldBuilder[] CreateFieldsAndProperties(TypeBuilder tb)
        {
            for (int i = scode; i < length + scode; i++)
            {
                MemberRubric mr             = fieldRubrics[i - scode];
                Type         type           = null;
                string       name           = mr.RubricName;
                string       fieldName      = "_" + mr.RubricName;
                bool         isBackingField = false;
                if (mr.MemberType == MemberTypes.Field)
                {
                    var fieldRubric = (FieldRubric)mr.RubricInfo;
                    isBackingField        = fieldRubric.IsBackingField;
                    fieldRubric.FieldName = fieldName;
                    type = mr.RubricType;

                    if (type == null)
                    {
                        type = fieldRubric.FieldType;
                    }
                }
                else if (mr.MemberType == MemberTypes.Property)
                {
                    type = mr.RubricType;
                    if (type == null)
                    {
                        type = ((PropertyRubric)mr.RubricInfo).PropertyType;
                    }
                }

                if ((type.IsArray && !type.GetElementType().IsValueType) ||
                    (!type.IsArray && !type.IsValueType && type != typeof(string)))
                {
                    type = null;
                }

                if (type != null)
                {
                    var _mr = mr;
                    if (isBackingField)
                    {
                        var __mr = propertyRubrics[mr.RubricName];
                        if (__mr != null)
                        {
                            _mr = __mr;
                        }
                    }

                    FieldBuilder fb = createField(tb, _mr, type, fieldName);

                    if (fb != null)
                    {
                        ResolveFigureAttributes(fb, _mr, mr);

                        PropertyBuilder pi = createProperty(tb, fb, type, name);
                        fields[i] = fb;
                        props[i]  = pi;
                        pi.SetCustomAttribute(new CustomAttributeBuilder(dataMemberCtor, new object[0], dataMemberProps, new object[2] {
                            i - scode, name
                        }));
                    }
                }
            }

            return(fields);
        }
Пример #13
0
        private void resolveFigureTreatmentAttributes(FieldBuilder fb, MemberInfo mi, MemberRubric mr)
        {
            object[] o = mi.GetCustomAttributes(typeof(FigureTreatmentAttribute), false);
            if (o != null && o.Any())
            {
                FigureTreatmentAttribute fta = (FigureTreatmentAttribute)o.First();;
                mr.AggregateOperand = fta.AggregateOperand;
                mr.SummaryOperand   = fta.SummaryOperand;

                if (fb != null)
                {
                    CreateFigureTreatmentAttribute(fb, fta);
                }
            }
            else if (mr.AggregateOperand != AggregateOperand.None || mr.SummaryOperand != AggregateOperand.None)
            {
                CreateFigureTreatmentAttribute(fb, new FigureTreatmentAttribute()
                {
                    AggregateOperand = mr.AggregateOperand, SummaryOperand = mr.SummaryOperand
                });
            }
        }