operator []= method

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

Implementation

operator []=(PropertyDefinition propertyDefinition, Object? value) {
  if (propertyDefinition.Version!.index >
      Owner!.Service.RequestedServerVersion.index) {
    throw 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 (!_loading) {
    // If the owner is new and if the property cannot be set, throw.
    if (Owner!.IsNew &&
        !propertyDefinition.HasFlag(
          PropertyDefinitionFlags.CanSet,
          _owner!.Service.RequestedServerVersion,
        )) {
      throw ServiceObjectPropertyException(
        /*Strings.PropertyIsReadOnly, */
        propertyDefinition,
      );
    }
    if (!Owner!.IsNew) {
      // If owner is an item attachment, properties cannot be updated (EWS doesn't support updating item attachments)
      if (Owner is Item && (Owner as Item).IsAttachment) {
        throw ServiceObjectPropertyException(
          /*Strings.ItemAttachmentCannotBeUpdated, */
          propertyDefinition,
        );
      }

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

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

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

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

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

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

    Changed();
  }
}