Пример #1
0
        /// <summary>
        /// CCtor
        /// </summary>
        static DataContractResolverStrategy()
        {
            string[] assemblyName = typeof(Object).Assembly.FullName.Split(',');
            assemblyName[0] = DataContractAssemblyName;

            Assembly assembly = Assembly.Load(String.Join(",", assemblyName));

            DataContractType     = assembly.GetType(DataContractTypeName);
            DataMemberType       = assembly.GetType(DataMemberTypeName);
            IgnoreDataMemberType = assembly.GetType(IgnoreDataMemberTypeName);

            if (DataContractType != null)
            {
                PropertyInfo property = DataContractType.GetProperty("Name", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                DataContractNameGetter = DynamicMethodGenerator.GetPropertyGetter(property);
                property = DataContractType.GetProperty("Namespace", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                DataContractNamespaceGetter = DynamicMethodGenerator.GetPropertyGetter(property);
            }

            if (DataContractResolverStrategy.DataMemberType != null)
            {
                PropertyInfo property = DataMemberType.GetProperty("Name", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                DataMemberNameGetter = DynamicMethodGenerator.GetPropertyGetter(property);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets a delegate which determines if the property or field should not be serialized based upon its value.
        /// </summary>
        /// <param name="member"></param>
        /// <returns>if has a value equivalent to the DefaultValueAttribute</returns>
        /// <remarks>
        /// This is useful for excluding serialization of default values.
        /// </remarks>
        public override ValueIgnoredDelegate GetValueIgnoredCallback(MemberInfo member)
        {
            Type objType = member.ReflectedType ?? member.DeclaringType;
            JsonSpecifiedPropertyAttribute specifiedPropertyAttr = TypeCoercionUtility.GetAttribute <JsonSpecifiedPropertyAttribute>(member);

            // look up specified property to see if exists
            GetterDelegate specifiedPropertyGetter = null;

            if (specifiedPropertyAttr != null && !String.IsNullOrEmpty(specifiedPropertyAttr.SpecifiedProperty))
            {
                PropertyInfo specProp = objType.GetProperty(specifiedPropertyAttr.SpecifiedProperty, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

                // ensure is correct return type
                if (specProp != null && specProp.PropertyType == typeof(bool))
                {
                    specifiedPropertyGetter = DynamicMethodGenerator.GetPropertyGetter(specProp);
                }
            }

            DefaultValueAttribute defaultAttr = TypeCoercionUtility.GetAttribute <DefaultValueAttribute>(member);

            if (defaultAttr == null)
            {
                if (specifiedPropertyGetter == null)
                {
                    // no need to even create a delegate
                    return(null);
                }

                // create a delegate which simply calls the specified property
                return(delegate(object target, object value)
                {
                    return Object.Equals(false, specifiedPropertyGetter(target));
                });
            }

            // extract default value since cannot change (is constant in attribute)
            object defaultValue = defaultAttr.Value;

            if (specifiedPropertyGetter == null)
            {
                // create a specific delegate which only has to compare the default value to the current value
                return(delegate(object target, object value)
                {
                    return Object.Equals(defaultValue, value);
                });
            }

            // create a combined delegate which checks both states
            return(delegate(object target, object value)
            {
                return
                Object.Equals(defaultValue, value) ||
                Object.Equals(false, specifiedPropertyGetter(target));
            });
        }
Пример #3
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="fieldInfo"></param>
        /// <param name="dataName"></param>
        /// <param name="isIgnored"></param>
        public MemberMap(FieldInfo fieldInfo, DataName dataName, ValueIgnoredDelegate isIgnored)
        {
            if (fieldInfo == null)
            {
                throw new ArgumentNullException("fieldInfo");
            }

            this.DataName   = dataName;
            this.MemberInfo = fieldInfo;
            this.Name       = fieldInfo.Name;
            this.Type       = fieldInfo.FieldType;
            this.Getter     = DynamicMethodGenerator.GetFieldGetter(fieldInfo);
            this.Setter     = DynamicMethodGenerator.GetFieldSetter(fieldInfo);
            this.IsIgnored  = isIgnored;
        }
Пример #4
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <param name="dataName"></param>
        /// <param name="isIgnored"></param>
        public MemberMap(PropertyInfo propertyInfo, DataName dataName, ValueIgnoredDelegate isIgnored)
        {
            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }

            this.DataName   = dataName;
            this.MemberInfo = propertyInfo;
            this.Name       = propertyInfo.Name;
            this.Type       = propertyInfo.PropertyType;
            this.Getter     = DynamicMethodGenerator.GetPropertyGetter(propertyInfo);
            this.Setter     = DynamicMethodGenerator.GetPropertySetter(propertyInfo);
            this.IsIgnored  = isIgnored;
        }
Пример #5
0
        /// <summary>
        /// Gets a delegate which determines if the property or field should not be serialized based upon its value.
        /// </summary>
        /// <param name="member"></param>
        /// <returns>if has a value equivalent to the DefaultValueAttribute or has a property named XXXSpecified which determines visibility</returns>
        /// <remarks>
        /// This is useful when default values need not be serialized.
        /// Under these situations XmlSerializer ignores properties based upon value:
        /// - DefaultValue: http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx
        /// - Specified Properies: http://msdn.microsoft.com/en-us/library/bb402199.aspx
        /// - ShouldSerialize Methods: http://msdn.microsoft.com/en-us/library/53b8022e.aspx
        /// </remarks>
        public override ValueIgnoredDelegate GetValueIgnoredCallback(MemberInfo member)
        {
            Type objType = member.ReflectedType ?? member.DeclaringType;

            // look up specified property to see if exists
            GetterDelegate specifiedPropertyGetter = null;
            PropertyInfo   specProp = objType.GetProperty(member.Name + SpecifiedSuffix, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            // ensure is correct return type
            if (specProp != null && specProp.PropertyType == typeof(bool))
            {
                specifiedPropertyGetter = DynamicMethodGenerator.GetPropertyGetter(specProp);
            }

            // look up specified property to see if exists
            ProxyDelegate shouldSerializeProxy = null;
            MethodInfo    shouldSerialize      = objType.GetMethod(ShouldSerializePrefix + member.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            // ensure is correct return type
            if (shouldSerialize != null && shouldSerialize.ReturnType == typeof(bool) && shouldSerialize.GetParameters().Length == 0)
            {
                shouldSerializeProxy = DynamicMethodGenerator.GetMethodProxy(shouldSerialize);
            }

            // to be most efficient must create a different delegate for each of 8 combinations so not performing extra work

            DefaultValueAttribute defaultAttr = TypeCoercionUtility.GetAttribute <DefaultValueAttribute>(member);

            if (defaultAttr == null)
            {
                if (specifiedPropertyGetter == null)
                {
                    if (shouldSerializeProxy == null)
                    {
                        // situation 1: only need to check if equal to null
                        return(delegate(object target, object value)
                        {
                            return (value == null);
                        });
                    }

                    // situation 2: create a delegate which simply calls the should serialize method
                    return(delegate(object target, object value)
                    {
                        return
                        (value == null) ||
                        Object.Equals(false, shouldSerializeProxy(target));
                    });
                }

                if (shouldSerializeProxy == null)
                {
                    // situation 3: create a delegate which simply calls the specified property
                    return(delegate(object target, object value)
                    {
                        return
                        (value == null) ||
                        Object.Equals(false, specifiedPropertyGetter(target));
                    });
                }

                // situation 4: create a delegate which calls both the specified property and the should serialize method
                return(delegate(object target, object value)
                {
                    return
                    (value == null) ||
                    Object.Equals(false, shouldSerializeProxy(target)) ||
                    Object.Equals(false, specifiedPropertyGetter(target));
                });
            }

            // extract default value since cannot change (is constant in attribute)
            object defaultValue = defaultAttr.Value;

            if (specifiedPropertyGetter == null)
            {
                if (shouldSerializeProxy == null)
                {
                    // situation 5: create a specific delegate which only has to compare the default value to the current value
                    return(delegate(object target, object value)
                    {
                        return
                        (value == null) ||
                        Object.Equals(defaultValue, value);
                    });
                }

                // situation 6: create a specific delegate which both compares to default value and calls should serialize method
                return(delegate(object target, object value)
                {
                    return
                    (value == null) ||
                    Object.Equals(defaultValue, value) ||
                    Object.Equals(false, shouldSerializeProxy(target));
                });
            }

            if (shouldSerializeProxy == null)
            {
                // situation 7: create a specific delegate which both compares to default value and checks specified property
                return(delegate(object target, object value)
                {
                    return
                    (value == null) ||
                    Object.Equals(defaultValue, value) ||
                    Object.Equals(false, specifiedPropertyGetter(target));
                });
            }

            // situation 8: create a combined delegate which checks all states
            return(delegate(object target, object value)
            {
                return
                (value == null) ||
                Object.Equals(defaultValue, value) ||
                Object.Equals(false, shouldSerializeProxy(target)) ||
                Object.Equals(false, specifiedPropertyGetter(target));
            });
        }
Пример #6
0
        /// <summary>
        /// Ctor
        /// </summary>
        public FactoryMap(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (FactoryMap.IsInvalidType(type))
            {
                throw new TypeLoadException(String.Format(
                                                FactoryMap.ErrorCannotInstantiate,
                                                type.FullName));
            }

            this.Ctor = DynamicMethodGenerator.GetTypeFactory(type);

            ConstructorInfo[] ctors;
            if (!typeof(IEnumerable).IsAssignableFrom(type))
            {
                if (this.Ctor != null)
                {
                    return;
                }

                ctors = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                if (ctors.Length == 1)
                {
                    ConstructorInfo ctor = ctors[0];
                    this.Ctor     = DynamicMethodGenerator.GetTypeFactory(ctor);
                    this.CtorArgs = ctor.GetParameters();
                }
                return;
            }

            // many ICollection types take an IEnumerable or ICollection
            // as a constructor argument.  look through constructors for
            // a compatible match.
            ctors = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);

            this.CollectionCtors = new Dictionary <Type, FactoryDelegate>(ctors.Length);

            foreach (ConstructorInfo ctor in ctors)
            {
                ParameterInfo[] paramList = ctor.GetParameters();
                if (paramList.Length != 1)
                {
                    continue;
                }

                Type argType = paramList[0].ParameterType;
                if ((argType == typeof(string)) ||
                    (
#if !NETCF
                        (argType.GetInterface(TypeCoercionUtility.TypeGenericIEnumerable, false) == null) &&
#endif
                        (typeof(IEnumerable).IsAssignableFrom(argType))))
                {
                    continue;
                }

                // save all constructors that can take an enumerable of objects
                this.CollectionCtors[argType] = DynamicMethodGenerator.GetTypeFactory(ctor);
            }

            if (this.Ctor == null)
            {
                // try to grab a private ctor if exists
                this.Ctor = DynamicMethodGenerator.GetTypeFactory(type);
            }

            // many collection types have an AddRange method
            // which adds a collection of items at once
            MethodInfo methodInfo = type.GetMethod("AddRange");
            if (methodInfo != null)
            {
                ParameterInfo[] parameters = methodInfo.GetParameters();
                if (parameters.Length == 1)
                {
                    this.AddRange     = DynamicMethodGenerator.GetMethodProxy(methodInfo);
                    this.AddRangeType = parameters[0].ParameterType;
                }
            }

            // many collection types have an Add method
            // which adds items one at a time
            Type collectionType = null;
#if !NETCF
            collectionType = type.GetInterface(TypeCoercionUtility.TypeGenericICollection, false);
#endif
            if (collectionType != null)
            {
                methodInfo = collectionType.GetMethod("Add");
            }
            else
            {
                methodInfo = type.GetMethod("Add");
            }

            if (methodInfo != null)
            {
                ParameterInfo[] parameters = methodInfo.GetParameters();
                if (parameters.Length == 1)
                {
                    this.Add     = DynamicMethodGenerator.GetMethodProxy(methodInfo);
                    this.AddType = parameters[0].ParameterType;
                }
            }
        }