Beispiel #1
0
        private static DataSetModel MakeDataSetModel(AnalyzeContext context, ObjectSerializationInfo info)
        {
            List <StadModel> listModels   = new List <StadModel>();
            List <StadModel> singleModels = new List <StadModel>();

            foreach (MemberSerializationInfo member in info.Members)
            {
                var memberObjectInfo = GetObjectInfo(context.CollectResult, member.Type);
                if (memberObjectInfo == null)
                {
                    throw new Exception($"Member not found, {member.Type}");
                }

                if (memberObjectInfo.Attributes.FirstOrDefault(a =>
                                                               a.Type.Contains(nameof(Stad.Annotation.IgnoreMemberAttribute))) != null)
                {
                    continue;
                }

                StadModel memberModel = MakeStadModel(context, memberObjectInfo);
                if (memberModel == null)
                {
                    throw new Exception($"Member create failed, {memberObjectInfo}");
                }

                if (memberModel.Type.Contains("StadKeyValueCollection") == true)
                {
                    listModels.Add(memberModel);
                }
                else
                {
                    singleModels.Add(memberModel);
                }
            }

            foreach (StadModel model in context.AllModels)
            {
                foreach (MemberDefinition memberDefinition in model.Members)
                {
                    var memberModel = context.AllModels.Find(m => m.Type == memberDefinition.Type);
                    memberDefinition.SetModel(memberModel);
                }
            }

            return(new DataSetModel(info.FullName, new ReadOnlyCollection <StadModel>(listModels), new ReadOnlyCollection <StadModel>(singleModels)));
        }
Beispiel #2
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);
        }
Beispiel #4
0
        private void CollectObject(Type type)
        {
            ObjectSerializationInfo info = GetObjectInfo(type);

            collectedObjectInfo.Add(info);
        }
        private void CollectObject(INamedTypeSymbol type)
        {
            ObjectSerializationInfo info = GetObjectInfo(type);

            collectedObjectInfo.Add(info);
        }