예제 #1
0
 internal void SerializationTypeRegistryTests(
     SerializationTypeRegistry registry,
     ContractTypeInfo info
     )
 {
     GIVEN["a new registry"] = () => registry = new SerializationTypeRegistry();
     WHEN["getting info for discriminator"]     = () => info = (ContractTypeInfo)registry.GetInfo("TEST:Test.UpdateObjects");
     THEN["the info contains the correct type"] = () => info.Type.Should().Be <UpdateCertificationObjects>();
     WHEN["getting info for a type"]            = () => info = (ContractTypeInfo)registry.GetInfo(typeof(UpdateServices));
     THEN["the info contains expected infos"]   = () => {
         info.Name.Name.Should().Be("Test.UpdateServices");
         info.Name.ModuleCode.Should().Be("TEST");
     };
 }
예제 #2
0
        private SerializationTypeInfo?Process(Type type)
        {
            if (_infoByType.TryGetValue(type, out SerializationTypeInfo result))
            {
                return(result);
            }

            ContractAttribute?attribute = type.GetCustomAttribute <ContractAttribute>(inherit: false);

            if (attribute != null)
            {
                string?moduleCode = GetModuleCode(type.Assembly);

                if (moduleCode != null)
                {
                    string typeName      = attribute.Name ?? RemoveGenericBackticks(type.Name);
                    string?containerName = GetContractContainerName(type);

                    var name = containerName != null ?
                               new SerializationTypeName(moduleCode, containerName, typeName) :
                               new SerializationTypeName(moduleCode, typeName);

                    ContractTypeInfo contractInfo = new ContractTypeInfo(type, name);

                    // HACK: This seems kind of hacky... Maybe rethink...
                    if (!type.IsGenericTypeDefinition)
                    {
                        Expect.That(_infoByDiscriminator.TryAdd(contractInfo.Discriminator, contractInfo));
                    }

                    result = contractInfo;
                }
                else
                {
                    result = new SerializationTypeInfo(type);
                }

                Expect.That(_infoByType.TryAdd(result.Type, result));

                bool hasBaseType = !type.IsValueType &&
                                   type.BaseType != typeof(object) &&
                                   type.BaseType != null;

                IEnumerable <Type> baseType = hasBaseType ? new[] { type.BaseType ! } : Enumerable.Empty <Type>();