Esempio n. 1
0
        private ObjectSerializationInfo GetObjectInfo(Type type)
        {
            var isClass = !type.IsValueType;
            var members = new List <MemberSerializationInfo>();
            // Only KeyAttribute members
            var searchFirst  = true;
            var hiddenIntKey = 0;

            foreach (PropertyInfo item in type.GetProperties())
            {
                var member = new MemberSerializationInfo
                {
                    IsReadable    = (item.GetMethod != null) && item.GetMethod.IsPublic,
                    IsWritable    = (item.SetMethod != null) && item.SetMethod.IsPublic,
                    IsProperty    = true,
                    IsField       = false,
                    Name          = item.Name,
                    Type          = item.PropertyType.FullName,
                    ShortTypeName = item.PropertyType.Name,
                    Attributes    = NeutralAttributeData.FromCustomAttributeArray(item.GetCustomAttributes())
                };
                if (!member.IsReadable && !member.IsWritable)
                {
                    continue;
                }

                if (searchFirst)
                {
                    searchFirst = false;
                }

                members.Add(member);
                this.CollectCore(item.PropertyType); // recursive collect
            }

            foreach (FieldInfo item in type.GetFields())
            {
                var member = new MemberSerializationInfo
                {
                    IsReadable    = item.IsPublic && !item.IsStatic,
                    IsWritable    = item.IsPublic && !item.IsInitOnly && !item.IsStatic,
                    IsProperty    = true,
                    IsField       = false,
                    Name          = item.Name,
                    Type          = item.FieldType.FullName,
                    ShortTypeName = item.FieldType.Name,
                };
                if (!member.IsReadable && !member.IsWritable)
                {
                    continue;
                }

                if (searchFirst)
                {
                    searchFirst = false;
                }

                members.Add(member);
                this.CollectCore(item.FieldType); // recursive collect
            }

            var info = new ObjectSerializationInfo
            {
                IsClass = isClass,
                ConstructorParameters = null,
                Members    = members.ToArray(),
                Name       = GetMinimallyQualifiedClassName(type),
                FullName   = type.FullName,
                Namespace  = type.Namespace,
                Attributes = NeutralAttributeData.FromCustomAttributeArray(type.GetCustomAttributes())
            };

            return(info);
        }
        private ObjectSerializationInfo GetObjectInfo(INamedTypeSymbol type)
        {
            var isClass = !type.IsValueType;
            var members = new List <MemberSerializationInfo>();
            // Only KeyAttribute members
            var searchFirst  = true;
            var hiddenIntKey = 0;

            foreach (IPropertySymbol item in RoslynExtensions.GetAllMembers(type).OfType <IPropertySymbol>())
            {
                if (item.IsIndexer)
                {
                    continue; // .tt files don't generate good code for this yet: https://github.com/neuecc/MessagePack-CSharp/issues/390
                }

                var member = new MemberSerializationInfo
                {
                    IsReadable    = (item.GetMethod != null) && item.GetMethod.DeclaredAccessibility == Accessibility.Public && !item.IsStatic,
                    IsWritable    = (item.SetMethod != null) && item.SetMethod.DeclaredAccessibility == Accessibility.Public && !item.IsStatic,
                    IsProperty    = true,
                    IsField       = false,
                    Name          = item.Name,
                    Type          = item.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
                    ShortTypeName = item.Type.ToDisplayString(BinaryWriteFormat),
                    Attributes    = NeutralAttributeData.FromAttributeDataArray(item.Type.GetAttributes())
                };
                if (!member.IsReadable && !member.IsWritable)
                {
                    continue;
                }

                if (searchFirst)
                {
                    searchFirst = false;
                }

                members.Add(member);
                this.CollectCore(item.Type); // recursive collect
            }

            foreach (IFieldSymbol item in RoslynExtensions.GetAllMembers(type).OfType <IFieldSymbol>())
            {
                if (item.IsImplicitlyDeclared)
                {
                    continue;
                }

                var member = new MemberSerializationInfo
                {
                    IsReadable    = item.DeclaredAccessibility == Accessibility.Public && !item.IsStatic,
                    IsWritable    = item.DeclaredAccessibility == Accessibility.Public && !item.IsReadOnly && !item.IsStatic,
                    IsProperty    = true,
                    IsField       = false,
                    Name          = item.Name,
                    Type          = item.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
                    ShortTypeName = item.Type.ToDisplayString(BinaryWriteFormat),
                };
                if (!member.IsReadable && !member.IsWritable)
                {
                    continue;
                }

                if (searchFirst)
                {
                    searchFirst = false;
                }

                members.Add(member);
                this.CollectCore(item.Type); // recursive collect
            }

            var info = new ObjectSerializationInfo
            {
                IsClass = isClass,
                ConstructorParameters = null,
                Members    = members.ToArray(),
                Name       = GetMinimallyQualifiedClassName(type),
                FullName   = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
                Namespace  = type.ContainingNamespace.IsGlobalNamespace ? null : type.ContainingNamespace.ToDisplayString(),
                Attributes = NeutralAttributeData.FromAttributeDataArray(type.GetAttributes())
            };

            return(info);
        }