Exemple #1
0
        internal static void RestoreDynamicControlsFrom <TActivityUi>(this TActivityUi source, StandardConfigurationControlsCM destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            var dynamicControlsCollection = Fr8ReflectionHelper.GetMembers(source.GetType()).Where(x => x.CanRead &&
                                                                                                   x.GetCustomAttribute <DynamicControlsAttribute>() != null &&
                                                                                                   Fr8ReflectionHelper.CheckIfMemberIsCollectionOf <IControlDefinition>(x))
                                            .ToDictionary(x => x.Name, x => x);

            if (dynamicControlsCollection.Count > 0)
            {
                foreach (var control in destination.Controls)
                {
                    var nameAndOwner = GetDynamicControlNameAndOwner(control.Name);
                    if (String.IsNullOrEmpty(nameAndOwner.Item2))
                    {
                        continue;
                    }
                    IMemberAccessor member;

                    if (!dynamicControlsCollection.TryGetValue(nameAndOwner.Item2, out member))
                    {
                        continue;
                    }

                    var controlsCollection = (IList)member.GetValue(source);

                    if (controlsCollection == null && (!member.CanWrite || member.MemberType.IsAbstract || member.MemberType.IsInterface))
                    {
                        continue;
                    }

                    if (controlsCollection == null)
                    {
                        controlsCollection = (IList)Activator.CreateInstance(member.MemberType);
                        member.SetValue(source, controlsCollection);
                    }

                    control.Name = nameAndOwner.Item1;
                    controlsCollection.Add(control);
                }
            }
        }
 private void FillOwnerByControl()
 {
     foreach (var collectionProperty in Fr8ReflectionHelper.GetMembers(typeof(TActivityUi)).Where(x => x.CanRead &&
                                                                                                  x.GetCustomAttribute <DynamicControlsAttribute>() != null &&
                                                                                                  Fr8ReflectionHelper.CheckIfMemberIsCollectionOf <IControlDefinition>(x)))
     {
         var collection = collectionProperty.GetValue(_activityUi) as IEnumerable <IControlDefinition>;
         if (collection == null)
         {
             continue;
         }
         foreach (var control in collection)
         {
             _ownerNameByControl.Add(control, collectionProperty.Name);
         }
     }
 }
Exemple #3
0
        internal static void SaveDynamicControlsTo <TActivityUi>(this TActivityUi source, StandardConfigurationControlsCM destination)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            var insertIndex = 0;

            foreach (var member in Fr8ReflectionHelper.GetMembers(source.GetType()).Where(x => x.CanRead))
            {
                if (member.GetCustomAttribute <DynamicControlsAttribute>() != null && Fr8ReflectionHelper.CheckIfMemberIsCollectionOf <IControlDefinition>(member))
                {
                    var collection = member.GetValue(source) as IList;

                    if (collection != null)
                    {
                        foreach (var control in collection.Cast <object>().OfType <ControlDefinitionDTO>())
                        {
                            control.Name = GetDynamicControlName(control.Name, member.Name);
                            destination.Controls.Insert(insertIndex, control);
                            insertIndex++;
                        }
                    }
                }

                var controlDef = member.GetValue(source) as IControlDefinition;
                if (!String.IsNullOrWhiteSpace(controlDef?.Name))
                {
                    for (int i = 0; i < destination.Controls.Count; i++)
                    {
                        if (destination.Controls[i].Name == controlDef.Name)
                        {
                            insertIndex = i + 1;
                            break;
                        }
                    }
                }
            }
        }