Exemplo n.º 1
0
        /// <summary>
        /// Gets field data from field path.
        /// </summary>
        public static ViewFieldData FromViewFieldPath(View sourceView, string viewFieldPath)
        {
            if (String.IsNullOrEmpty(viewFieldPath) || sourceView == null)
            {
                return(null);
            }

            ViewFieldData fieldData = new ViewFieldData();

            fieldData.TargetView = sourceView;
            fieldData.SourceView = sourceView;

            var viewTypeData      = sourceView.ViewTypeData;
            var viewFieldPathInfo = viewTypeData.GetViewFieldPathInfo(viewFieldPath);

            if (viewFieldPathInfo != null)
            {
                fieldData.ViewFieldPathInfo = viewFieldPathInfo;
                return(fieldData);
            }
            else
            {
                fieldData.ViewFieldPathInfo = new ViewFieldPathInfo();
                fieldData.ViewFieldPathInfo.ViewFieldTypeName   = sourceView.ViewTypeName;
                fieldData.ViewFieldPathInfo.ViewFieldPath       = viewFieldPath;
                fieldData.ViewFieldPathInfo.TargetViewFieldPath = viewFieldPath;
            }

            Type viewType   = typeof(View);
            var  viewFields = viewFieldPath.Split('.');

            // do we have a view field path consisting of multiple view fields?
            if (viewFields.Length > 1)
            {
                // yes. get first view field
                var firstViewField = viewFields[0];

                // is this a field that refers to another view?
                var fieldInfo = sourceView.GetType().GetField(firstViewField);
                if (fieldInfo != null && viewType.IsAssignableFrom(fieldInfo.FieldType))
                {
                    // yes. set target view and return
                    fieldData.ViewFieldPathInfo.TargetViewFieldPath = String.Join(".", viewFields.Skip(1).ToArray());
                    fieldData.ViewFieldPathInfo.MemberInfo.Add(fieldInfo);
                    fieldData.ViewFieldPathInfo.IsMapped     = true;
                    fieldData.ViewFieldPathInfo.IsPathParsed = true;
                    fieldData.TargetViewSet = false;
                    viewTypeData.AddViewFieldPathInfo(viewFieldPath, fieldData.ViewFieldPathInfo);
                    return(fieldData);
                }

                // is this a property that refers to a view?
                var propertyInfo = sourceView.GetType().GetProperty(firstViewField);
                if (propertyInfo != null && viewType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    // yes. set target view and return
                    fieldData.ViewFieldPathInfo.TargetViewFieldPath = String.Join(".", viewFields.Skip(1).ToArray());
                    fieldData.ViewFieldPathInfo.MemberInfo.Add(propertyInfo);
                    fieldData.ViewFieldPathInfo.IsMapped     = true;
                    fieldData.ViewFieldPathInfo.IsPathParsed = true;
                    fieldData.TargetViewSet = false;
                    viewTypeData.AddViewFieldPathInfo(viewFieldPath, fieldData.ViewFieldPathInfo);
                    return(fieldData);
                }

                // does first view field or property exist?
                if (fieldInfo == null && propertyInfo == null)
                {
                    // no. check if it refers to a view in the hierarchy
                    var result = fieldData.TargetView.Find <View>(x => x.Id == viewFields[0], true, fieldData.TargetView);
                    if (result == null)
                    {
                        // no. assume that it refers to this view (in cases like x.SetValue(() => x.Field, value))
                        return(FromViewFieldPath(sourceView, String.Join(".", viewFields.Skip(1).ToArray())));
                    }

                    // view found
                    fieldData.ViewFieldPathInfo.TargetViewFieldPath = String.Join(".", viewFields.Skip(1).ToArray());
                    fieldData.ViewFieldPathInfo.IsMapped            = true;
                    fieldData.TargetViewSet = true;
                    fieldData.TargetView    = result;
                    return(fieldData);
                }
            }

            // try parse the path
            fieldData.ParseViewFieldPath();
            return(fieldData);
        }
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public ViewFieldBindingSource(ViewFieldData viewFieldData, bool negateValue = false)
 {
     ViewFieldData = viewFieldData;
     NegateValue   = negateValue;
 }