Пример #1
0
        public PropModelType ParsePropModel(IPropBagTemplate pbt)
        {
            if (pbt.ClassName == "PersonCollectionViewModel")
            {
                System.Diagnostics.Debug.WriteLine($"We are processing the {nameof(pbt.ClassName)} PropItem.");
            }

            //DeriveFromClassModeEnum deriveFrom = pbt.DeriveFromClassMode;
            //Type targetType = pbt.TargetType;

            //TypeInfoField wrapperTypeInfoField = GetWrapperTypeInfo(pbt);
            //Type targetTypeFromWTInfoField = GetTypeFromInfoField(wrapperTypeInfoField, PropKindEnum.Prop, targetType, out Type itemTypeDummy);

            PropModelType result = new PropModel
                                   (
                className: pbt.ClassName,
                namespaceName: pbt.OutPutNameSpace,
                deriveFrom: pbt.DeriveFromClassMode,
                targetType: pbt.TargetType,
                propFactory: null,
                propFactoryType: pbt.PropFactoryType,
                propModelCache: null,
                typeSafetyMode: pbt.TypeSafetyMode,
                deferMethodRefResolution: pbt.DeferMethodRefResolution,
                requireExplicitInitialValue: pbt.RequireExplicitInitialValue
                                   );

            // Add Namespaces
            int namespacesCount = pbt.Namespaces == null ? 0 : pbt.Namespaces.Count;

            for (int nsPtr = 0; nsPtr < namespacesCount; nsPtr++)
            {
                result.Namespaces.Add(pbt.Namespaces[nsPtr].Namespace);
            }

            DoWhenChangedHelper doWhenChangedHelper = new DoWhenChangedHelper();

            int propsCount = pbt.Props == null ? 0 : pbt.Props.Count;

            // Parse each Prop Item
            for (int propPtr = 0; propPtr < propsCount; propPtr++)
            {
                IPropTemplateItem pi = pbt.Props[propPtr];

                try
                {
                    IPropItemModel rpi = ProcessProp(pi, doWhenChangedHelper);
                    result.Add(rpi.PropertyName, rpi);
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException($"Error: {e.Message} occured while parsing PropItem: {pi.PropertyName} for class: {pbt.ClassName}.", e);
                }
            }

            return(result);
        }
Пример #2
0
        private IPropItemModel ProcessProp(IPropTemplateItem pi, DoWhenChangedHelper doWhenChangedHelper)
        {
            PropStorageStrategyEnum storageStrategy = pi.StorageStrategy;
            bool   typeIsSolid = pi.TypeIsSolid;
            string extraInfo   = pi.ExtraInfo;

            IPropItemModel rpi = new PropItemModel
                                 (
                type: pi.PropertyType,
                name: pi.PropertyName,
                storageStrategy: storageStrategy,
                typeIsSolid: typeIsSolid,
                propKind: pi.PropKind,
                propTypeInfoField: null,
                initialValueField: null,
                extraInfo: extraInfo,
                comparer: null,
                itemType: null,
                binderField: null,
                mapperRequest: null,
                propCreator: null
                                 );

            bool isCProp            = pi.PropKind.IsCollection();
            bool foundTypeInfoField = false;

            ItemCollection items = ((PropItem)pi).Items;

            foreach (Control uc in items)
            {
                // ToDo: Find and process this field first, and then enter the enclosing foreach loop.
                // because one day, some of the other processing may depend on the PropertyType.

                // Type Info Field
                if (uc is DRM.PropBagControlsWPF.TypeInfoField tif)
                {
                    foundTypeInfoField = true;
                    Type propertyType = GetTypeFromInfoField(tif, pi.PropKind, pi.PropertyType, out Type itemType);
                    if (isCProp)
                    {
                        rpi.CollectionType = propertyType;
                        rpi.ItemType       = itemType;
                    }
                    else
                    {
                        rpi.PropertyType = propertyType;
                        rpi.ItemType     = null;
                    }
                }

                // Initial Value Field
                else if (uc is InitialValueField ivf)
                {
                    IPropInitialValueField rivf;

                    // TODO: Add error handling here.
                    if (ivf.PropBagFullClassName != null)
                    {
                        rivf = PropInitialValueField.FromPropBagFCN(ivf.PropBagFullClassName);
                    }
                    else if (ivf.CreateNew)
                    {
                        rivf = PropInitialValueField.UseCreateNew;
                    }
                    else
                    {
                        rivf = new PropInitialValueField(ivf.InitialValue, ivf.SetToDefault, ivf.SetToUndefined,
                                                         ivf.SetToNull, ivf.SetToEmptyString);
                    }

                    rpi.InitialValueField = rivf;
                }

                // Do When Changed Field
                else if (uc is DRM.PropBagControlsWPF.PropDoWhenChangedField dwc)
                {
                    MethodInfo mi = doWhenChangedHelper.GetMethodAndSubKind(dwc, rpi.PropertyType, rpi.PropertyName, out SubscriptionKind subscriptionKind);

                    SubscriptionPriorityGroup priorityGroup = dwc?.DoAfterNotify ?? false ? SubscriptionPriorityGroup.Last : SubscriptionPriorityGroup.Standard;

                    IPropDoWhenChangedField rdwc = new DRM.PropBag.PropDoWhenChangedField
                                                   (
                        target: null, method: mi,
                        subscriptionKind: subscriptionKind, priorityGroup: priorityGroup,
                        methodIsLocal: true, declaringType: null,
                        fullClassName: null, instanceKey: null
                                                   );

                    rpi.DoWhenChangedField = rdwc;
                }

                // Comparer Field
                else if (uc is DRM.PropBagControlsWPF.PropComparerField pcf)
                {
                    IPropComparerField rpcf = new PropBag.PropComparerField(pcf.ComparerFunc.Comparer, pcf.UseRefEquality);

                    rpi.ComparerField = rpcf;
                }

                // Local Binder Field
                else if (uc is DRM.PropBagControlsWPF.PropBinderField binderField)
                {
                    IPropBinderField rBinderField = new PropBag.PropBinderField(binderField.Path);

                    rpi.BinderField = rBinderField;
                    rpi.MapperRequestResourceKey = binderField.MapperRequestResourceKey;
                }
            }

            if (!foundTypeInfoField)
            {
                if (isCProp)
                {
                    Type propertyType = GetTypeFromInfoField(null, pi.PropKind, pi.PropertyType, out Type itemType);
                    rpi.CollectionType = propertyType;
                    rpi.ItemType       = itemType;
                }
                else if (pi.PropKind == PropKindEnum.CollectionView)
                {
                    rpi.PropertyType   = typeof(ListCollectionView);
                    rpi.CollectionType = rpi.PropertyType;
                    rpi.ItemType       = pi.PropertyType;
                }
                else
                {
                    // TODO: Check other PropKinds.
                    // Do Nothing.
                }
            }

            return(rpi);
        }