Ejemplo n.º 1
0
        /// <summary>
        /// Gets the value of a properties property.
        /// </summary>
        /// <param name="setting"></param>
        /// <param name="value">The value of the specified profile property, typed as <see cref="System.Object"/>.</param>
        /// <returns>The result segnaled.</returns>
        protected virtual bool GetPropertyValue(SiteSettingsProperty setting, out object value)
        {
            Initialize();

            if (!values.Contains(setting))
            {
                value = null;
                return false;
            }

            value = values[setting].Value;

            return true;
        }
Ejemplo n.º 2
0
        protected virtual bool SetPropertyValue(SiteSettingsProperty setting, object value)
        {
            Initialize();

            var property = values[setting];

            if (property == null)
            {
                property = new SiteSettingsPropertyValue(setting);
                values.Add(property);
            }
            
            property.Value = value;

            return true;
        }
Ejemplo n.º 3
0
        private MethodBuilder CreateGetter(SiteSettingsProperty setting, PropertyInfo property, FieldInfo backingField)
        {
            var methodBuilder = proxy
                .DefineMethod("get_" + property.Name,
                              MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName,
                              property.PropertyType,
                              Type.EmptyTypes);
            var generator = methodBuilder.GetILGenerator();
            var result = generator.DeclareLocal(typeof(Object));

            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Ldfld, backingField);
            generator.Emit(OpCodes.Ldloca_S, result);
            generator.EmitCall(OpCodes.Callvirt, getPropertyMethod, Type.EmptyTypes);

            var label = generator.DefineLabel();

            generator.Emit(OpCodes.Brtrue_S, label);

            if (setting.DefaultValue != null)
            {
                var defaultValueMethod = typeof (SiteSettingsProperty)
                    .GetProperty("DefaultValue")
                    .GetGetMethod();

                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldfld, backingField);
                generator.Emit(OpCodes.Callvirt, defaultValueMethod);
                generator.Emit(OpCodes.Stloc_0);
            }
            else
            {
                generator.Emit(OpCodes.Newobj, typeof (Exception).GetConstructor(Type.EmptyTypes));
                generator.Emit(OpCodes.Throw);
            }

            generator.MarkLabel(label);
            generator.Emit(OpCodes.Ldloc_0);
            generator.Emit(property.PropertyType.IsValueType
                               ? OpCodes.Unbox_Any
                               : OpCodes.Castclass,
                           property.PropertyType);
            generator.Emit(OpCodes.Ret);

            return methodBuilder;
        }
Ejemplo n.º 4
0
        private static void GenerateDefaultValue(ILGenerator generator, SiteSettingsProperty setting)
        {
            if (setting.DefaultValue == null)
            {
                generator.Emit(OpCodes.Ldnull);
                return;
            }

            var tc = Type.GetTypeCode(setting.PropertyType);

            switch (tc)
            {
                case TypeCode.Byte:
                    {
                        generator.Emit(OpCodes.Ldc_I4_S, (byte)setting.DefaultValue);
                        generator.Emit(OpCodes.Box, setting.PropertyType);
                    }
                    break;

                case TypeCode.Char:
                    {
                        generator.Emit(OpCodes.Ldc_I4_S, (char)setting.DefaultValue);
                        generator.Emit(OpCodes.Box, setting.PropertyType);
                    }
                    break;

                case TypeCode.UInt16:
                    {
                        generator.Emit(OpCodes.Ldc_I4, (ushort)setting.DefaultValue);
                        generator.Emit(OpCodes.Box, setting.PropertyType);
                    }
                    break;

                case TypeCode.Int32:
                    {
                        generator.Emit(OpCodes.Ldc_I4_S, (int) setting.DefaultValue);
                        generator.Emit(OpCodes.Box, setting.PropertyType);
                    }
                    break;

                case TypeCode.UInt64:
                    {
                        generator.Emit(OpCodes.Ldc_I8, (ulong) setting.DefaultValue);
                        generator.Emit(OpCodes.Box, setting.PropertyType);
                    }
                    break;

                case TypeCode.Int64:
                    {
                        generator.Emit(OpCodes.Ldc_I8, (long) setting.DefaultValue);
                        generator.Emit(OpCodes.Box, setting.PropertyType);
                    }
                    break;

                case TypeCode.String:
                    {
                        generator.Emit(OpCodes.Ldstr, (string)setting.DefaultValue);
                    }
                    break;

                case TypeCode.Boolean:
                    {
                        generator.Emit((bool)setting.DefaultValue ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
                        generator.Emit(OpCodes.Box, setting.PropertyType);
                    }
                    break;

                default:
                    throw new NotSupportedException();
            }
        }