GetPropertyValue() public méthode

Gets the value of a property.
/// If there is no such property, if the property isn't readable, or /// if getting the property value throws an exception. ///
public GetPropertyValue ( IExpression propertyExpression ) : object
propertyExpression IExpression /// The property expression that should be used to retrieve the property value. ///
Résultat object
    /// <summary>
    /// 取得該語言的內容
    /// </summary>
    /// <param name="object">抓取的物件</param>
    /// <param name="fieldNamePrefix">抓取欄位的名稱(不包含Language的字,預設中文是沒有lng的字眼)</param>
    /// <returns></returns>
    public string GetContent(object obj, string fieldNamePrefix)
    {
        string content = string.Empty;
        string fieldName = fieldNamePrefix;
        if (!WebLanguageUtil.Language.Cht.ToString().Equals(m_SessionHelper.FrontLanguage))
        {
            fieldName += m_SessionHelper.FrontLanguage.ToUpper();
        }
        m_Log.Info("取得內容的欄位:" + fieldName);

        try
        {
            ObjectWrapper ow = new ObjectWrapper(obj);
            object value = ow.GetPropertyValue(fieldName);
            if (value != null)
            {
                content = value.ToString();
            }
        }
        catch (Exception ex)
        {
            m_Log.Debug(ex);
        }

        return content;
    }
