public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
            {
                //Construct properties list using visibility attributes

                List <PropertyDescriptor> lstProperties = new List <PropertyDescriptor>();

                PropertyDescriptorCollection properties = base.GetProperties(context, value, attributes);

                foreach (PropertyDescriptor property in properties)
                {
                    VisibilityAttribute visiblityAttribute = property.Attributes[typeof(VisibilityAttribute)] as VisibilityAttribute;
                    if (visiblityAttribute == null)
                    {
                        lstProperties.Add(property);
                    }
                    else
                    {
                        if (visiblityAttribute.CheckCondition(value))
                        {
                            lstProperties.Add(property);
                        }
                    }
                }

                return(new PropertyDescriptorCollection(lstProperties.ToArray()));
            }
Пример #2
0
    public static MvcHtmlString CustomDisplayFor <TModel, TResult>(this HtmlHelper <TModel> html,
                                                                   Expression <Func <TModel, TResult> > expression)
    {
        ExpressionType type = expression.Body.NodeType;

        if (type == ExpressionType.MemberAccess)
        {
            MemberExpression memberExpression = (MemberExpression)expression.Body;
            PropertyInfo     pi = memberExpression.Member as PropertyInfo;
            var attributes      = pi.GetCustomAttributes();
            foreach (var attribute in attributes)
            {
                if (attribute is VisibilityAttribute)
                {
                    VisibilityAttribute vi = attribute as VisibilityAttribute;
                    if (vi.Status)
                    {
                        var metadata = ModelMetadata.FromLambdaExpression <TModel, TResult>(expression, html.ViewData);
                        return(MvcHtmlString.Create(metadata.SimpleDisplayText));
                    }
                }
            }
        }
        return(MvcHtmlString.Create(""));
    }
Пример #3
0
        private bool GetCondition(SerializedProperty property, VisibilityAttribute visibility)
        {
            string parentPath = null;

            if (property.propertyPath.Contains("."))
            {
                parentPath = property.propertyPath.Substring(0, property.propertyPath.LastIndexOf(".") + 1);
            }

            var result = false;

            if (visibility.targetProperty.Contains("&"))
            {
                result = true;
                var props = visibility.targetProperty.Split('&');
                foreach (var prop in props)
                {
                    result &= GetPropertyCondition(property, parentPath, prop);
                }
            }
            else if (visibility.targetProperty.Contains("|"))
            {
                var props = visibility.targetProperty.Split('|');
                foreach (var prop in props)
                {
                    result |= GetPropertyCondition(property, parentPath, prop);
                }
            }
            else
            {
                result = GetPropertyCondition(property, parentPath, visibility.targetProperty);
            }

            return(result);
        }
Пример #4
0
        void LoadFieldAttributes()
        {
            //get input variables
            System.Reflection.FieldInfo[] fInfos = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            List <string> actualFields = new List <string>();

            anchorFieldInfoMap.Clear();

            foreach (var field in fInfos)
            {
                // reading field informations.

                actualFields.Add(field.Name);
                anchorFieldInfoMap[field.Name] = field;

                if (!anchorFieldDictionary.ContainsKey(field.Name))
                {
                    anchorFieldDictionary[field.Name] = CreateAnchorField();
                }

                AnchorField anchorField = anchorFieldDictionary[field.Name];

                //detect multi-anchor by checking for PWArray<T> type
                if (field.FieldType.IsGenericType)
                {
                    if (field.FieldType.GetGenericTypeDefinition() == typeof(PWArray <>))
                    {
                        anchorField.multiple = true;
                    }
                }

                System.Object[] attrs = field.GetCustomAttributes(true);
                foreach (var attr in attrs)
                {
                    InputAttribute       inputAttr       = attr as InputAttribute;
                    OutputAttribute      outputAttr      = attr as OutputAttribute;
                    ColorAttribute       colorAttr       = attr as ColorAttribute;
                    OffsetAttribute      offsetAttr      = attr as OffsetAttribute;
                    VisibilityAttribute  visibilityAttr  = attr as VisibilityAttribute;
                    NotRequiredAttribute notRequiredAttr = attr as NotRequiredAttribute;

                    if (inputAttr != null)
                    {
                        anchorField.anchorType = AnchorType.Input;
                        if (inputAttr.name != null)
                        {
                            anchorField.name = inputAttr.name;
                        }
                    }
                    if (outputAttr != null)
                    {
                        anchorField.anchorType = AnchorType.Output;
                        if (outputAttr.name != null)
                        {
                            anchorField.name = outputAttr.name;
                        }
                    }
                    if (colorAttr != null)
                    {
                        anchorField.color = new SerializableColor(colorAttr.color);
                    }
                    if (offsetAttr != null)
                    {
                        anchorField.offset  = offsetAttr.offset;
                        anchorField.padding = offsetAttr.padding;
                    }
                    if (notRequiredAttr != null)
                    {
                        anchorField.required = false;
                    }
                    if (visibilityAttr != null)
                    {
                        anchorField.defaultVisibility = visibilityAttr.visibility;
                    }
                }
                if (anchorField.anchorType == AnchorType.None)                 //field does not have a PW attribute
                {
                    anchorFieldDictionary.Remove(field.Name);
                }
                else
                {
                    //create anchor in this anchorField if there is not existing one
                    if (anchorField.anchors.Count == 0)
                    {
                        anchorField.CreateNewAnchor();
                    }

                    anchorField.colorSchemeName = ColorTheme.GetAnchorColorSchemeName(field.FieldType);
                    anchorField.fieldName       = field.Name;
                    anchorField.fieldType       = (SerializableType)field.FieldType;
                    anchorField.fieldValue      = field.GetValue(this);
                    anchorField.nodeRef         = this;
                }
            }

            //remove inhexistants field dictionary entries (for renamed variables):
            var toRemoveKeys = new List <string>();

            foreach (var kp in anchorFieldDictionary)
            {
                if (!actualFields.Contains(kp.Key))
                {
                    toRemoveKeys.Add(kp.Key);
                }
            }

            foreach (var toRemoveKey in toRemoveKeys)
            {
                anchorFieldDictionary.Remove(toRemoveKey);
            }
        }