internal void SetProperties(ADObject server) { ADPropertyBag adpropertyBag = new ADPropertyBag(); adpropertyBag.SetIsReadOnly(false); foreach (PropertyDefinition propertyDefinition in this.Schema.AllProperties) { ADPropertyDefinition key = (ADPropertyDefinition)propertyDefinition; object value = server.propertyBag.Contains(key) ? server.propertyBag[key] : null; adpropertyBag.SetField(key, value); } MultiValuedProperty <string> multiValuedProperty = adpropertyBag[ADObjectSchema.ObjectClass] as MultiValuedProperty <string>; if (multiValuedProperty == null || multiValuedProperty.Count == 0) { multiValuedProperty = new MultiValuedProperty <string>(this.MostDerivedObjectClass); adpropertyBag.SetField(ADObjectSchema.ObjectClass, multiValuedProperty); } if (adpropertyBag[ADObjectSchema.WhenChangedUTC] == null) { DateTime utcNow = DateTime.UtcNow; adpropertyBag.SetField(ADObjectSchema.WhenChangedUTC, utcNow); adpropertyBag.SetField(ADObjectSchema.WhenCreatedUTC, utcNow); } adpropertyBag.SetIsReadOnly(true); this.propertyBag = adpropertyBag; }
internal static MiniVirtualDirectory CreateFrom(ADObject virtualDirectory, ICollection <PropertyDefinition> propertyDefinitions, object[] propertyValues) { MiniVirtualDirectory miniVirtualDirectory = new MiniVirtualDirectory(); IEnumerable <PropertyDefinition> allProperties = miniVirtualDirectory.Schema.AllProperties; ADPropertyBag adpropertyBag = new ADPropertyBag(); adpropertyBag.SetIsReadOnly(false); foreach (PropertyDefinition propertyDefinition in allProperties) { ADPropertyDefinition key = (ADPropertyDefinition)propertyDefinition; object value = virtualDirectory.propertyBag.Contains(key) ? virtualDirectory.propertyBag[key] : null; adpropertyBag.SetField(key, value); } MultiValuedProperty <string> multiValuedProperty = adpropertyBag[ADObjectSchema.ObjectClass] as MultiValuedProperty <string>; if (multiValuedProperty == null || multiValuedProperty.Count == 0) { multiValuedProperty = new MultiValuedProperty <string>(virtualDirectory.MostDerivedObjectClass); adpropertyBag.SetField(ADObjectSchema.ObjectClass, multiValuedProperty); } if (adpropertyBag[ADObjectSchema.WhenChangedUTC] == null) { DateTime utcNow = DateTime.UtcNow; adpropertyBag.SetField(ADObjectSchema.WhenChangedUTC, utcNow); adpropertyBag.SetField(ADObjectSchema.WhenCreatedUTC, utcNow); } if (propertyDefinitions != null && propertyValues != null) { adpropertyBag.SetProperties(propertyDefinitions, propertyValues); } adpropertyBag.SetIsReadOnly(true); miniVirtualDirectory.propertyBag = adpropertyBag; return(miniVirtualDirectory); }
public static TObject CreateFrom <TObject>(SimpleADObject simpleADObject, ADObjectSchema schema, IEnumerable <PropertyDefinition> additionalProperties) where TObject : ADRawEntry, new() { ArgumentValidator.ThrowIfNull("simpleADObject", simpleADObject); if (typeof(TObject).Equals(typeof(ADRawEntry))) { throw new ArgumentException("TObject cannot be ADRawEntry"); } ADPropertyBag adpropertyBag = new ADPropertyBag(); SimpleADObject.SimpleList <SimpleADObject.SimpleADProperty> properties = simpleADObject.Properties; SimpleADObject.SimpleADProperty simpleADProperty; if (!properties.TryGetValue(ADObjectSchema.Id.LdapDisplayName, out simpleADProperty)) { throw new InvalidOperationException("dn is missing"); } ValidationError validationError; ADObjectId value = (ADObjectId)ADValueConvertor.ConvertFromADAndValidateSingleValue(simpleADProperty.Value as string, ADObjectSchema.Id, false, out validationError); adpropertyBag.SetField(ADObjectSchema.Id, value); adpropertyBag.SetField(ADObjectSchema.ObjectState, simpleADObject.ObjectState); adpropertyBag.SetObjectVersion(simpleADObject.ExchangeVersion); TObject tobject = Activator.CreateInstance <TObject>(); IEnumerable <PropertyDefinition> enumerable; if (schema != null) { enumerable = schema.AllProperties; } else { enumerable = ((ADObject)((object)tobject)).Schema.AllProperties; } if (additionalProperties != null) { enumerable = enumerable.Concat(additionalProperties); } foreach (PropertyDefinition propertyDefinition in enumerable) { ADPropertyDefinition propertyDefinition2 = (ADPropertyDefinition)propertyDefinition; SimpleADObject.AddPropertyToPropertyBag(propertyDefinition2, adpropertyBag, properties); } if (tobject is MiniObject) { adpropertyBag.SetIsReadOnly(true); } if (schema != null || (!(tobject is ADRecipient) && !(tobject is MiniRecipient))) { tobject = (TObject)((object)ADObjectFactory.CreateAndInitializeConfigObject <TObject>(adpropertyBag, tobject, null)); } else { tobject = (TObject)((object)ADObjectFactory.CreateAndInitializeRecipientObject <TObject>(adpropertyBag, tobject, null)); } tobject.OriginatingServer = simpleADObject.OriginatingServer; tobject.WhenReadUTC = new DateTime?(simpleADObject.WhenReadUTC); tobject.DirectoryBackendType = simpleADObject.DirectoryBackendType; tobject.IsCached = true; tobject.ValidateRead(); tobject.ResetChangeTracking(); return(tobject); }