operator []= method

void operator []=(
  1. PropertyDefinition propertyDefinition,
  2. Object? value
)

Implementation

operator []=(PropertyDefinition propertyDefinition, Object? value) {
  if (propertyDefinition.Version!.index >
      this.Owner!.Service.RequestedServerVersion.index) {
    throw new ServiceVersionException("""string.Format(
                      Strings.PropertyIncompatibleWithRequestVersion,
                      propertyDefinition.Name,
                      propertyDefinition.Version)""");
  }

  // If the property bag is not in the loading state, we need to verify whether
  // the property can actually be set or updated.
  if (!this._loading) {
    // If the owner is new and if the property cannot be set, throw.
    if (this.Owner!.IsNew &&
        !propertyDefinition.HasFlag(PropertyDefinitionFlags.CanSet,
            _owner!.Service.RequestedServerVersion)) {
      throw new ServiceObjectPropertyException(
          /*Strings.PropertyIsReadOnly, */
          propertyDefinition);
    }
    if (!this.Owner!.IsNew) {
      // If owner is an item attachment, properties cannot be updated (EWS doesn't support updating item attachments)
      if (this.Owner is Item && (this.Owner as Item).IsAttachment) {
        throw new ServiceObjectPropertyException(
            /*Strings.ItemAttachmentCannotBeUpdated, */
            propertyDefinition);
      }

      // If the property cannot be deleted, throw.
      if (value == null &&
          !propertyDefinition.HasFlagWithoutExchangeVersion(
              PropertyDefinitionFlags.CanDelete)) {
        throw new ServiceObjectPropertyException(
            /*Strings.PropertyCannotBeDeleted,*/
            propertyDefinition);
      }

      // If the property cannot be updated, throw.
      if (!propertyDefinition.HasFlagWithoutExchangeVersion(
          PropertyDefinitionFlags.CanUpdate)) {
        throw new ServiceObjectPropertyException(
            /*"Strings.PropertyCannotBeUpdated",*/
            propertyDefinition);
      }
    }
  }

  // If the value is set to null, delete the property.
  if (value == null) {
    this.DeleteProperty(propertyDefinition);
  } else {
    ComplexProperty complexProperty;

    if (this._properties.containsKey(propertyDefinition)) {
      if (this._properties[propertyDefinition] is ComplexProperty) {
        (this._properties[propertyDefinition] as ComplexProperty)
            .removeChangeEvent(this.PropertyChanged);
      }
    }

    // If the property was to be deleted, the deletion becomes an update.
    if (this._deletedProperties.containsKey(propertyDefinition)) {
      this._deletedProperties.remove(propertyDefinition);
      AddToChangeList(propertyDefinition, this._modifiedProperties);
    } else {
      // If the property value was not set, we have a newly set property.
      if (!this._properties.containsKey(propertyDefinition)) {
        AddToChangeList(propertyDefinition, this._addedProperties);
      } else {
        // The last case is that we have a modified property.
        if (!this._modifiedProperties.contains(propertyDefinition)) {
          AddToChangeList(propertyDefinition, this._modifiedProperties);
        }
      }
    }

    if (value is ComplexProperty) {
      this.InitComplexProperty(value);
    }
    this._properties[propertyDefinition] = value;

    this.Changed();
  }
}