Exemple #2
0
            /// <summary>
            /// Applies attributes to the proxy class.
            /// </summary>
            /// <param name="typeBuilder">The type builder to use.</param>
            /// <param name="targetType">The proxied class.</param>
            /// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/>
            /// <see cref="IProxyTypeBuilder.TypeAttributes"/>
            protected override void ApplyTypeAttributes(TypeBuilder typeBuilder, Type targetType)
            {
                foreach (object attr in GetTypeAttributes(targetType))
                {
                    if (attr is CustomAttributeBuilder)
                    {
                        typeBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
                    }
#if NET_2_0
                    else if (attr is CustomAttributeData)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
                    }
#endif
                    else if (attr is Attribute)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((Attribute)attr));
                    }
                    else if (attr is IObjectDefinition)
                    {
                        RootObjectDefinition objectDefinition = (RootObjectDefinition) attr;

                        //TODO check that object definition is for an Attribute type.

                        //Change object definition so it can be instantiated and make prototype scope.
                        objectDefinition.IsAbstract = false;
                        objectDefinition.IsSingleton = false;
                        string objectName = ObjectDefinitionReaderUtils.GenerateObjectName(objectDefinition, objectFactory);
                        objectFactory.RegisterObjectDefinition(objectName, objectDefinition);
                        

                        //find constructor and constructor arg values to create this attribute.                       
                        ConstructorResolver constructorResolver = new ConstructorResolver(objectFactory, objectFactory,
                                                                               new SimpleInstantiationStrategy(),
                                                                               new ObjectDefinitionValueResolver(objectFactory));

                        
                        ConstructorInstantiationInfo ci = constructorResolver.GetConstructorInstantiationInfo(objectName,
                                                                                                              objectDefinition,
                                                                                                              null, null);

                        if (objectDefinition.PropertyValues.PropertyValues.Length == 0)
                        {
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo,
                                                                                    ci.ArgInstances);
                            typeBuilder.SetCustomAttribute(cab);
                        }
                        else
                        {
                            object attributeInstance = objectFactory.GetObject(objectName);
                            IObjectWrapper wrappedAttributeInstance = new ObjectWrapper(attributeInstance);
                            PropertyInfo[] namedProperties = wrappedAttributeInstance.GetPropertyInfos();
                            object[] propertyValues = new object[namedProperties.Length];
                            for (int i = 0; i < namedProperties.Length; i++)
                            {
                                propertyValues[i] =
                                    wrappedAttributeInstance.GetPropertyValue(namedProperties[i].Name);
                            }
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo, ci.ArgInstances, 
                                                                                    namedProperties, propertyValues);
                            typeBuilder.SetCustomAttribute(cab);
                        }


                    }
                    
                }
            }
        /// <summary>
        /// Convert QueryResults by generate decorate serializable class with transformed values.
        /// </summary>
        /// <param name="dynamicPageService"></param>
        /// <param name="results"></param>
        /// <returns></returns>
        private static IEnumerable ConvertQueryResults(IDynamicPage dynamicPageService, IEnumerable results)
        {
            IEnumerable<object> originalObjects = results.Cast<object>();
            if (originalObjects.Count() == 0) return results;

            IEnumerable<PropertyDefinition> propertyDecorateConfigs = CreatePropertyDecorateConfigs(dynamicPageService);
            IEnumerable<object> convertResults = null;

            try
            {
                convertResults = ClassDecorator.CreateDecorateObjects(originalObjects, propertyDecorateConfigs.ToArray()).Cast<object>();
            }
            catch (CompileException exp)
            {
                string loggingMessage = string.Format(CultureInfo.InvariantCulture, "There has errors to compile a dynamic class from dynamic page configuration to render UI grid of the dynamic page with object id equals to \"{0}\".", QueryStringUtility.ObjectId);
                Logger.Instance(typeof(DynamicPageDataServiceHandler)).Error(loggingMessage, exp);
                throw;
            }

            ObjectWrapper convertResultObjectWrapper = new ObjectWrapper(convertResults.First().GetType());
            for (int i = 0; i < originalObjects.Count(); i++)
            {
                object originalObject = originalObjects.ElementAt(i);
                GridRowControlBindEventArgs arg = new GridRowControlBindEventArgs(originalObject);
                dynamicPageService.OnGridRowControlsBind(arg);
                convertResultObjectWrapper.WrappedInstance = convertResults.ElementAt(i);

                // set visibility of checkbox
                bool showCheckBoxColumn = (bool)convertResultObjectWrapper.GetPropertyValue(DynamicPageDataServiceHandler.ShowCheckBoxColumnPropertyName);
                convertResultObjectWrapper.SetPropertyValue(DynamicPageDataServiceHandler.ShowCheckBoxColumnPropertyName, showCheckBoxColumn && arg.ShowCheckBoxColumn);

                // set visibility of edit button
                string permissionValue = string.Format(CultureInfo.InvariantCulture, "{0}.Update", dynamicPageService.Configuration.PermissionValue);
                bool hasUpdatePermission = permissionBridge.HasPermission(permissionValue);
                bool showEditButtonColumn = (bool)convertResultObjectWrapper.GetPropertyValue(DynamicPageDataServiceHandler.ShowEditButtonColumnPropertyName);
                convertResultObjectWrapper.SetPropertyValue(DynamicPageDataServiceHandler.ShowEditButtonColumnPropertyName, showEditButtonColumn && arg.ShowEditButton && hasUpdatePermission);

                // set visibility of view button
                bool showViewButtonColumn = (bool)convertResultObjectWrapper.GetPropertyValue(DynamicPageDataServiceHandler.ShowViewButtonColumnPropertyName);
                convertResultObjectWrapper.SetPropertyValue(DynamicPageDataServiceHandler.ShowViewButtonColumnPropertyName, showViewButtonColumn && arg.ShowViewButton);

                // set visibility of delete button
                permissionValue = string.Format(CultureInfo.InvariantCulture, "{0}.Delete", dynamicPageService.Configuration.PermissionValue);
                bool hasDeletePermission = permissionBridge.HasPermission(permissionValue);
                bool showDeleteButtonColumn = (bool)convertResultObjectWrapper.GetPropertyValue(DynamicPageDataServiceHandler.ShowDeleteButtonColumnPropertyName);
                convertResultObjectWrapper.SetPropertyValue(DynamicPageDataServiceHandler.ShowDeleteButtonColumnPropertyName, showDeleteButtonColumn && arg.ShowDeleteButton && hasDeletePermission);
            }

            return convertResults;
        }
    /// <summary>
    /// 取得該語言的內容
    /// </summary>
    /// <param name="object">抓取的物件</param>
    /// <param name="fieldNamePrefix">抓取欄位的名稱(不包含Language的字,預設中文是沒有lng的字眼)</param>
    /// <param name="dateFormate">日期格式</param>
    /// <returns></returns>
    public string GetDateContent(object obj, string fieldName, string dateFormate)
    {
        string content = string.Empty;
        m_Log.Info("取得內容的欄位:" + fieldName);

        try
        {
            ObjectWrapper ow = new ObjectWrapper(obj);
            object value = ow.GetPropertyValue(fieldName);
            //預設日期格式
            if (string.IsNullOrEmpty(dateFormate))
            {
                dateFormate = "MMMM dd, yyyy";
            }

            if (value != null)
            {
                //只有日期欄位才做
                if (ow.GetPropertyType(fieldName) == typeof(DateTime) || ow.GetPropertyType(fieldName) == typeof(DateTime?))
                {
                    //中文格式一種,其他語言一種,目前是兩個都設定一樣
                    if (WebLanguageUtil.Language.Cht.ToString().Equals(m_SessionHelper.FrontLanguage))
                    {
                        DateTime dateTime = (DateTime)value;
                        content = dateTime.ToString(dateFormate, new System.Globalization.CultureInfo("en-us"));
                        //content = dateTime.ToString(dateFormate, new System.Globalization.CultureInfo("zh-tw"));
                    }
                    else
                    {
                        DateTime dateTime = (DateTime)value;
                        content = dateTime.ToString(dateFormate, new System.Globalization.CultureInfo("en-us"));
                    }
                }
                //else if (ow.GetPropertyType(fieldName) == typeof(DateTime?))
                //{
                //}
            }
        }
        catch (Exception ex)
        {
            m_Log.Debug(ex);
        }

        return content;
    }
        public void CreateDecorateObjectsWithoutConvertPropertyTypesTest()
        {
            var anonymousObjects = new[]
            {
                new { Name = "Eunge Liu", Career = "Software Development", Birthday = new DateTime(1982, 2, 7) },
                new { Name = "Lucy Liu", Career = "Financial", Birthday = new DateTime(1982, 5, 1) }
            };

            IEnumerable<PropertyDefinition> propertyDecorateConfigs = new PropertyDefinition[]
            {
                new PropertyDefinition(typeof(string)) { PropertyName = "Name", NewPropertyName = "Name" },
                new PropertyDefinition(typeof(string)) { PropertyName = "Career", NewPropertyName = "Career" },
                new PropertyDefinition(typeof(DateTime)) { PropertyName = "Birthday", NewPropertyName = "Birthday" },
            };

            IEnumerable<object> returnObjects = ClassDecorator.CreateDecorateObjects(anonymousObjects, propertyDecorateConfigs).Cast<object>();
            Assert.IsNotNull(returnObjects);
            Assert.AreEqual(2, returnObjects.Count());

            IObjectWrapper objectWrapper = new ObjectWrapper(returnObjects.First());
            Assert.AreEqual("Eunge Liu", objectWrapper.GetPropertyValue("Name"));
            Assert.AreEqual("Software Development", objectWrapper.GetPropertyValue("Career"));
            Assert.AreEqual(new DateTime(1982, 2, 7), objectWrapper.GetPropertyValue("Birthday"));

            objectWrapper = new ObjectWrapper(returnObjects.Last());
            Assert.AreEqual("Lucy Liu", objectWrapper.GetPropertyValue("Name"));
            Assert.AreEqual("Financial", objectWrapper.GetPropertyValue("Career"));
            Assert.AreEqual(new DateTime(1982, 5, 1), objectWrapper.GetPropertyValue("Birthday"));
        }
        public void CreateDecorateObjectsWithConvertPropertyTypesTest()
        {
            var anonymousObjects = new[]
            {
                new { Name = "Eunge Liu", Career = "Software Development", Birthday = new DateTime(1982, 2, 7), Salary = 13000 },
                new { Name = "Lucy Liu", Career = "Financial", Birthday = new DateTime(1982, 5, 1), Salary = 3000 }
            };

            IEnumerable results = ClassDecorator.CreateDecorateObjects(anonymousObjects, new[]
                {
                    new PropertyDefinition(typeof(string)) { PropertyName = "Name" },
                    new PropertyDefinition(typeof(string)) { PropertyName = "Career" },
                    new PropertyDefinition(typeof(string)) { PropertyName = "Birthday", PropertyValueConvertCallback = sender => { return ((DateTime)sender).ToString("yyyy-MM-dd") ;}},
                    new PropertyDefinition(typeof(string)) { PropertyName = "Salary" },
                });
            IEnumerable<object> returnObjects = results.Cast<object>();
            Assert.IsNotNull(returnObjects);
            Assert.AreEqual(2, returnObjects.Count());

            IObjectWrapper objectWrapper = new ObjectWrapper(returnObjects.First());
            Assert.AreEqual("Eunge Liu", objectWrapper.GetPropertyValue("Name"));
            Assert.AreEqual("Software Development", objectWrapper.GetPropertyValue("Career"));
            Assert.AreEqual("1982-02-07", objectWrapper.GetPropertyValue("Birthday"));
            Assert.AreEqual("13000", objectWrapper.GetPropertyValue("Salary"));

            objectWrapper = new ObjectWrapper(returnObjects.Last());
            Assert.AreEqual("Lucy Liu", objectWrapper.GetPropertyValue("Name"));
            Assert.AreEqual("Financial", objectWrapper.GetPropertyValue("Career"));
            Assert.AreEqual("1982-05-01", objectWrapper.GetPropertyValue("Birthday"));
            Assert.AreEqual("3000", objectWrapper.GetPropertyValue("Salary"));
        }
        public void CreateDecorateObjectsForTypeWithDictionaryPropertyTest()
        {
            CodeDomTestObject obj1 = new CodeDomTestObject { Name = "Eunge" };
            obj1.Properties["AnnualPackage"] = 210000;
            obj1.Properties["Birthday"] = new DateTime(1982, 2, 7);
            obj1.Properties["Position"] = "Engineer";

            IEnumerable results = ClassDecorator.CreateDecorateObjects(new[] { obj1 }, new[]
                {
                    new PropertyDefinition(typeof(string)) { PropertyName = "Name" },
                    new PropertyDefinition(typeof(int))
                    {
                        PropertyName = @"Properties[""AnnualPackage""]",
                        NewPropertyName = FieldNameTransformUtility.DataBoundFieldName(@"Properties[""AnnualPackage""]", 1),
                    },
                    new PropertyDefinition(typeof(DateTime))
                    {
                        PropertyName = @"Properties[""Birthday""]",
                        NewPropertyName = FieldNameTransformUtility.DataBoundFieldName(@"Properties[""Birthday""]", 2),
                    },
                    new PropertyDefinition(typeof(string))
                    {
                        PropertyName = @"Properties[""Position""]",
                        NewPropertyName = FieldNameTransformUtility.DataBoundFieldName(@"Properties[""Position""]", 3),
                    }
                });

            IEnumerable<object> returnObjects = results.Cast<object>();
            Assert.IsNotNull(returnObjects);
            Assert.AreEqual(1, returnObjects.Count());

            IObjectWrapper objectWrapper = new ObjectWrapper(returnObjects.First());
            Assert.AreEqual("Eunge", objectWrapper.GetPropertyValue("Name"));

            string positionPropertyName = FieldNameTransformUtility.DataBoundFieldName(@"Properties[""Position""]", 3);
            Assert.AreEqual("Engineer", objectWrapper.GetPropertyValue(positionPropertyName));
        }