Пример #1
0
        public DynamicField Field(string fieldName)
        {
            DynamicField field;

            if (!_fields.TryGetValue(fieldName, out field))
            {
                field = new DynamicField
                {
                    FieldName = fieldName
                };
                _fields.Add(fieldName, field);
            }
            return(field);
        }
Пример #2
0
        private void DefineField(TypeBuilder typeBuilder, ConstructorBuilder constructorBuilder, DynamicField fieldInfo)
        {
            var fieldAttributes = FieldAttributes.Public;

            if (fieldInfo.Static)
            {
                fieldAttributes |= FieldAttributes.Static;
            }

            var fieldType    = _typeBuilders[fieldInfo.FieldType.Name].Item1;
            var fieldBuilder = typeBuilder.DefineField(fieldInfo.FieldName, fieldType, fieldAttributes);

            if (fieldInfo.SetInstancePattern)
            {
                var staticConstructorBuilder = typeBuilder.DefineConstructor(
                    MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, Type.EmptyTypes);
                var generator = staticConstructorBuilder.GetILGenerator();

                generator.Emit(OpCodes.Newobj, constructorBuilder);
                generator.Emit(OpCodes.Stsfld, fieldBuilder);
                generator.Emit(OpCodes.Ret);
            }
        }