示例#1
0
        public void TestEnumTypeResolving()
        {
            var inspector = new ModelInspector();

            inspector.ImportEnum(typeof(SomeEnum));
            inspector.ImportEnum(typeof(ActResource.SomeOtherEnum));

            var result = inspector.FindEnumMappingByType(typeof(SomeEnum));

            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(SomeEnum), result.EnumType);

            result = inspector.FindEnumMappingByType(typeof(ActResource.SomeOtherEnum));
            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(ActResource.SomeOtherEnum), result.EnumType);
        }
示例#2
0
        public void TestAssemblyInspection()
        {
            var inspector = new ModelInspector();

            // Inspect the HL7.Fhir.Model assembly
            inspector.Import(typeof(Resource).Assembly);

            // Check for presence of some basic ingredients
            Assert.IsNotNull(inspector.FindClassMappingForResource("patient"));
            Assert.IsNotNull(inspector.FindClassMappingForFhirDataType("HumanName"));
            Assert.IsNotNull(inspector.FindClassMappingForFhirDataType("code"));
            Assert.IsNotNull(inspector.FindClassMappingForFhirDataType("boolean"));

            // Verify presence of nested enumerations
            Assert.IsNotNull(inspector.FindEnumMappingByType(typeof(Address.AddressUse)));

            // Should have skipped abstract classes
            Assert.IsNull(inspector.FindClassMappingForResource("ComplexElement"));
            Assert.IsNull(inspector.FindClassMappingForResource("Element"));
            Assert.IsNull(inspector.FindClassMappingForResource("Resource"));

            // The open generic Code<> should not be there
            var codeOfT = inspector.FindClassMappingByType(typeof(Code <>));

            Assert.IsNull(codeOfT);
        }
        internal void Serialize(object value, XmlSerializationHint hint)
        {
            if (value != null)
            {
                var nativeType = value.GetType();

                if (nativeType.IsEnum())
                {
                    var enumMapping = _inspector.FindEnumMappingByType(nativeType);

                    if (enumMapping != null)
                    {
                        value = enumMapping.GetLiteral((Enum)value);
                    }
                }
            }

            _writer.WritePrimitiveContents(value, hint);
        }
        internal object Deserialize(Type nativeType)
        {
            if (nativeType == null)
            {
                throw Error.ArgumentNull("nativeType");
            }

            object primitiveValue = _current.GetPrimitiveValue();

            if (nativeType.IsEnum() && primitiveValue.GetType() == typeof(string))
            {
                var enumMapping = _inspector.FindEnumMappingByType(nativeType);

                if (enumMapping != null)
                {
                    var enumLiteral = (string)primitiveValue;
                    if (enumMapping.ContainsLiteral(enumLiteral))
                    {
                        return(enumMapping.ParseLiteral((string)primitiveValue));
                    }
                    else
                    {
                        throw Error.Format("Literal {0} is not a valid value for enumeration {1}", _current, enumLiteral, enumMapping.Name);
                    }
                }
                else
                {
                    throw Error.Format("Cannot find an enumeration mapping for enum " + nativeType.Name, _current);
                }
            }

            try
            {
                return(PrimitiveTypeConverter.ConvertTo(primitiveValue, nativeType));
            }
            catch (NotSupportedException exc)
            {
                // thrown when an unsupported conversion was required
                throw Error.Format(exc.Message, _current);
            }
        }
        private object read(Type nativeType)
        {
            object primitiveValue = _current.GetPrimitiveValue();

            if (nativeType.IsEnum && primitiveValue.GetType() == typeof(string))
            {
                var enumMapping = _inspector.FindEnumMappingByType(nativeType);

                if (enumMapping != null)
                {
                    return(enumMapping.ParseLiteral((string)primitiveValue));
                }
            }

            try
            {
                return(PrimitiveTypeConverter.Convert(primitiveValue, nativeType));
            }
            catch (NotSupportedException exc)
            {
                // thrown when an unsupported conversion was required
                throw Error.Format(exc.Message, _current);
            }
        }