function typecastPropertyValue ($name, $v)
  {
    if ($this->isScalar ($name) && $this->isEnum ($name))
      $this->validateEnum ($name, $v);

    $type = $this->getTypeOf ($name);
    if ($type && !type::validate ($type, $v))
      throw new ComponentException ($this->component,
        sprintf (
          "%s is not a valid value for the <kbd>$name</kbd> property, which is of type <kbd>%s</kbd>",
          is_scalar ($v)
            ? sprintf ("The %s<kbd>%s</kbd>", typeOf ($v), var_export ($v, true))
            : sprintf ("A value of PHP type <kbd>%s</kbd>", typeOf ($v)),
          type::getNameOf ($type)
        ));

    // A special case for content type properties:
    // Convert a content property specified as attribute=string to a format equivalent to the one generated by
    // <subtag>string</subtag>
    if ($type == type::content && is_string ($v)) {
      $content = new Metadata($this->component->context, $name, type::metadata);
      $content->setChildren ([Text::from ($this->component->context, $v)]);
      return $content;
    }

    return type::typecast ($type, $v);
  }