Example #1
0
 //添加属性设置
 public void Add(PropertySetter ps)
 {
     //删除旧的同名属性设置
     var olds = from old in propertySetters where old.PropertyName.Equals(ps.PropertyName) select old;
     var oldPs = olds.FirstOrDefault();
     if (oldPs != null)
     {
         propertySetters.Remove(oldPs);
     }
     propertySetters.Add(ps);
     ps.Object = this;
     //有默认值,且对象的值为空,设置对象值为默认值
     if (ps.Default != null)
     {
         PropertyInfo pi = ps.Object.GetType().GetProperty(ps.PropertyName);
         //是动态属性
         if (pi == null || pi is CustomPropertyInfoHelper)
         {
             if (GetPropertyValue(ps.PropertyName) == null)
             {
                 SetPropertyValue(ps.PropertyName, ps.Default, false, true);
             }
         }
         else
         {
             if (pi.GetValue(ps.Object, null) == null)
             {
                 pi.SetValue(ps.Object, ps.Default, null);
             }
         }
     }
     //对值进行校验
     Validate(ps.PropertyName, GetPropertyValue(ps.PropertyName));
 }
 /// <summary>
 /// Constructs the property manager.
 /// </summary>
 /// <param name="platformType">The platform type to represent.</param>
 /// <param name="getter">Receives property get requests and returns mock values.</param>
 /// <param name="setter">Receives property set requests and checks them.</param>
 public MockPropertyManager(
     ProjectPlatformType platformType, PropertyGetter getter, PropertySetter setter)
 {
   this.PlatformType = platformType;
   if (platformType == ProjectPlatformType.NaCl)
     this.PlatformName = Strings.NaCl64PlatformName;
   else
     this.PlatformName = Strings.PepperPlatformName;
   getter_ = getter;
   setter_ = setter;
 }
        public void PropertySetter_DefaultValue()
        {
            var methods = new[]
            {
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestBoolean(null, false)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestByte(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestSByte(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestInt16(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestUInt16(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestInt32(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestUInt32(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestInt64(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestUInt64(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestSingle(null, 0)),
                (MethodInfo)ReflectionHelpers.InfoOf((DefaultTest t) => t.TestDouble(null, 0)),
            };

            var instance = new DefaultTest();

            Assert.AreEqual(default(byte), instance.TestByteValue);
            Assert.AreEqual(default(sbyte), instance.TestSByteValue);
            Assert.AreEqual(default(short), instance.TestInt16Value);
            Assert.AreEqual(default(ushort), instance.TestUInt16Value);
            Assert.AreEqual(default(int), instance.TestInt32Value);
            Assert.AreEqual(default(uint), instance.TestUInt32Value);
            Assert.AreEqual(default(long), instance.TestInt64Value);
            Assert.AreEqual(default(ulong), instance.TestUInt64Value);
            Assert.AreEqual(default(float), instance.TestSingleValue);
            Assert.AreEqual(default(double), instance.TestDoubleValue);
            Assert.AreEqual(false, instance.TestBooleanValue);

            var emptyMap = new ReactStylesDiffMap(new JObject());

            foreach (var method in methods)
            {
                var setter = PropertySetter.CreateViewManagerSetters(method).Single();
                setter.UpdateViewManagerProperty(instance, null, emptyMap);
            }

            Assert.AreEqual(byte.MaxValue, instance.TestByteValue);
            Assert.AreEqual(sbyte.MaxValue, instance.TestSByteValue);
            Assert.AreEqual(short.MaxValue, instance.TestInt16Value);
            Assert.AreEqual(ushort.MaxValue, instance.TestUInt16Value);
            Assert.AreEqual(int.MaxValue, instance.TestInt32Value);
            Assert.AreEqual(uint.MaxValue, instance.TestUInt32Value);
            Assert.AreEqual(long.MaxValue, instance.TestInt64Value);
            Assert.AreEqual(ulong.MaxValue, instance.TestUInt64Value);
            Assert.AreEqual(float.MaxValue, instance.TestSingleValue);
            Assert.AreEqual(double.MaxValue, instance.TestDoubleValue);
            Assert.AreEqual(true, instance.TestBooleanValue);
        }
Example #4
0
        public void PropertySetter_SetScalar(float value)
        {
            var applicationOptions = OptionsMocks.CreateApplicationOptions();
            var propertySetter     = new PropertySetter(applicationOptions);
            var component          = ComponentMocks.CreateComponent <BasicTemplatelessComponent>();

            component.Name = "TestComponent";

            var propertyValue = new ScalarPropertyValue(value);

            propertySetter.SetValue(component, nameof(BasicTemplatelessComponent.Length), propertyValue);

            Assert.Equal(value, component.Single);
        }
Example #5
0
        public void PropertySetter_SetTimeSpan(float seconds)
        {
            var applicationOptions = OptionsMocks.CreateApplicationOptions();
            var propertySetter     = new PropertySetter(applicationOptions);
            var component          = ComponentMocks.CreateComponent <BasicTemplatelessComponent>();

            component.Name = "TestComponent";

            var propertyValue = new TimeSpanPropertyValue(seconds);

            propertySetter.SetValue(component, nameof(BasicTemplatelessComponent.TimeSpan), propertyValue);

            Assert.Equal(TimeSpan.FromSeconds(seconds), component.TimeSpan);
        }
Example #6
0
		private static IPropertySetterDictionary CreatePropertySetters(Type type)
		{
            PropertyInfo[] props = type.GetProperties();
            Dictionary<string, PropertySetter> internalSetters = new Dictionary<string, PropertySetter>(props.Length);
            foreach (PropertyInfo pi in props)
            {
                PropertySetter setter = GetPropertySetter(pi);
                if (setter != null)
                {
                    internalSetters.Add(pi.Name, setter);
                }
            }
            return new PropertySetterDictionary(internalSetters);
		}
Example #7
0
 /// <summary>
 /// Constructs the property manager.
 /// </summary>
 /// <param name="platformType">The platform type to represent.</param>
 /// <param name="getter">Receives property get requests and returns mock values.</param>
 /// <param name="setter">Receives property set requests and checks them.</param>
 public MockPropertyManager(
     ProjectPlatformType platformType, PropertyGetter getter, PropertySetter setter)
 {
     this.PlatformType = platformType;
     if (platformType == ProjectPlatformType.NaCl)
     {
         this.PlatformName = Strings.NaCl64PlatformName;
     }
     else
     {
         this.PlatformName = Strings.PepperPlatformName;
     }
     getter_ = getter;
     setter_ = setter;
 }
Example #8
0
        public sealed override T Build()
        {
            T instance = InstanceFactory.Create(_propertyOverwriters);

            var overwrittenProperties =
                from property in _properties
                join overwriter in _propertyOverwriters on property.Name equals overwriter.PropertyName
                select(Property : property, Overwriter : overwriter);

            foreach (var entry in overwrittenProperties)
            {
                PropertySetter.SetProperty(instance, entry.Property, entry.Overwriter.GetValue());
            }

            return(instance);
        }
Example #9
0
        private string parseNestedPropertyNameFromMemberExpression <T>(Expression <Func <T, object> > memberExpression)
        {
            DB.throwIfNullOrEmpty <MappingException>(memberExpression, "Member expression");

            MemberExpression propertyMemberExpression = null;
            string           nestedPropertyName;

            if (memberExpression.Body is MemberExpression)
            {
                propertyMemberExpression = (memberExpression.Body as MemberExpression);
            }
            else if (memberExpression.Body is UnaryExpression)             // value typelar objecte cast edildiği için member expression, unary (cast) expression içerisinde yeralıyor
            {
                UnaryExpression unaryExpression = (memberExpression.Body as UnaryExpression);
                if (unaryExpression.Operand is MemberExpression)
                {
                    propertyMemberExpression = (unaryExpression.Operand as MemberExpression);
                }
            }

            DB.throwIfNullOrEmpty <MappingException>(propertyMemberExpression, "Property member expression");

            int firstDotPosition = propertyMemberExpression.ToString().IndexOf(".", StringComparison.Ordinal);

            nestedPropertyName = propertyMemberExpression.ToString().Substring(firstDotPosition + 1);
            PropertyInfo propertyInfo = (propertyMemberExpression.Member as PropertyInfo);

            DB.throwIfNullOrEmpty <MappingException>(propertyInfo, "Property info");

            //mesela DateTime.Now gibi birşey girilmişse entity tipinin bir nested property'si olmadığı için hata verecek
            if (typeof(T) != getOutermostTypeFromNestedMemberExpression(propertyMemberExpression))
            {
                throw new MappingException("Property is in not a direct or nested property of the entity type.");
            }

            entityMapping.NestedPropertyInfoCacheAtMapper.Add(nestedPropertyName, propertyInfo);
            PropertyGetter getter = PropertyGetterSetterFactory.CreateGetter(propertyInfo);

            entityMapping.NestedPropertyGetterCacheAtMapper.Add(nestedPropertyName, getter);
            PropertySetter setter = PropertyGetterSetterFactory.CreateSetter(propertyInfo);

            entityMapping.NestedPropertySetterCacheAtMapper.Add(nestedPropertyName, setter);

            entityMapping.GenerateRootTypePropertyNamePropertyInfoAndGetterSetterCacheEntryAtMapper(typeof(T), nestedPropertyName);

            return(nestedPropertyName);
        }
Example #10
0
        public void PropertySetter_SetLength(string unit, float length)
        {
            var applicationOptions = OptionsMocks.CreateApplicationOptions();
            var propertySetter     = new PropertySetter(applicationOptions);
            var component          = ComponentMocks.CreateComponent <BasicTemplatelessComponent>();

            component.Name = "TestComponent";

            var propertyValue = new LengthPropertyValue(unit, length);

            propertySetter.SetValue(component, nameof(BasicTemplatelessComponent.Length), propertyValue);

            Assert.Equal(unit == "%", component.Length[UnitType.Percentage] != 0);
            Assert.Equal(unit == "px", component.Length[UnitType.Pixel] != 0);
            Assert.Equal(unit == "x", component.Length[UnitType.Ratio] != 0);
            Assert.Equal(unit == "u", component.Length[UnitType.Unit] != 0);
        }
Example #11
0
        public void ReadOnlyProperty()
        {
            var          inst     = new PropertyTests();
            Type         instType = inst.GetType();
            PropertyInfo propRO   = instType.GetProperty("MyReadOnlyProperty");

            Assert.IsNotNull(propRO);

            inst.MyWriteOnlyProperty = 999;

            PropertyGetter <PropertyTests, string> getter = propRO.CreatePropertyGetter <PropertyTests, string>();

            Assert.IsNotNull(getter);

            string actual = getter(inst);

            Assert.AreEqual("999", actual);

            try
            {
                propRO.CreatePropertySetter <PropertyTests, string>();
                Assert.Fail("Setter should not exist.");
            }
            catch (ArgumentException)
            {
            }

            propRO = instType.GetProperty("MyReadOnlyAutoProperty");

            Assert.IsNotNull(propRO);

            getter = propRO.CreatePropertyGetter <PropertyTests, string>();

            Assert.IsNotNull(getter);

            Assert.AreEqual("MyAuto", getter(inst));

            PropertySetter <PropertyTests, string> setter = propRO.CreatePropertySetter <PropertyTests, string>();

            Assert.IsNotNull(setter);

            setter(inst, "NoAuto");

            Assert.AreEqual("NoAuto", inst.MyReadOnlyAutoProperty);
        }
Example #12
0
        public static ComponentTemplate ParseComponentTemplateCode(string code)
        {
            var appOptions            = OptionsMocks.CreateApplicationOptions();
            var propertySetter        = new PropertySetter(appOptions);
            var dssParser             = new DssParser();
            var expressionExecutor    = new ExpressionExecutor();
            var componentTypeResolver = new ComponentTypeResolver();
            var agpmlParser           = new AGPMLParser(propertySetter, dssParser, expressionExecutor, componentTypeResolver);

            var sourceInfo = new SourceCodeInfo("TestCode", code);
            var template   = agpmlParser.ParseComponentTemplate(sourceInfo);

            if (!ComponentTemplateProvider.ComponentTypes.Contains(template.ComponentType))
            {
                ComponentTemplateProvider.Add(template);
            }

            return(template);
        }
Example #13
0
 public override void AddAttribute(string name, string value)
 {
     if (name == "name")
     {
         component = ComponentGetter.GetFromAssemblies(value, element.module.data.GetNamespaces(), element.data.GetGameObject());
         if (component == null)
         {
             throw new SetAttributeException(name, value, this.name, value + " is not found. Ensure you wrote it correct or added \"using namespace\" element to the xml module.");
         }
     }
     else if (component != null)
     {
         PropertySetter.SetValue(component, name, value, PropertySetter.Data.Create(element.data, element.module.data));
     }
     else
     {
         throw new SetAttributeException(name, value, this.name, "Set name attribute first.");
     }
 }
Example #14
0
        private static T CreateItemFromReader <T>(IDataReader reader, PropertySetter[] setters) where T : new()
        {
            //Sabemos con seguridad que se puede hacer por la restriccion.
            T item = new T();

            int fieldCount = reader.FieldCount;

            for (int fieldOrdinal = 0; fieldOrdinal < fieldCount; fieldOrdinal++)
            {
                PropertySetter setter = setters[fieldOrdinal];
                if (setter != null)
                {
                    object fieldValue = reader.IsDBNull(fieldOrdinal) ? null : reader.GetValue(fieldOrdinal);
                    //Se invoca como si fuese un metodo y le establece el valor fieldvalue.
                    setter(item, fieldValue);
                }
            }
            return(item);
        }
        private void AssertOtherResult(LandingReport report,
                                       LandingParameters para, BoeingPerfTable table)
        {
            var calc = new LandingCalculator(table, para);

            foreach (var i in report.AllSettings)
            {
                int brakeIndex = Array.FindIndex(
                    table.BrakesAvailable(para.SurfaceCondition),
                    x => x == i.BrkSetting);

                PropertySetter.Set(para, "BrakeIndex", brakeIndex);

                double rwyRequired = calc.DistanceRequiredMeter();
                Assert.AreEqual(rwyRequired, i.ActualDisMeter, 0.5);

                double disRemain = para.RwyLengthMeter - rwyRequired;
                Assert.AreEqual(disRemain, i.DisRemainMeter, 0.5);
            }
        }
        public override DbCommand CreateCommand()
        {
            var command = base.CreateCommand();

            if (BindByNameSetter == null)
            {
                Type commandType = command.GetType();
                var  pi          = commandType.GetProperty("BindByName");
                if (pi == null)
                {
                    throw new InvalidOperationException("BindByName property not found on type " + commandType.FullName);
                }
                else
                {
                    BindByNameSetter = PropertyHelper.GetPropertySetter(pi);
                }
            }
            BindByNameSetter(command, true);
            return(command);
        }
Example #17
0
        public void ReadWriteProperty()
        {
            var          inst     = new PropertyTests();
            Type         instType = inst.GetType();
            PropertyInfo propRW   = instType.GetProperty("MyProperty");

            Assert.IsNotNull(propRW);

            PropertyGetter <PropertyTests, int> getter = propRW.CreatePropertyGetter <PropertyTests, int>();
            PropertySetter <PropertyTests, int> setter = propRW.CreatePropertySetter <PropertyTests, int>();

            Assert.IsNotNull(getter);
            Assert.IsNotNull(setter);

            setter(inst, 456);

            Assert.AreEqual(456, inst.MyProperty);

            int actual = Convert.ToInt32(getter(inst));

            Assert.AreEqual(456, actual);
        }
Example #18
0
        /// <summary>
        /// 생성자
        /// </summary>
        public SqlCommandSet() {
            if(IsDebugEnabled)
                log.Debug("{0}를 동적으로 생성하고, 내부 함수를 Delegate로 이용하여, 노출시킵니다.", SqlCommandSetTypeName);

            _sqlCommandSetInstance = ActivatorTool.CreateInstance(_sqlCommandSetType, true);

            _sqlCommandSetInstance.ShouldNotBeNull(SqlCommandSetTypeName);

            _connectionSettter =
                (PropertySetter<SqlConnection>)CreateDelegate(typeof(PropertySetter<SqlConnection>),
                                                              _sqlCommandSetInstance,
                                                              "set_Connection");
            _transactionSetter =
                (PropertySetter<SqlTransaction>)CreateDelegate(typeof(PropertySetter<SqlTransaction>),
                                                               _sqlCommandSetInstance,
                                                               "set_Transaction");
            _commandTimeoutSetter =
                (PropertySetter<int>)CreateDelegate(typeof(PropertySetter<int>),
                                                    _sqlCommandSetInstance,
                                                    "set_CommandTimeout");

            _connectionGetter =
                (PropertyGetter<SqlConnection>)CreateDelegate(typeof(PropertyGetter<SqlConnection>),
                                                              _sqlCommandSetInstance,
                                                              "get_Connection");

            _batchCommandGetter =
                (PropertyGetter<SqlCommand>)CreateDelegate(typeof(PropertyGetter<SqlCommand>),
                                                           _sqlCommandSetInstance,
                                                           "get_BatchCommand");

            _doAppend = (AppendCommand)CreateDelegate(typeof(AppendCommand), _sqlCommandSetInstance, "Append");
            _doExecuteNonQuery =
                (ExecuteNonQueryCommand)CreateDelegate(typeof(ExecuteNonQueryCommand), _sqlCommandSetInstance, "ExecuteNonQuery");
            _doDispose = (DisposeCommand)CreateDelegate(typeof(DisposeCommand), _sqlCommandSetInstance, "Dispose");
        }
Example #19
0
        public void WriteOnlyProperty()
        {
            var          inst     = new PropertyTests();
            Type         instType = inst.GetType();
            PropertyInfo propWO   = instType.GetProperty("MyWriteOnlyProperty");

            Assert.IsNotNull(propWO);

            PropertySetter <PropertyTests, int> setter = propWO.CreatePropertySetter <PropertyTests, int>();

            Assert.IsNotNull(setter);

            setter(inst, 789);

            Assert.AreEqual("789", inst.MyReadOnlyProperty);

            PropertyGetter <PropertyTests, int> getter = propWO.CreatePropertyGetter <PropertyTests, int>();

            Assert.IsNotNull(getter);

            int actual = Convert.ToInt32(getter(inst));

            Assert.AreEqual(789, actual);
        }
Example #20
0
 public static void SetProp(FrameworkElement ui, PropertySetter value)
 {
     ui.SetValue(PropProperty, value);
 }
 public override DbCommand CreateCommand()
 {
     var command = base.CreateCommand();
     if (BindByNameSetter == null)
     {
         Type commandType = command.GetType();
         var pi = commandType.GetProperty("BindByName");
         if (pi == null) throw new InvalidOperationException("BindByName property not found on type " + commandType.FullName);
         BindByNameSetter = PropertyHelper.GetPropertySetter(pi);
     }
     BindByNameSetter(command, true);
     return command;
 }
Example #22
0
 /// <summary>
 /// 用配置的属性值校验规则校验属性值,校验规则用javascript代码书写。为了方便在xaml中书写,
 /// 用and代替&&,用or代替||,用^代替小于号,在调用javascript前注意转换。
 /// </summary>
 /// <param name="value">属性值</param>
 /// <param name="setter">属性配置,里面有用javascript写的校验规则</param>
 /// <returns></returns>
 private bool ValidateValue(object value, PropertySetter setter)
 {
     //空值不用进行属性值校验
     if (value == null)
     {
         return true;
     }
     bool result = (bool)setter.Validation.DynamicInvoke(new object[] { value}); 
     return result;
 }