예제 #1
0
        public static ContentProperty GetContentPropertyByPropertyInfo(this PropertyInfo p, object input)
        {
            var result = new ContentProperty();

            if (p.GetCustomAttribute <BasePropertyAttribute>() != null)
            {
                result.BaseProperty = true;
            }
            if (p.GetCustomAttribute <IgnoreEditAttribute>() != null || p.GetCustomAttribute <HideEditAttribute>() != null)
            {
                result.IgnoreProperty = true;
            }
            if (p.GetCustomAttribute <CustomPropertyAttribute>() != null)
            {
                result.CustomProperty = true;
            }
            result.Key = p.Name;
            var inputType = p.GetObjectCustomAttribute <InputTypeAttribute>();

            result.SetSameType <IInputCommon>(inputType);
            var cValue = p.GetValue(input).MyTryConvert(typeof(string));

            if (cValue != null)
            {
                result.Value = (string)cValue;
            }

            Type relatedType    = null;
            var  inputAttribute = inputType;

            if (inputAttribute != null)
            {
                relatedType = inputAttribute.RelatedType;
                if (inputAttribute.RangeMaxSelf)
                {
                    var max = input.MyTryConvert <int>();
                    result.RangeMax = max;
                }
            }
            var propertyType     = p.GetType().GetRealType();
            var displayAttribute = p.GetCustomAttributes().Where(b => b.GetType().GetRealType().Name == "DisplayAttribute").FirstOrDefault();
            var property         = displayAttribute != null?displayAttribute.GetType().GetRealType().GetProperties().Where(b => b.Name == "Name").FirstOrDefault() : null;

            if (displayAttribute != null && property != null && !String.IsNullOrEmpty((string)property.GetValue(displayAttribute)))
            {
                result.Title = (string)property.GetValue(displayAttribute);
            }
            else
            {
                result.Title = p.Name.SpacesFromCamel();
            }

            var type         = p.PropertyType;
            var datetimeType = typeof(DateTime);

            if (result.EditorType == EnumInputType.DropDwon)
            {
                var stringValue = input != null?p.GetValue(input).MyTryConvert <string>() : "";

                var stringValueList = stringValue.StringValueToList();
                if (result.MultiSelect || stringValueList.Count() > 1)
                {
                    result.MultiValue = stringValueList;
                    result.Value      = "";
                }
                p.SetDropDownSelect(
                    (List <DropDownViewModel>)result.SelectItems, relatedType, result.Value, result.MultiValue);
            }
            return(result);
            //return new ContentProperty()
            //{
            //  Key = p.Name,
            //  Value = postValue,
            //  EditorType = editorType,
            //  ValueType = propertyType.FullName,
            //  Title = displayTitle,
            //  MultiSelect = multiSelect,
            //  SelectItems = editorType == EnumInputType.DropDwon ? selector : Enumerable.Empty<DropDownViewModel>(),
            //  MultiValue = multiSelect ? postMultiValue : Enumerable.Empty<string>(),
            //};
        }
예제 #2
0
        public static ContentProperty GetContentPropertyByPropertyInfo(this PropertyInfo p, object input, int lang = 0, long modelId = 0)
        {
            var result = new ContentProperty();

            result.Lang    = lang;
            result.ModelId = modelId;
            if (p.GetCustomAttribute <BasePropertyAttribute>() != null)
            {
                result.BaseProperty = true;
            }
            if (p.GetCustomAttribute <IgnoreEditAttribute>() != null || p.GetCustomAttribute <HideEditAttribute>() != null)
            {
                result.IgnoreProperty = true;
            }
            if (p.GetCustomAttribute <CustomPropertyAttribute>() != null)
            {
                result.CustomProperty = true;
            }
            result.Key = p.Name;
            var inputType = p.GetObjectCustomAttribute <InputTypeAttribute>();

            result.SetSameType <IInputCommon>(inputType);
            var cValue = p.GetValue(input).MyTryConvert(typeof(string));

            if (cValue != null)
            {
                result.Value = (string)cValue;
            }

            Type relatedType    = null;
            var  inputAttribute = inputType;

            if (inputAttribute != null)
            {
                relatedType = inputAttribute.RelatedType;
                if (inputAttribute.RangeMaxSelf)
                {
                    var max = input.MyTryConvert <int>();
                    result.RangeMax = max;
                }
            }
            result.RelatedType = relatedType;
            var propertyType     = p.GetType().GetRealType();
            var displayAttribute = p.GetCustomAttributes().Where(b => b.GetType().GetRealType().Name == "DisplayAttribute").FirstOrDefault();
            var property         = displayAttribute != null?displayAttribute.GetType().GetRealType().GetProperties().Where(b => b.Name == "Name").FirstOrDefault() : null;

            if (displayAttribute != null && property != null && !String.IsNullOrEmpty((string)property.GetValue(displayAttribute)))
            {
                result.Title = (string)property.GetValue(displayAttribute);
            }
            else
            {
                result.Title = p.Name.SpacesFromCamel();
            }

            var type         = p.PropertyType;
            var datetimeType = typeof(DateTime);

            switch (result.EditorType)
            {
            case EnumInputType.DropDwon:
                var stringValue = input != null?p.GetValue(input).MyTryConvert <string>() : "";

                var stringValueList = stringValue.StringValueToList();
                if (result.MultiSelect || stringValueList.Count() > 1)
                {
                    result.MultiValue = stringValueList;
                    result.Value      = "";
                }
                p.SetDropDownSelect(
                    (List <DropDownViewModel>)result.SelectItems, relatedType, result.Value, result.MultiValue);
                break;

            case EnumInputType.SharedLink:
                //result.SharedLinks = GetSharedLinks(inputType.RelatedType, result.Lang, inputType.IsSingleRecord, inputType.IsLinkedRecord, result.ModelId);
                result.SharedLinks = Enumerable.Empty <ISharedLink>();
                break;

            default:
                break;
            }

            return(result);
        }