示例#1
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);
        }
示例#2
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);
        }