Beispiel #1
0
        private object getDataContextValue(object dataContext, string modelFieldName)
        {
            if (dataContext == null)
            {
                throw new Exception($"Error accessing [field: {modelFieldName}]. DataContext special field in model is null");
            }

            var fieldPath = new model.ModelFieldNamePathInfo(modelFieldName);

            if (dataContext is lib.BindableDynamicDictionary dynDict)
            {
                if (!dynDict.HasKey(fieldPath.Current))
                {
                    throw new Exception($"Field [{fieldPath.Current}] does not exist on DataContext object");
                }

                var val = dynDict[fieldPath.Current];
                if (fieldPath.ChildPath.Length > 0)
                {
                    return(getDataContextValue(val, fieldPath.ChildPath));
                }
                else
                {
                    return(val);
                }
            }
            else
            {
                Type dcType = dataContext.GetType();
                var  prop   = dcType.GetProperty(fieldPath.Current);
                if (prop == null)
                {
                    throw new Exception(
                              $"Field [{fieldPath.Current}] does not exist on DataContext object of [type: {dcType.Name}]");
                }

                var val = prop.GetValue(dataContext);

                if (fieldPath.ChildPath.Length > 0)
                {
                    if (val == null)
                    {
                        // warn that we have a child path but a parent is null
                        log.Warn($"DataContext warning.  Child Path found [FullPath: {modelFieldName}] but current [Path: {fieldPath.Current}] is null.  Would not be able to find [child path: {fieldPath.ChildPath}] while parent is null.");
                    }
                    return(getDataContextValue(val, fieldPath.ChildPath));
                }
                else
                {
                    return(val);
                }
            }
        }
Beispiel #2
0
        private void setDataContextValue(object dataContext, string modelFieldName, object val)
        {
            var fieldPath = new model.ModelFieldNamePathInfo(modelFieldName);

            if (fieldPath.ChildPath.Length > 0)
            {
                if (dataContext is lib.BindableDynamicDictionary dyncDict)
                {
                    // dynDict must contain this part of the path, because it's a path
                    if (!dyncDict.HasKey(fieldPath.Current))
                    {
                        throw new Exception($"Field [{fieldPath.Current}] does not exist on DataContext object");
                    }

                    var currentPathVal = dyncDict[fieldPath.Current];
                    setDataContextValue(currentPathVal, fieldPath.ChildPath, val);
                }
                else
                {
                    Type dcType = dataContext.GetType();
                    var  prop   = dcType.GetProperty(fieldPath.Current);
                    if (prop == null)
                    {
                        throw new Exception(
                                  $"Field [{fieldPath.Current}] does not exist on DataContext object of [type: {dcType.Name}]");
                    }

                    var currentPathVal = prop.GetValue(dataContext);
                    setDataContextValue(currentPathVal, fieldPath.ChildPath, val);
                }
            }
            else
            {
                // no child path so do the normal
                if (dataContext is lib.BindableDynamicDictionary dynDict)
                {
                    dynDict[fieldPath.Current] = val;
                }
                else
                {
                    Type dcType = dataContext.GetType();
                    var  prop   = dcType.GetProperty(fieldPath.Current);
                    if (prop == null)
                    {
                        throw new Exception(
                                  $"Field [{fieldPath.Current}] does not exist on DataContext object of [type: {dcType.Name}]");
                    }
                    prop.SetValue(dataContext, val);
                }
            }
        }