예제 #1
0
        public static bool IsMappableType(Type type)
        {
            var hasAttribute = type.GetTypeInfo().IsDefined(typeof(FhirTypeAttribute), false);

            if (!hasAttribute)
            {
                return(false);
            }

            //if (type.GetTypeInfo().IsAbstract)
            //    throw Error.Argument(nameof(type), "Type {0} is marked as a mappable tpe, but is abstract so cannot be used directly to represent a FHIR datatype".FormatWith(type.Name));

            // Open generic type definitions can never appear as roots of objects
            // to parse. In instances, they will either have been used in closed type definitions
            // or as the closed type of a property. However, the FhirType attribute propagates to
            // these closed definitions, so we will allow having this attribute on an open generic,
            // it's not going to be directly mappable however.
            if (ReflectionHelper.IsOpenGenericTypeDefinition(type))
            {
                Message.Info("Type {0} is marked as a FhirType and is an open generic type, which cannot be used directly to represent a FHIR datatype", type.Name);
                return(false);
            }

            return(true);
        }
예제 #2
0
        public static bool TryCreate(Type type, out ClassMapping result, FhirRelease fhirVersion = (FhirRelease)int.MaxValue)
        {
            result = null;

            var typeAttribute = GetAttribute <FhirTypeAttribute>(type.GetTypeInfo(), fhirVersion);

            if (typeAttribute == null)
            {
                return(false);
            }

            if (ReflectionHelper.IsOpenGenericTypeDefinition(type))
            {
                Message.Info("Type {0} is marked as a FhirType and is an open generic type, which cannot be used directly to represent a FHIR datatype", type.Name);
                return(false);
            }

            result = new ClassMapping
            {
                Name       = collectTypeName(typeAttribute, type),
                IsResource = typeAttribute.IsResource || type.CanBeTreatedAsType(typeof(Resource)),
                IsCodeOfT  = ReflectionHelper.IsClosedGenericType(type) &&
                             ReflectionHelper.IsConstructedFromGenericTypeDefinition(type, typeof(Code <>)),
                NativeType          = type,
                IsNestedType        = typeAttribute.IsNestedType,
                _mappingInitializer = () => inspectProperties(type, fhirVersion)
            };

            return(true);
        }