当前位置: 首页>>代码示例>>C#>>正文


C# Entity.SetProperty方法代码示例

本文整理汇总了C#中Entity.SetProperty方法的典型用法代码示例。如果您正苦于以下问题:C# Entity.SetProperty方法的具体用法?C# Entity.SetProperty怎么用?C# Entity.SetProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Entity的用法示例。


在下文中一共展示了Entity.SetProperty方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SyncSelectionToValue_Single

        /// <summary>
        /// 单选模式下的写值操作
        /// </summary>
        /// <param name="curEntity">正在编辑的实体。</param>
        /// <param name="svm">选择视图模型</param>
        /// <param name="selectedEntities">当前被选择的实体</param>
        private SetSelectionResult SyncSelectionToValue_Single(Entity curEntity, SelectionViewMeta svm, IList<Entity> selectedEntities)
        {
            bool success = false;

            //引用属性,应该先尝试设置实体属性,再设置 Id 属性。
            var rp = this.Meta.PropertyMeta.ManagedProperty as IRefProperty;
            if (selectedEntities.Count > 0)
            {
                var selectedEntity = selectedEntities[0] as Entity;
                if (rp != null)
                {
                    //如果 SelectedValuePath 是一个引用属性,或者直接就是一个实体属性,
                    //则应该获取相应的实体的值。
                    var valuePath = svm.SelectedValuePath;
                    if (valuePath != null)
                    {
                        if (valuePath is IRefProperty)
                        {
                            selectedEntity = selectedEntity.GetRefEntity((valuePath as IRefProperty).RefEntityProperty);
                        }
                        else if (rp.RefEntityType.IsAssignableFrom(valuePath.PropertyType))
                        {
                            selectedEntity = this.GetSelectedValue(selectedEntity) as Entity;
                        }
                    }

                    //设置实体到本引用属性上。
                    this.OnReferenceEntityChanging();
                    curEntity.SetRefEntity(rp.RefEntityProperty, selectedEntity, ManagedPropertyChangedSource.FromUIOperating);
                    success = curEntity.GetRefEntity(rp.RefEntityProperty) == selectedEntity;
                    if (success) { this.OnReferenceEntityChanged(); }
                }
                else
                {
                    var value = this.GetSelectedValue(selectedEntity);
                    this.PropertyValue = value;
                    success = this.PropertyValue == value;
                    if (success && svm.RefIdHost != null)
                    {
                        curEntity.SetProperty(svm.RefIdHost, selectedEntity.Id, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
            }
            else
            {
                if (rp != null)
                {
                    this.OnReferenceEntityChanging();
                    curEntity.SetRefEntity(rp.RefEntityProperty, null, ManagedPropertyChangedSource.FromUIOperating);
                    success = curEntity.GetRefEntity(rp.RefEntityProperty) == null;
                    if (success) { this.OnReferenceEntityChanged(); }
                }
                else
                {
                    this.PropertyValue = null;
                    success = this.PropertyValue == null;
                    if (success && svm.RefIdHost != null)
                    {
                        curEntity.SetProperty(svm.RefIdHost, null, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
            }

            return success ? SetSelectionResult.Success : SetSelectionResult.Cancel;
        }
开发者ID:569550384,项目名称:Rafy,代码行数:71,代码来源:EntitySelectionPropertyEditor.cs

示例2: SyncSelectionToValue_Multiple

        /// <summary>
        /// 多选模式下的写值操作
        /// </summary>
        /// <param name="curEntity">正在编辑的实体。</param>
        /// <param name="svm">选择视图模型</param>
        /// <param name="selectedEntities">当前被选择的实体</param>
        private SetSelectionResult SyncSelectionToValue_Multiple(Entity curEntity, SelectionViewMeta rvm, IList<Entity> selectedEntities)
        {
            var result = string.Join(rvm.SplitterIfMulti, selectedEntities.Select(i => this.GetSelectedValue(i)));

            //赋值给this.PropertyValue
            this.PropertyValue = result;

            SetSelectionResult res = (this.PropertyValue as string) == result ? SetSelectionResult.Success : SetSelectionResult.Cancel;
            if (res == SetSelectionResult.Success)
            {
                //此时这个属性应该是一个 int 数组类型
                if (rvm.RefIdHost != null)
                {
                    var idArray = selectedEntities.Select(i => i.Id).ToArray();
                    curEntity.SetProperty(rvm.RefIdHost, idArray, ManagedPropertyChangedSource.FromUIOperating);
                }
            }

            return res;
        }
开发者ID:569550384,项目名称:Rafy,代码行数:26,代码来源:EntitySelectionPropertyEditor.cs

示例3: TrySetProperty

        private void TrySetProperty(Entity e, string pName, JToken value)
        {
            //有些小写的客户端数据被传输到了服务端,需要被过滤掉。
            if (char.IsLower(pName[0])) { return; }

            var pm = _entityMeta.Property(pName) as PropertyMeta ??
                _entityMeta.ChildrenProperty(pName);
            if (pm != null)
            {
                var mp = pm.ManagedProperty;
                if (mp is IListProperty)
                {
                    //todo: 此处的性能可能需要优化,聚合保存子列表时,重复的查询 Repository
                    var entityType = EntityMatrix.FindByList(pm.PropertyType).EntityType;
                    var repo = RF.Find(entityType);

                    //列表属性的设置不能使用 SetProperty,否则,list.Parent 将会无值。
                    //但是也不能直接使用 LoadProperty,否则会导致调用 list.MarkOld,从而不会保存这个列表。
                    //所以只能先装载一个空列表,然后再把 json 中的数据转换为实体加入到这个列表中。
                    var list = repo.NewList();
                    e.LoadProperty(mp, list);

                    ListReader.JsonToEntity(value as JObject, repo, list);
                }
                else
                {
                    var rawValue = (value as JValue).Value;

                    rawValue = EntityJsonConverter.ToServerValue(pm.PropertyType, rawValue);

                    e.SetProperty(mp, rawValue, ManagedPropertyChangedSource.FromUIOperating);
                }
            }
            else
            {
                var rawValue = (value as JValue).Value;

                //如果没有找到一般的属性,则尝试查找外键属性
                for (int i = 0, c = _refIdProperties.Count; i < c; i++)
                {
                    var rip = _refIdProperties[i];
                    if (rip.Name == pName)
                    {
                        e.SetRefId(rip, rawValue);
                        break;
                    }
                }
                //只读属性。
                //if(notFound)
                //{
                //    throw new InvalidOperationException("没有在实体中找到这个属性:" + pName);
                //}
            }
        }
开发者ID:569550384,项目名称:Rafy,代码行数:54,代码来源:EntityPropertySetter.cs

示例4: CalculateCollectValue

 private static void CalculateCollectValue(Entity entity, IManagedProperty property, ManagedPropertyChangedEventArgs args)
 {
     var distance = Convert.ToDouble(args.NewValue) - Convert.ToDouble(args.OldValue);
     var oldValue = Convert.ToDouble(entity.GetProperty(property));
     entity.SetProperty(property, oldValue + distance);
 }
开发者ID:569550384,项目名称:Rafy,代码行数:6,代码来源:AutoCollectHelper.cs

示例5: DeserializeProperties

        /// <summary>
        /// 遍历 JSON 对象的属性,并使用托管属性 API 来设置一般属性的值。
        /// </summary>
        /// <param name="jObject"></param>
        /// <param name="entity"></param>
        private void DeserializeProperties(JObject jObject, Entity entity)
        {
            var properties = entity.PropertiesContainer.GetAvailableProperties();
            foreach (var propertyValue in jObject)
            {
                var propertyName = propertyValue.Key;
                var jValue = propertyValue.Value;
                var mp = properties.Find(propertyName, true);
                if (mp != null)
                {
                    //id 属性需要提前处理,而不需要在这里直接反序列化。
                    if (mp == Entity.IdProperty) continue;

                    if (mp is IListProperty)
                    {
                        DeserializeList(entity, mp as IListProperty, jValue as JArray);
                    }
                    else if (mp is IRefEntityProperty)
                    {
                        //一般引用属性不支持反序列化。
                    }
                    //一般属性。
                    else
                    {
                        object value = null;

                        //对于数组的泛型列表类型,需要进行特殊的处理。
                        if (jValue is JArray)
                        {
                            var jArray = jValue as JArray;
                            var propertyType = mp.PropertyType;
                            if (propertyType.IsArray)
                            {
                                var elementType = propertyType.GetElementType();
                                var array = Array.CreateInstance(elementType, jArray.Count);
                                for (int i = 0, c = jArray.Count; i < c; i++)
                                {
                                    var itemTyped = TypeHelper.CoerceValue(elementType, jArray[i]);
                                    array.SetValue(itemTyped, i);
                                }
                                value = array;
                            }
                            else if (TypeHelper.IsGenericType(propertyType, typeof(List<>)))//List<string>
                            {
                                var elementType = propertyType.GetGenericArguments()[0];
                                var list = Activator.CreateInstance(propertyType) as IList;
                                for (int i = 0, c = jArray.Count; i < c; i++)
                                {
                                    var itemTyped = TypeHelper.CoerceValue(elementType, jArray[i]);
                                    list.Add(itemTyped);
                                }
                                value = list;
                            }
                            else
                            {
                                //如果不是数组类型或者泛型列表类型的属性,则不支持反序列化。
                                //do nothing;
                            }
                        }
                        else
                        {
                            value = (jValue as JValue).Value;

                            if (value is string && mp.PropertyType == typeof(byte[]))
                            {
                                value = Convert.FromBase64String(value as string);
                            }
                        }

                        entity.SetProperty(mp, value, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
                else
                {
                    //如果指定了状态,则主动设置该实体的状态。
                    if (propertyName.EqualsIgnoreCase(PersistenceStatusProperty))
                    {
                        var value = (jValue as JValue).Value;
                        var status = (PersistenceStatus)Enum.Parse(typeof(PersistenceStatus), value.ToString(), true);
                        entity.PersistenceStatus = status;
                    }
                }
            }

            //可以使用 Json.NET 来遍历给实体属性赋值。
            //using (var jsonTextReader = new StringReader(strContent))
            //{
            //    var jsonSerializer = JsonSerializer.Create(this.SerializerSettings);
            //    jsonSerializer.Populate(jsonTextReader, entity);
            //}
        }
开发者ID:569550384,项目名称:Rafy,代码行数:96,代码来源:AggtDeserializer.cs

示例6: TryModifyName

 /// <summary>
 /// 有必要的话,就修改Name
 /// </summary>
 /// <param name="newObject"></param>
 private void TryModifyName(Entity newObject, bool modifyName)
 {
     if (modifyName)
     {
         if (newObject is IHasHame)
         {
             var no = newObject as IHasHame;
             no.Name += "-新增";
         }
         else
         {
             var mp = newObject.PropertiesContainer.GetAvailableProperties().Find("Name");
             if (mp != null && !mp.IsReadOnly)
             {
                 string name = newObject.GetProperty(mp).ToString();
                 newObject.SetProperty(mp, name + "-新增");
             }
         }
     }
 }
开发者ID:569550384,项目名称:Rafy,代码行数:24,代码来源:CopyAndNewCommand.cs


注:本文中的Entity.SetProperty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。