Partager via


Code Snippet: Delegate-based APIs

// Example: Action-based API (class library)

public

void ChangeItem(PropValue[] propsToSet, params Action<MapiMessage>[] actions)
{
using (MapiMessage mapiMessage = collector.ImportMessageChange( /* Contents Elided */ ))
{
foreach (Action<MapiMessage> action in actions)
{
action(mapiMessage);
}
mapiMessage.SaveChanges();
}
}

// Example: Action-based API (client)

Action

<MapiMessage> copyProps = new Action<MapiMessage>(delegate(MapiMessage mapiMessage)
{
using (MapiMessage sourceMessage = store.OpenEntry(messageProp[PropTag.EntryId].GetBytes()) as MapiMessage)
{
List<PropTag> propTagsToInclude = excludePropTags(new List<PropTag>(PropTagGroup.GetApptProps(store)), propsToExclude);
PropProblem[] problems = sourceMessage.CopyProps(mapiMessage, propTagsToInclude.ToArray());
}
});

Action

<MapiMessage> modifyRecipients = new Action<MapiMessage>(delegate(MapiMessage message)
{
message.ModifyRecipients(ModifyRecipientsFlags.AddRecipients,
new AdrEntry(
new PropValue(PropTag.DisplayName, "userInfo.DisplayName"),
new PropValue(PropTag.RecipientType, (int)RecipientType.To),
new PropValue(PropTag.AddrType, "EX"),
new PropValue(PropTag.EmailAddress, "userInfo.LegacyDn")));
});

outboxProp.SendItem(propsToSet.ToArray(), copyProps, modifyRecipients);

// Example: Predicate

Predicate

<PropValue> hasNoError = new Predicate<PropValue>(delegate(PropValue propValue)
{
if (propValue.IsNull() || propValue.IsError()) return false;
return true;
});

PropValue[] sourceProps = null;
PropValue[] destProps = Array.FindAll(sourceProps, hasNoError);

// Example: Converter

Converter

<PropValue, PropTag> getPropTag = new Converter<PropValue,PropTag>(delegate(PropValue propValue)
{
return propValue.PropTag;
});

PropValue

[] propValues = null;
PropTag[] propTags = Array.ConvertAll(propValues, getPropTag);

// Example: Comparision

Comparison

<PropValue> compareProp = new Comparison<PropValue>(delegate(PropValue lhs, PropValue rhs)
{
int propTag = Comparer<int>.Default.Compare((int)lhs.PropTag, (int)rhs.PropTag);
if (propTag != 0) return propTag;
if (object.Equals(lhs.Value, rhs.Value)) return 0;
return -1;
});

Array

.Sort<PropValue>(propValues, compareProp);