コード例 #1
0
        private void CheckIfTypeHasProperDataContractAttirbuteInHierarchy()
        {
            if (Type == null)
            {
                return;
            }

            var inspector = this;

            while (true)
            {
                var baseType = inspector.Type.BaseType;

                if (baseType == null || baseType == typeof(object) || baseType == typeof(ValueType) || baseType == typeof(Enum))
                {
                    return;
                }

                inspector = new TypeInspector(baseType);
                if (!inspector.IsSerializable)
                {
                    throw Exceptions.DataContractAttributeMissingFromHierarchy(Type, baseType);
                }
            }
        }
コード例 #2
0
        private static void CheckIfShapeshifterRootAttributeIsPresent(Type type)
        {
            var ti = new TypeInspector(type);

            if (ti.HasDataContractAttribute && !ti.HasShapeshifterRootAttribute)
            {
                throw Exceptions.ShapeshifterRootAttributeMissing(type);
            }
        }
コード例 #3
0
        private void InternalWalkType(Type type)
        {
            //TODO cache typeInspectors statically?
            var typeInspector = new TypeInspector(type);

            //skip natives and already visited types
            if (typeInspector.IsNativeType || _typesVisited.Contains(type))
            {
                return;
            }

            _typesVisited.Add(type);

            typeInspector.AcceptOnNonStaticMethods(_visitor);
            typeInspector.AcceptOnStaticMethods(_visitor);

            if (!typeInspector.IsSerializable)
            {
                return;
            }

            if (_packformatNamesUsed.Contains(typeInspector.PackformatName))
            {
                throw Exceptions.PackformatNameCollision(typeInspector.PackformatName, type);
            }

            _packformatNamesUsed.Add(typeInspector.PackformatName);

            typeInspector.AcceptOnType(_visitor);

            foreach (var candidate in typeInspector.SerializableMemberCandidates)
            {
                Type candidateType = candidate.Type;

                //recognize arrays and all IEnumerable<T> types we support (like List<T> and Collection<T>) and add the T to the list of known types
                if (candidateType.IsArray)
                {
                    InternalWalkType(candidateType.GetElementType());
                }
                else if (candidateType.IsGenericType)
                {
                    Type genericDefType = candidateType.GetGenericTypeDefinition();
                    if (genericDefType == typeof(List <>) || genericDefType == typeof(Collection <>) || genericDefType == typeof(IEnumerable <>))
                    {
                        InternalWalkType(candidateType.GetGenericArguments()[0]);
                    }
                    else if (genericDefType == typeof(Dictionary <,>))
                    {
                        InternalWalkType(candidateType.GetGenericArguments()[0]);
                        InternalWalkType(candidateType.GetGenericArguments()[1]);
                    }
                }
                InternalWalkType(candidateType);
            }

            if (typeInspector.HasKnownTypeAttribute)
            {
                foreach (var knownType in typeInspector.GetKnownTypes())
                {
                    WalkKnownType(knownType);
                }
            }
        }