public static PropValue Pack(object value, MapiPropertyDefinition propertyDefinition)
        {
            PropTag  propertyTag = propertyDefinition.PropertyTag;
            PropType propType    = propertyTag.ValueType();
            Type     type        = MapiPropValueConvertor.TypeFromPropType(propType, false);

            if (value != null)
            {
                if (typeof(byte[]) == type)
                {
                    if (typeof(Guid) == value.GetType())
                    {
                        return(new PropValue(propertyTag, ((Guid)value).ToByteArray()));
                    }
                    if (typeof(Schedule) == value.GetType())
                    {
                        return(new PropValue(propertyTag, ((Schedule)value).ToByteArray()));
                    }
                }
                if (typeof(DateTime) == type && typeof(DateTime) == value.GetType())
                {
                    return(new PropValue(propertyTag, ((DateTime)value).ToUniversalTime()));
                }
                if (typeof(DateTime[]) == type)
                {
                    Type type2 = value.GetType();
                    if (typeof(MultiValuedProperty <DateTime>) == type2 || typeof(DateTime[]) == type2)
                    {
                        List <DateTime> list = new List <DateTime>(((ICollection)value).Count);
                        foreach (object obj in ((ICollection)value))
                        {
                            list.Add(((DateTime)obj).ToUniversalTime());
                        }
                        return(new PropValue(propertyTag, list.ToArray()));
                    }
                }
                if (type.IsAssignableFrom(value.GetType()))
                {
                    return(new PropValue(propertyTag, value));
                }
            }
            object value2 = null;

            if (MapiPropValueConvertor.TryCastValueToPack(value, propType, out value2))
            {
                return(new PropValue(propertyTag, value2));
            }
            throw MapiPropValueConvertor.ConstructPackingException(value, propertyDefinition, Strings.ConstantNa);
        }
 internal static bool TryCastValueToPack(object value, PropType type, out object valueToPack)
 {
     valueToPack = null;
     try
     {
         valueToPack = Convert.ChangeType(value, MapiPropValueConvertor.TypeFromPropType(type, false));
     }
     catch (ArgumentNullException)
     {
         return(false);
     }
     catch (InvalidCastException)
     {
         return(false);
     }
     catch (OverflowException)
     {
         return(false);
     }
     return(true);
 }
        public static object Extract(PropValue value, MapiPropertyDefinition propertyDefinition)
        {
            bool isReadOnly     = propertyDefinition.IsReadOnly;
            Type type           = propertyDefinition.Type;
            Type underlyingType = Nullable.GetUnderlyingType(type);

            if (null != underlyingType)
            {
                type = underlyingType;
            }
            if (propertyDefinition.PropertyTag.Id() != value.PropTag.Id())
            {
                throw MapiPropValueConvertor.ConstructExtractingException(value, propertyDefinition, Strings.ExceptionUnmatchedPropTag(propertyDefinition.PropertyTag.ToString(), value.PropTag.ToString()));
            }
            if (PropType.Binary == value.PropType)
            {
                if (typeof(Guid) == type)
                {
                    return(new Guid(value.GetBytes()));
                }
                if (typeof(Schedule) == type)
                {
                    return(Schedule.FromByteArray(value.GetBytes()));
                }
                if (typeof(MapiEntryId) == type)
                {
                    return(new MapiEntryId(value.GetBytes()));
                }
            }
            if (typeof(short) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <short>(isReadOnly, propertyDefinition, value.GetShortArray()));
                }
                return(value.GetShort());
            }
            else if (typeof(int) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <int>(isReadOnly, propertyDefinition, value.GetIntArray()));
                }
                return(value.GetInt());
            }
            else if (typeof(long) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <long>(isReadOnly, propertyDefinition, value.GetLongArray()));
                }
                return(value.GetLong());
            }
            else if (typeof(float) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <float>(isReadOnly, propertyDefinition, value.GetFloatArray()));
                }
                return(value.GetFloat());
            }
            else if (typeof(double) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <double>(isReadOnly, propertyDefinition, value.GetDoubleArray()));
                }
                return(value.GetDouble());
            }
            else if (typeof(DateTime) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    DateTime[]      dateTimeArray = value.GetDateTimeArray();
                    List <DateTime> list          = new List <DateTime>(dateTimeArray.Length);
                    foreach (DateTime dateTime in dateTimeArray)
                    {
                        list.Add(dateTime.ToLocalTime());
                    }
                    return(new MultiValuedProperty <DateTime>(isReadOnly, propertyDefinition, list.ToArray()));
                }
                return(value.GetDateTime().ToLocalTime());
            }
            else if (typeof(bool) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <bool>(isReadOnly, propertyDefinition, value.GetBoolArray()));
                }
                return(value.GetBoolean());
            }
            else if (typeof(string) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <string>(isReadOnly, propertyDefinition, value.GetStringArray()));
                }
                return(value.GetString());
            }
            else if (typeof(Guid) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <Guid>(isReadOnly, propertyDefinition, value.GetGuidArray()));
                }
                return(value.GetGuid());
            }
            else if (typeof(byte[]) == type)
            {
                if (propertyDefinition.IsMultivalued)
                {
                    return(new MultiValuedProperty <byte[]>(isReadOnly, propertyDefinition, value.GetBytesArray()));
                }
                return(value.GetBytes());
            }
            else
            {
                if (typeof(short[]) == type)
                {
                    return(value.GetShortArray());
                }
                if (typeof(int[]) == type)
                {
                    return(value.GetIntArray());
                }
                if (typeof(long[]) == type)
                {
                    return(value.GetLongArray());
                }
                if (typeof(float[]) == type)
                {
                    return(value.GetFloatArray());
                }
                if (typeof(double[]) == type)
                {
                    return(value.GetDoubleArray());
                }
                if (typeof(DateTime[]) == type)
                {
                    DateTime[]      dateTimeArray2 = value.GetDateTimeArray();
                    List <DateTime> list2          = new List <DateTime>(dateTimeArray2.Length);
                    foreach (DateTime dateTime2 in dateTimeArray2)
                    {
                        list2.Add(dateTime2.ToLocalTime());
                    }
                    return(list2.ToArray());
                }
                if (typeof(bool[]) == type)
                {
                    return(value.GetBoolArray());
                }
                if (typeof(string[]) == type)
                {
                    return(value.GetStringArray());
                }
                if (typeof(Guid[]) == type)
                {
                    return(value.GetGuidArray());
                }
                if (typeof(byte[][]) == type)
                {
                    return(value.GetBytesArray());
                }
                if (type.IsAssignableFrom(MapiPropValueConvertor.TypeFromPropType(value.PropType, false)))
                {
                    return(value.RawValue);
                }
                object result = null;
                if (MapiPropValueConvertor.TryCastValueToExtract(value, type, out result))
                {
                    return(result);
                }
                throw MapiPropValueConvertor.ConstructExtractingException(value, propertyDefinition, Strings.ConstantNa);
            }
        }
