private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            Attribute[] customAttributes = ReflectionUtils.GetAttributes(type, null, true);

            foreach (Attribute attribute in customAttributes)
            {
                Type attributeType = attribute.GetType();

                // only test on attribute type name
                // attribute assembly could change because of type forwarding, etc
                if (string.Equals(attributeType.FullName, "System.ComponentModel.DataAnnotations.MetadataTypeAttribute", StringComparison.Ordinal))
                {
                    const string metadataClassTypeName = "MetadataClassType";

                    if (_metadataTypeAttributeReflectionObject == null)
                    {
                        _metadataTypeAttributeReflectionObject = ReflectionObject.Create(attributeType, metadataClassTypeName);
                    }

                    return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
                }
            }

            return(null);
        }
        public static bool GetStringLength(JsonProperty property, out int minimumLength, out int maximumLength)
        {
            if (property != null)
            {
                Attribute attribute = GetAttributeByName(property, StringLengthAttributeName, out Type matchingType);
                if (attribute != null)
                {
                    if (_stringLengthReflectionObject == null)
                    {
                        _stringLengthReflectionObject = ReflectionObject.Create(
                            matchingType,
#if !NET35
                            "MinimumLength",
#endif
                            "MaximumLength");
                    }

#if !NET35
                    minimumLength = (int)_stringLengthReflectionObject.GetValue(attribute, "MinimumLength");
#else
                    minimumLength = 0;
#endif
                    maximumLength = (int)_stringLengthReflectionObject.GetValue(attribute, "MaximumLength");
                    return(true);
                }
            }

            minimumLength = 0;
            maximumLength = 0;
            return(false);
        }
        public static string GetFormat(JsonProperty property)
        {
            if (property != null)
            {
                if (GetAttributeByName(property, UrlAttributeName, out _) != null)
                {
                    return(Constants.Formats.Uri);
                }

                if (GetAttributeByName(property, PhoneAttributeName, out _) != null)
                {
                    return(Constants.Formats.Phone);
                }

                if (GetAttributeByName(property, EmailAddressAttributeName, out _) != null)
                {
                    return(Constants.Formats.Email);
                }

                Attribute dataTypeAttribute = GetAttributeByName(property, DataTypeAttributeName, out Type matchingType);
                if (dataTypeAttribute != null)
                {
                    if (_dataTypeReflectionObject == null)
                    {
                        _dataTypeReflectionObject = ReflectionObject.Create(matchingType, "DataType");
                    }
                    string s = _dataTypeReflectionObject.GetValue(dataTypeAttribute, "DataType").ToString();
                    switch (s)
                    {
                    case "Url":
                        return(Constants.Formats.Uri);

                    case "Date":
                        return(Constants.Formats.Date);

                    case "Time":
                        return(Constants.Formats.Time);

                    case "DateTime":
                        return(Constants.Formats.DateTime);

                    case "EmailAddress":
                        return(Constants.Formats.Email);

                    case "PhoneNumber":
                        return(Constants.Formats.Phone);
                    }
                }
            }

            return(null);
        }
        public static int?GetMinLength(JsonProperty property)
        {
            if (property != null)
            {
                Attribute minLengthAttribute = GetAttributeByName(property, MinLengthAttributeName);
                if (minLengthAttribute != null)
                {
                    ReflectionObject o = ReflectionObject.Create(minLengthAttribute.GetType(), "Length");
                    return((int)o.GetValue(minLengthAttribute, "Length"));
                }
            }

            return(null);
        }
        public static Type GetEnumDataType(JsonProperty property)
        {
            if (property != null)
            {
                Attribute attribute = GetAttributeByName(property, EnumDataTypeAttributeName);
                if (attribute != null)
                {
                    ReflectionObject o = ReflectionObject.Create(attribute.GetType(), "EnumType");
                    return((Type)o.GetValue(attribute, "EnumType"));
                }
            }

            return(null);
        }
        public static string GetPattern(JsonProperty property)
        {
            if (property != null)
            {
                Attribute regexAttribute = GetAttributeByName(property, RegularExpressionAttributeName);
                if (regexAttribute != null)
                {
                    ReflectionObject o = ReflectionObject.Create(regexAttribute.GetType(), "Pattern");
                    return((string)o.GetValue(regexAttribute, "Pattern"));
                }
            }

            return(null);
        }
        public static string GetPattern(JsonProperty property)
        {
            if (property != null)
            {
                Attribute regexAttribute = GetAttributeByName(property, RegularExpressionAttributeName, out Type matchingType);
                if (regexAttribute != null)
                {
                    if (_regexReflectionObject == null)
                    {
                        _regexReflectionObject = ReflectionObject.Create(matchingType, "Pattern");
                    }
                    return((string)_regexReflectionObject.GetValue(regexAttribute, "Pattern"));
                }
            }

            return(null);
        }
        public static int?GetMaxLength(JsonProperty property)
        {
            if (property != null)
            {
                Attribute maxLengthAttribute = GetAttributeByName(property, MaxLengthAttributeName, out Type matchingType);
                if (maxLengthAttribute != null)
                {
                    if (_maxLengthReflectionObject == null)
                    {
                        _maxLengthReflectionObject = ReflectionObject.Create(matchingType, "Length");
                    }
                    return((int)_maxLengthReflectionObject.GetValue(maxLengthAttribute, "Length"));
                }
            }

            return(null);
        }
        public static Type GetEnumDataType(JsonProperty property)
        {
            if (property != null)
            {
                Attribute attribute = GetAttributeByName(property, EnumDataTypeAttributeName, out Type matchingType);
                if (attribute != null)
                {
                    if (_enumTypeReflectionObject == null)
                    {
                        _enumTypeReflectionObject = ReflectionObject.Create(matchingType, "EnumType");
                    }
                    return((Type)_enumTypeReflectionObject.GetValue(attribute, "EnumType"));
                }
            }

            return(null);
        }
