Exemplo n.º 1
0
        /// <summary>
        /// Creates a <see cref="JsonObjectContract"/> for the given type.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <returns>A <see cref="JsonObjectContract"/> for the given type.</returns>
        protected virtual ObjectContract CreateObjectContract(Type objectType)
        {
            ObjectContract contract = new ObjectContract(objectType);

            InitializeContract(contract);

            var scheme = TryResolveDsdlType(objectType);

            contract.DsdlType   = scheme.ConcreteType;
            contract.UavcanType = scheme.RootType;

            contract.Properties.AddRange(CreateProperties(contract.NonNullableUnderlyingType, scheme.ConcreteType));

            if (contract.IsInstantiable)
            {
                if (contract.DefaultCreator == null || contract.DefaultCreatorNonPublic)
                {
                    throw new NotSupportedException($"Public default constructor expected, '{contract.UnderlyingType.FullName}'.");
                }
            }

            ValidateDsdlTypeCompatibility(contract, scheme.ConcreteType);

            return(contract);
        }
Exemplo n.º 2
0
        void ValidateDsdlTypeCompatibility(ObjectContract contract, CompositeDsdlTypeBase scheme)
        {
            if (scheme == null)
            {
                return;
            }

            foreach (var member in scheme.Fields)
            {
                if (member.Type is VoidDsdlType)
                {
                    continue;
                }

                if (contract.Properties.GetProperty(member.Name, StringComparison.Ordinal) == null)
                {
                    throw new InvalidOperationException($"Cannot find {member.Name} member for type {contract.UnderlyingType.FullName}.");
                }
            }
        }