示例#1
0
        public static SimpleADObject CreateFromRawEntry(ADRawEntry adRawEntry, IEnumerable <PropertyDefinition> properties, bool includeNullProperties)
        {
            ArgumentValidator.ThrowIfNull("adRawEntry", adRawEntry);
            ArgumentValidator.ThrowIfNull("properties", properties);
            SimpleADObject simpleADObject = new SimpleADObject();

            simpleADObject.OriginatingServer    = adRawEntry.OriginatingServer;
            simpleADObject.ObjectState          = adRawEntry.ObjectState;
            simpleADObject.WhenReadUTC          = adRawEntry.WhenReadUTC.Value;
            simpleADObject.DirectoryBackendType = adRawEntry.DirectoryBackendType;
            simpleADObject.ExchangeVersion      = adRawEntry.ExchangeVersion;
            foreach (PropertyDefinition propertyDefinition in properties)
            {
                ADPropertyDefinition adpropertyDefinition = (ADPropertyDefinition)propertyDefinition;
                if (adpropertyDefinition == null)
                {
                    ArgumentValidator.ThrowIfTypeInvalid("properties", properties, typeof(IEnumerable <ADPropertyDefinition>));
                }
                if (adpropertyDefinition.LdapDisplayName != null && !adpropertyDefinition.IsCalculated)
                {
                    object obj = null;
                    if (!adRawEntry.propertyBag.TryGetField(adpropertyDefinition, ref obj) || obj == null)
                    {
                        if (includeNullProperties)
                        {
                            simpleADObject.AddProperty(new SimpleADObject.SimpleADProperty
                            {
                                Name  = adpropertyDefinition.LdapDisplayName,
                                Value = null
                            });
                        }
                    }
                    else
                    {
                        if (adpropertyDefinition.IsMultivalued)
                        {
                            MultiValuedPropertyBase multiValuedPropertyBase = (MultiValuedPropertyBase)obj;
                            if (multiValuedPropertyBase == null || multiValuedPropertyBase.Count == 0)
                            {
                                if (includeNullProperties)
                                {
                                    simpleADObject.AddProperty(new SimpleADObject.SimpleADProperty
                                    {
                                        Name  = adpropertyDefinition.LdapDisplayName,
                                        Value = null
                                    });
                                    continue;
                                }
                                continue;
                            }
                        }
                        SimpleADObject.SimpleADProperty simpleADProperty = new SimpleADObject.SimpleADProperty();
                        simpleADProperty.Name = adpropertyDefinition.LdapDisplayName;
                        ADObjectId adobjectId = obj as ADObjectId;
                        if (!adpropertyDefinition.IsMultivalued)
                        {
                            if (adpropertyDefinition.IsBinary)
                            {
                                simpleADProperty.Value = ADValueConvertor.ConvertValueToBinary(obj, adpropertyDefinition.FormatProvider);
                            }
                            else if (adobjectId != null && adpropertyDefinition.IsSoftLinkAttribute && string.IsNullOrEmpty(adobjectId.DistinguishedName))
                            {
                                simpleADProperty.Value = adobjectId.ToSoftLinkValue();
                            }
                            else if (adobjectId != null)
                            {
                                simpleADProperty.Value = adobjectId.ToExtendedDN();
                            }
                            else
                            {
                                simpleADProperty.Value = ADValueConvertor.ConvertValueToString(obj, adpropertyDefinition.FormatProvider);
                            }
                        }
                        else
                        {
                            IEnumerable enumerable = (IEnumerable)obj;
                            if (adpropertyDefinition.IsBinary)
                            {
                                List <byte[]> list = new List <byte[]>();
                                foreach (object originalValue in enumerable)
                                {
                                    list.Add(ADValueConvertor.ConvertValueToBinary(originalValue, adpropertyDefinition.FormatProvider));
                                }
                                if (list.Count == 0)
                                {
                                    list = null;
                                }
                                simpleADProperty.Value = list;
                            }
                            else
                            {
                                List <string> list2 = new List <string>();
                                foreach (object obj2 in enumerable)
                                {
                                    adobjectId = (obj2 as ADObjectId);
                                    if (adobjectId != null)
                                    {
                                        list2.Add(adobjectId.ToExtendedDN());
                                    }
                                    else
                                    {
                                        list2.Add(ADValueConvertor.ConvertValueToString(obj2, adpropertyDefinition.FormatProvider));
                                    }
                                }
                                if (list2.Count == 0)
                                {
                                    list2 = null;
                                }
                                simpleADProperty.Value = list2;
                            }
                        }
                        simpleADObject.AddProperty(simpleADProperty);
                    }
                }
            }
            return(simpleADObject);
        }