Exemple #10
0
        public static bool GetDisplayName(Type type, JsonProperty memberProperty, out string displayName)
        {
            Attribute displayNameAttribute = GetAttributeByNameFromTypeOrProperty(type, memberProperty, DisplayNameAttributeName);

            if (displayNameAttribute != null)
            {
                if (_displayNameReflectionObject == null)
                {
                    _displayNameReflectionObject = ReflectionObject.Create(displayNameAttribute.GetType(), "DisplayName");
                }
                displayName = (string)_displayNameReflectionObject.GetValue(displayNameAttribute, "DisplayName");
                return(true);
            }

            displayName = null;
            return(false);
        }
Exemple #11
0
        public static bool GetDescription(Type type, JsonProperty memberProperty, out string description)
        {
            Attribute descriptionAttribute = GetAttributeByNameFromTypeOrProperty(type, memberProperty, DescriptionAttributeName);

            if (descriptionAttribute != null)
            {
                if (_descriptionReflectionObject == null)
                {
                    _descriptionReflectionObject = ReflectionObject.Create(descriptionAttribute.GetType(), "Description");
                }
                description = (string)_descriptionReflectionObject.GetValue(descriptionAttribute, "Description");
                return(true);
            }

            description = null;
            return(false);
        }
        public static bool GetRange(JsonProperty property, out double minimum, out double maximum)
        {
            if (property != null)
            {
                Attribute attribute = GetAttributeByName(property, RangeAttributeName);
                if (attribute != null)
                {
                    ReflectionObject o = ReflectionObject.Create(attribute.GetType(), "Minimum", "Maximum");
                    minimum = Convert.ToDouble(o.GetValue(attribute, "Minimum"), CultureInfo.InvariantCulture);
                    maximum = Convert.ToDouble(o.GetValue(attribute, "Maximum"), CultureInfo.InvariantCulture);
                    return(true);
                }
            }

            minimum = 0;
            maximum = 0;
            return(false);
        }
        public static bool GetStringLength(JsonProperty property, out int minimumLength, out int maximumLength)
        {
            if (property != null)
            {
                Attribute attribute = GetAttributeByName(property, StringLengthAttributeName);
                if (attribute != null)
                {
                    ReflectionObject o = ReflectionObject.Create(attribute.GetType(), "MinimumLength", "MaximumLength");
                    minimumLength = (int)o.GetValue(attribute, "MinimumLength");
                    maximumLength = (int)o.GetValue(attribute, "MaximumLength");
                    return(true);
                }
            }

            minimumLength = 0;
            maximumLength = 0;
            return(false);
        }
        private static bool GetDisplay(Type type, JsonProperty memberProperty, out string name, out string description)
        {
            Attribute displayAttribute = GetAttributeByNameFromTypeOrProperty(type, memberProperty, DisplayAttributeName, out Type matchingType);

            if (displayAttribute != null)
            {
                if (_displayReflectionObject == null)
                {
                    _displayReflectionObject = ReflectionObject.Create(matchingType, "GetName", "GetDescription");
                }
                name        = (string)_displayReflectionObject.GetValue(displayAttribute, "GetName");
                description = (string)_displayReflectionObject.GetValue(displayAttribute, "GetDescription");
                return(true);
            }

            name        = null;
            description = null;
            return(false);
        }
        public static bool GetRange(JsonProperty property, out double minimum, out double maximum)
        {
            if (property != null)
            {
                Attribute rangeAttribute = GetAttributeByName(property, RangeAttributeName, out Type matchingType);
                if (rangeAttribute != null)
                {
                    if (_rangeReflectionObject == null)
                    {
                        _rangeReflectionObject = ReflectionObject.Create(matchingType, "Minimum", "Maximum");
                    }
                    minimum = Convert.ToDouble(_rangeReflectionObject.GetValue(rangeAttribute, "Minimum"), CultureInfo.InvariantCulture);
                    maximum = Convert.ToDouble(_rangeReflectionObject.GetValue(rangeAttribute, "Maximum"), CultureInfo.InvariantCulture);
                    return(true);
                }
            }

            minimum = 0;
            maximum = 0;
            return(false);
        }
        public static bool GetDisplayName(Type type, JsonProperty memberProperty, out string displayName)
        {
            if (GetDisplay(type, memberProperty, out displayName, out _) && !string.IsNullOrEmpty(displayName))
            {
                return(true);
            }

            Attribute displayNameAttribute = GetAttributeByNameFromTypeOrProperty(type, memberProperty, DisplayNameAttributeName, out Type matchingType);

            if (displayNameAttribute != null)
            {
                if (_displayNameReflectionObject == null)
                {
                    _displayNameReflectionObject = ReflectionObject.Create(matchingType, "DisplayName");
                }
                displayName = (string)_displayNameReflectionObject.GetValue(displayNameAttribute, "DisplayName");
                return(true);
            }

            displayName = null;
            return(false);
        }
        public static bool GetDescription(Type type, JsonProperty memberProperty, out string description)
        {
            if (GetDisplay(type, memberProperty, out _, out description) && !string.IsNullOrEmpty(description))
            {
                return(true);
            }

            Attribute descriptionAttribute = GetAttributeByNameFromTypeOrProperty(type, memberProperty, DescriptionAttributeName, out Type matchingType);

            if (descriptionAttribute != null)
            {
                if (_descriptionReflectionObject == null)
                {
                    _descriptionReflectionObject = ReflectionObject.Create(matchingType, "Description");
                }
                description = (string)_descriptionReflectionObject.GetValue(descriptionAttribute, "Description");
                return(true);
            }

            description = null;
            return(false);
        }
        public static ReflectionObject Create(Type t, MethodBase creator, params string[] memberNames)
        {
            ReflectionDelegateFactory delegateFactory = ReflectionDelegateFactory.SupportedInstance;

            ObjectConstructor <object> creatorConstructor = null;

            if (creator != null)
            {
                creatorConstructor = delegateFactory.CreateParameterizedConstructor(creator);
            }
            else
            {
                if (ReflectionUtils.HasDefaultConstructor(t, false))
                {
                    Func <object> ctor = delegateFactory.CreateDefaultConstructor <object>(t);

                    creatorConstructor = args => ctor();
                }
            }

            ReflectionObject d = new ReflectionObject(creatorConstructor);

            foreach (string memberName in memberNames)
            {
                MemberInfo[] members = t.GetMember(memberName, BindingFlags.Instance | BindingFlags.Public);
                if (members.Length != 1)
                {
                    throw new ArgumentException("Expected a single member with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, memberName));
                }

                MemberInfo member = members.Single();

                ReflectionMember reflectionMember = new ReflectionMember();

                switch (member.MemberType())
                {
                case MemberTypes.Field:
                case MemberTypes.Property:
                    if (ReflectionUtils.CanReadMemberValue(member, false))
                    {
                        reflectionMember.Getter = delegateFactory.CreateGet <object>(member);
                    }

                    if (ReflectionUtils.CanSetMemberValue(member, false, false))
                    {
                        reflectionMember.Setter = delegateFactory.CreateSet <object>(member);
                    }
                    break;

                case MemberTypes.Method:
                    MethodInfo method = (MethodInfo)member;
                    if (method.IsPublic)
                    {
                        ParameterInfo[] parameters = method.GetParameters();
                        if (parameters.Length == 0 && method.ReturnType != typeof(void))
                        {
                            MethodCall <object, object> call = delegateFactory.CreateMethodCall <object>(method);
                            reflectionMember.Getter = target => call(target);
                        }
                        else if (parameters.Length == 1 && method.ReturnType == typeof(void))
                        {
                            MethodCall <object, object> call = delegateFactory.CreateMethodCall <object>(method);
                            reflectionMember.Setter = (target, arg) => call(target, arg);
                        }
                    }
                    break;

                default:
                    throw new ArgumentException("Unexpected member type '{0}' for member '{1}'.".FormatWith(CultureInfo.InvariantCulture, member.MemberType(), member.Name));
                }

                if (ReflectionUtils.CanReadMemberValue(member, false))
                {
                    reflectionMember.Getter = delegateFactory.CreateGet <object>(member);
                }

                if (ReflectionUtils.CanSetMemberValue(member, false, false))
                {
                    reflectionMember.Setter = delegateFactory.CreateSet <object>(member);
                }

                reflectionMember.MemberType = ReflectionUtils.GetMemberUnderlyingType(member);

                d.Members[memberName] = reflectionMember;
            }

            return(d);
        }