public string EncodeProperty(vCardProperty property)
{
if (property == null)
throw new ArgumentNullException("property");
if (string.IsNullOrEmpty(property.Name))
throw new ArgumentException();
StringBuilder builder = new StringBuilder();
builder.Append(property.Name);
foreach (vCardSubproperty subproperty in property.Subproperties)
{
builder.Append(';');
builder.Append(subproperty.Name);
if (!string.IsNullOrEmpty(subproperty.Value))
{
builder.Append('=');
builder.Append(subproperty.Value);
}
}
// The property name and all subproperties have been
// written to the string builder (the colon separator
// has not been written). The next step is to write
// the value. Depending on the type of value and any
// characters in the value, it may be necessary to
// use an non-default encoding. For example, byte arrays
// are written encoded in BASE64.
if (property.Value == null)
{
builder.Append(':');
}
else
{
Type valueType = property.Value.GetType();
if (valueType == typeof(byte[]))
{
// A byte array should be encoded in BASE64 format.
builder.Append(";ENCODING=b:");
builder.Append(EncodeBase64((byte[])property.Value));
}
else if (valueType == typeof(vCardValueCollection))
{
vCardValueCollection values = (vCardValueCollection)property.Value;
builder.Append(':');
for (int index = 0; index < values.Count; index++)
{
builder.Append(EncodeEscaped(values[index]));
if (index < values.Count - 1)
{
builder.Append(values.Separator);
}
}
}
else
{
// The object will be converted to a string (if it is
// not a string already) and encoded if necessary.
// The first step is to get the string value.
string stringValue = null;
if (valueType == typeof(char[]))
{
stringValue = new string(((char[])property.Value));
}
else
{
stringValue = property.Value.ToString();
}
builder.Append(':');
switch (property.Subproperties.GetValue("ENCODING"))
{
case "QUOTED-PRINTABLE":
builder.Append(EncodeQuotedPrintable(stringValue));
break;
default:
builder.Append(EncodeEscaped(stringValue));
break;
}
}
}
return builder.ToString();
}