public PropertieDefinition(Type type, FieldInfo fieldInfo)
 {
     Name            = fieldInfo.Name;
     Type            = fieldInfo.FieldType;
     _getter         = ObjectAccessors.CreatePropertyGetter(type, fieldInfo.Name);
     _propertySetter = ObjectAccessors.CreatePropertySetter(type, fieldInfo.Name);
 }
 public PropertieDefinition(Type type, PropertyInfo propertyInfo)
 {
     Name            = propertyInfo.Name;
     Type            = propertyInfo.PropertyType;
     _getter         = ObjectAccessors.CreatePropertyGetter(type, propertyInfo.Name);
     _propertySetter = ObjectAccessors.CreatePropertySetter(type, propertyInfo.Name);
 }
Пример #3
0
 public PropertieDefinition(Type type, FieldInfo fieldInfo, string name)
 {
     Name            = string.IsNullOrEmpty(name) ? fieldInfo.Name : name;
     TypeDefinition  = TypeDefinitionCache.GetDefinition(fieldInfo.FieldType);
     IsWritable      = !fieldInfo.IsInitOnly;
     _getter         = ObjectAccessors.CreatePropertyGetter(type, fieldInfo.Name);
     _propertySetter = CreateSetter(type, fieldInfo.Name);
 }
Пример #4
0
 public PropertieDefinition(Type type, PropertyInfo propertyInfo, string name)
 {
     Name            = string.IsNullOrEmpty(name) ? propertyInfo.Name : name;
     TypeDefinition  = TypeDefinitionCache.GetDefinition(propertyInfo.PropertyType);
     IsWritable      = propertyInfo.CanWrite;
     _getter         = ObjectAccessors.CreatePropertyGetter(type, propertyInfo.Name);
     _propertySetter = CreateSetter(type, propertyInfo.Name);
 }
Пример #5
0
        ObjectAccessors.PropertySetter CreateSetter(Type type, string name)
        {
            if (IsWritable)
            {
                return(ObjectAccessors.CreatePropertySetter(type, name));
            }

            var result = TypeDefinition.MethodAddToCollection != null ? new ObjectAccessors.PropertySetter(TypeDefinition.MethodAddToCollection) : null;

            return(result);
        }
Пример #6
0
        public TypeDefinition(Type type)
        {
            Type = type;
            Name = type.Name;

            var typeInfo = type.GetTypeInfo();

            if (typeInfo.IsGenericType)
            {
                Type[] types = type.GetGenericArguments();

                if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    Type = type.GetGenericArguments()[0];
                }

                Name = Name.Replace("`" + types.Length, "Of" + string.Join("", types.Select(p => p.Name)));
            }

            FullName = type.FullName;

            GetPrimitiveInfo(Type);

            IsArray = typeInfo.IsArray;

            if (!IsPrimitive && typeof(IEnumerable).IsAssignableFrom(type))
            {
                IsEnumerable = true;
            }

            if (IsEnumerable)
            {
                if (typeof(IDictionary).IsAssignableFrom(type))
                {
                    IsDictionary = true;
                }
                var elementType = type.GetElementType();
                if (elementType != null)
                {
                    Name = "ArrayOf" + elementType.Name;
                }

                if (typeInfo.IsGenericType)
                {
                    GenericArguments = type.GetGenericArguments();
                    if (elementType == null)
                    {
                        Name = "ArrayOf" + string.Join("", GenericArguments.Select(p => p.Name));
                    }
                    if (IsDictionary)
                    {
                        MethodAddToDictionary = ObjectAccessors.CreateMethodAddToDictionary(type);
                    }
                    else
                    {
                        MethodAddToCollection = ObjectAccessors.CreateMethodAddCollection(type);
                    }
                }
            }


            IsObjectToSerialize = // !typeInfo.IsPrimitive && !typeInfo.IsValueType &&
                                  !IsPrimitive &&
                                  !typeInfo.IsEnum && type != typeof(string) &&
                                  //not generic or generic but not List<> and Set<>
                                  (!typeInfo.IsGenericType ||
                                   (typeInfo.IsGenericType && !typeof(IEnumerable).IsAssignableFrom(type)));
            if (IsObjectToSerialize)
            {
                Properties = GetPropertieToSerialze(type);
            }

            ObjectActivator = ObjectAccessors.CreateObjectActivator(type, IsPrimitive);
        }
Пример #7
0
        public TypeDefinition(Type type)
        {
            Type = type;
            Name = type.Name;

            var typeInfo      = type.GetTypeInfo();
            var isGenericType = typeInfo.IsGenericType;

            if (isGenericType)
            {
                var types = type.GetGenericArguments();

                if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    Type = type.GetGenericArguments()[0];
                }

                Name = Name.Replace("`" + types.Length, "Of" + string.Join("", types.Select(p => p.Name)));
            }

            FullName = type.FullName;

            GetPrimitiveInfo(Type);

            IsArray = typeInfo.IsArray;

            IsEnumerable = !IsPrimitive && typeof(IEnumerable).IsAssignableFrom(type);

            if (IsEnumerable)
            {
                IsDictionary = typeof(IDictionary).IsAssignableFrom(type);

                var elementType = ElementTypeLocator.Default.Locate(type);
                if (isGenericType)
                {
                    GenericArguments = type.GetGenericArguments();
                }
                else if (elementType != null)
                {
                    GenericArguments = new[] { elementType };
                }

                Name = IsArray || isGenericType
                                        ? $"ArrayOf{string.Join(string.Empty, GenericArguments.Select(p => p.Name))}"
                                        : type.Name;

                if (IsDictionary)
                {
                    MethodAddToDictionary = ObjectAccessors.CreateMethodAddToDictionary(type);
                }
                else if (elementType != null && !IsArray)
                {
                    MethodAddToCollection = ObjectAccessors.CreateMethodAddCollection(type, elementType);
                }
            }

            var attribute = typeInfo.GetCustomAttribute <XmlRootAttribute>();

            if (attribute != null)
            {
                Name = attribute.ElementName;
            }

            IsObjectToSerialize =             // !typeInfo.IsPrimitive && !typeInfo.IsValueType &&
                                  !IsPrimitive &&
                                  !typeInfo.IsEnum && type != typeof(string) &&
                                  //not generic or generic but not List<> and Set<>
                                  (!isGenericType || !IsEnumerable);
            _properties = new Lazy <IEnumerable <PropertieDefinition> >(GetPropertieToSerialze);

            ObjectActivator = ObjectAccessors.CreateObjectActivator(type, IsPrimitive);
        }