GetPropertyValueOrException method

Object? GetPropertyValueOrException(
  1. PropertyDefinition propertyDefinition,
  2. OutParam<ServiceLocalException> serviceExceptionOutParam
)
Gets the property value. The property definition. Exception that would be raised if there's an error retrieving the property.

Implementation

/* private */
Object? GetPropertyValueOrException(PropertyDefinition propertyDefinition,
    OutParam<ServiceLocalException> serviceExceptionOutParam) {
  OutParam<Object> propertyValueOutParam = new OutParam<Object>();
  propertyValueOutParam.param = null;
  serviceExceptionOutParam.param = null;

  if (propertyDefinition.Version!.index >
      this.Owner!.Service.RequestedServerVersion.index) {
    serviceExceptionOutParam.param =
        new ServiceVersionException("""string.Format(
                                      Strings.PropertyIncompatibleWithRequestVersion,
                                      propertyDefinition.Name,
                                      propertyDefinition.Version)""");
    return null;
  }

  if (this.TryGetValue(propertyDefinition, propertyValueOutParam)) {
    // If the requested property is in the bag, return it.
    return propertyValueOutParam.param;
  } else {
    if (propertyDefinition.HasFlagWithoutExchangeVersion(
        PropertyDefinitionFlags.AutoInstantiateOnRead)) {
      // The requested property is an auto-instantiate-on-read property
      ComplexPropertyDefinitionBase complexPropertyDefinition =
          propertyDefinition as ComplexPropertyDefinitionBase;

      EwsUtilities.Assert(
          complexPropertyDefinition != null,
          "PropertyBag.get_this[]",
          "propertyDefinition is marked with AutoInstantiateOnRead but is not a descendant of ComplexPropertyDefinitionBase");

      ComplexProperty propertyValue =
          complexPropertyDefinition.CreatePropertyInstance(this.Owner);
      propertyValueOutParam.param = propertyValue;

      if (propertyValue != null) {
        this.InitComplexProperty(propertyValue);
        this._properties[propertyDefinition] = propertyValue;
      }
    } else {
      // If the property is not the Id (we need to let developers read the Id when it's null) and if has
      // not been loaded, we throw.
      if (propertyDefinition != this.Owner!.GetIdPropertyDefinition()) {
        if (!this.IsPropertyLoaded(propertyDefinition)) {
          serviceExceptionOutParam.param =
              new ServiceObjectPropertyException.withMessage(
                  "Strings.MustLoadOrAssignPropertyBeforeAccess",
                  propertyDefinition);
          return null;
        }

        // Non-nullable properties (int, bool, etc.) must be assigned or loaded; cannot return null value.
        if (!propertyDefinition.IsNullable) {
          String errorMessage = this._IsRequestedProperty(propertyDefinition)
              ? "Strings.ValuePropertyNotLoaded"
              : "Strings.ValuePropertyNotAssigned";
          serviceExceptionOutParam.param =
              new ServiceObjectPropertyException.withMessage(
                  errorMessage, propertyDefinition);
        }
      }
    }

    return propertyValueOutParam.param;
  }
}