Beispiel #4
0
 private MapiPropertyDefinition(string name, Type type, PropTag propertyTag, MapiPropertyDefinitionFlags propertyDefinitionFlags, object defaultValue, object initialValue, MapiPropValueExtractorDelegate propertyValueExtractor, MapiPropValuePackerDelegate propertyValuePacker, PropertyDefinitionConstraint[] readConstraints, PropertyDefinitionConstraint[] writeConstraints, ProviderPropertyDefinition[] supportingProperties, CustomFilterBuilderDelegate customFilterBuilderDelegate, GetterDelegate getterDelegate, SetterDelegate setterDelegate) : base(name ?? propertyTag.ToString(), ExchangeObjectVersion.Exchange2003, type ?? MapiPropValueConvertor.TypeFromPropType(propertyTag.ValueType(), true), defaultValue, readConstraints ?? PropertyDefinitionConstraint.None, writeConstraints ?? PropertyDefinitionConstraint.None, supportingProperties ?? ProviderPropertyDefinition.None, customFilterBuilderDelegate, getterDelegate, setterDelegate)
 {
     this.propertyTag = propertyTag;
     if (((PropTag)12288U & propertyTag) != PropTag.Null)
     {
         propertyDefinitionFlags |= MapiPropertyDefinitionFlags.MultiValued;
     }
     this.propertyDefinitionFlags = propertyDefinitionFlags;
     this.initialValue            = initialValue;
     this.propertyValueExtractor  = (propertyValueExtractor ?? new MapiPropValueExtractorDelegate(MapiPropValueConvertor.Extract));
     this.propertyValuePacker     = (propertyValuePacker ?? new MapiPropValuePackerDelegate(MapiPropValueConvertor.Pack));
 }