Example #1
0
        /// <summary>
        /// Attempts to set the fields value.
        /// </summary>
        /// <param name="value">new value of the field</param>
        /// <exception cref="MetadataSystemException">Field type mismatch.</exception>
        /// <exception cref="MetadataSystemException">Field not found.</exception>
        public void SetFieldValue(string fieldName, object value)
        {
            Type type = value.GetType();

            int index = Array.IndexOf <string>(indices, fieldName);

            if (index != -1)
            {
                MetadataField field = fields[index];

                if (field.TypeOfValue == type)
                {
                    field.SetValue(value);
                }
                else
                {
                    // Field type mismatch.
                    throw MetadataSystemException.TypeMismatch(name, fieldName, type, field.TypeOfValue);
                }
            }
            else
            {
                // Field not found.
                throw MetadataSystemException.FieldNotFound(name, fieldName);
            }
        }
Example #2
0
        /// <summary>
        /// Attempts to read a field value from this object.
        /// </summary>
        /// <typeparam name="T">type of the field</typeparam>
        /// <param name="fieldName">name of the field</param>
        /// <returns>fields value</returns>
        /// <exception cref="MetadataSystemException">Field type mismatch.</exception>
        /// <exception cref="MetadataSystemException">Field not found.</exception>
        public T GetFieldValue <T>(string fieldName)
        {
            Type type = typeof(T);

            int index = Array.IndexOf <string>(indices, fieldName);

            if (index != -1)
            {
                MetadataField field = fields[index];

                if (field.TypeOfValue == type)
                {
                    return(field.GetValue <T>());
                }

                // Field type mismatch.
                throw MetadataSystemException.TypeMismatch(name, fieldName, type, field.TypeOfValue);
            }

            // Field not found.
            throw MetadataSystemException.FieldNotFound(name, fieldName);
        }