Exemplo n.º 1
0
        private void BuildMember(MemberInfo mi)
        {
            TypeBuilderHelper nestedType = _typeBuilder.DefineNestedType(
                "Accessor$" + mi.Name, TypeAttributes.NestedPrivate, typeof(MemberAccessor));

            ConstructorBuilderHelper ctorBuilder = BuildNestedTypeConstructor(nestedType);

            BuildGetter(mi, nestedType);
            BuildSetter(mi, nestedType);
            BuildInitMember(mi, ctorBuilder);

            Type type = mi is FieldInfo ? ((FieldInfo)mi).FieldType : ((PropertyInfo)mi).PropertyType;

            BuildIsNull(mi, nestedType, type);

            if (type.IsEnum)
            {
                type = Enum.GetUnderlyingType(type);
            }

            string typedPropertyName = type.Name;

            if (type.IsGenericType)
            {
                Type underlyingType = Nullable.GetUnderlyingType(type);

                if (underlyingType != null)
                {
                    BuildTypedGetterForNullable(mi, nestedType, underlyingType);
                    BuildTypedSetterForNullable(mi, nestedType, underlyingType);

                    if (underlyingType.IsEnum)
                    {
                        underlyingType = Enum.GetUnderlyingType(underlyingType);
                        type           = typeof(Nullable <>).MakeGenericType(underlyingType);
                    }
                    typedPropertyName = "Nullable" + underlyingType.Name;
                }
                else
                {
                    typedPropertyName = null;
                }
            }

            if (typedPropertyName != null)
            {
                BuildTypedGetter(mi, nestedType, typedPropertyName);
                BuildTypedSetter(mi, nestedType, type, typedPropertyName);
            }

            BuildCloneValueMethod(mi, nestedType, type);

            // FW 1.1 wants nested types to be created before parent.
            //
            _nestedTypes.Add(nestedType);
        }
Exemplo n.º 2
0
        private void BuildMember(MemberInfo mi)
        {
            var isValueType = _originalType.IsValueType;
            var nestedType  = _typeBuilder.DefineNestedType("Accessor$" + mi.Name, TypeAttributes.NestedPrivate, typeof(MemberAccessor));
            var ctorBuilder = BuildNestedTypeConstructor(nestedType);

            BuildGetter(mi, nestedType);
            if (!isValueType)
            {
                BuildSetter(mi, nestedType);
            }
            BuildInitMember(mi, ctorBuilder);

            var type = mi is FieldInfo ? ((FieldInfo)mi).FieldType : ((PropertyInfo)mi).PropertyType;

            BuildIsNull(mi, nestedType, type);

            if (type.IsEnum)
            {
                type = Enum.GetUnderlyingType(type);
            }

            var typedPropertyName = type.Name;

            if (type.IsGenericType)
            {
                var underlyingType = Nullable.GetUnderlyingType(type);

                if (underlyingType != null)
                {
                    BuildTypedGetterForNullable(mi, nestedType, underlyingType);
                    if (!isValueType)
                    {
                        BuildTypedSetterForNullable(mi, nestedType, underlyingType);
                    }

                    if (underlyingType.IsEnum)
                    {
                        // Note that PEVerify will complain on using Nullable<SomeEnum> as Nullable<Int32>.
                        // It works in the current CLR implementation, bu may not work in future releases.
                        //
                        underlyingType = Enum.GetUnderlyingType(underlyingType);
                        type           = typeof(Nullable <>).MakeGenericType(underlyingType);
                    }

                    typedPropertyName = "Nullable" + underlyingType.Name;
                }
                else
                {
                    typedPropertyName = null;
                }
            }

            if (typedPropertyName != null)
            {
                BuildTypedGetter(mi, nestedType, typedPropertyName);
                if (!isValueType)
                {
                    BuildTypedSetter(mi, nestedType, type, typedPropertyName);
                }
            }

            if (!isValueType)
            {
                BuildCloneValueMethod(mi, nestedType, type);
            }

            // FW 1.1 wants nested types to be created before parent.
            //
            _nestedTypes.Add(nestedType);
        }