Пример #1
0
 /// <summary>
 /// 获得活动目录实体
 /// </summary>
 /// <typeparam name="T">活动目录实体类型</typeparam>
 /// <param name="de">DirectoryEntry</param>
 /// <returns>活动目录实体</returns>
 public T GetEntity <T>(DirectoryEntry de) where T : IDirectoryEntity
 {
     try {
         T entityObject = (T)Activator.CreateInstance(typeof(T));
         foreach (PropertyInfo property in typeof(T).GetProperties())
         {
             foreach (object customAttribute in property.GetCustomAttributes(true))
             {
                 DirectoryAttributeAttribute attribute = customAttribute as DirectoryAttributeAttribute;
                 if (null != attribute)
                 {
                     object value = de.Properties[attribute.Attribute].Value;
                     property.SetValue(entityObject, Convertor.ChangeType(de.Properties[attribute.Attribute].Value, property.PropertyType), null);
                 }
             }
         }
         DirectoryEntity entity = entityObject as DirectoryEntity;
         entity.ParentGuid       = de.Parent.Guid;
         entity.ParentPath       = de.Parent.Path;
         entity.Path             = de.Path;
         entity.DirectoryEntry   = de;
         entity.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(entity_PropertyChanged);
         return(entityObject);
     } catch {
         return(default(T));
     }
 }
Пример #2
0
        /// <summary>
        /// 提交更改
        /// </summary>
        public void SubmitChanges()
        {
            foreach (DirectoryChangeInfo changeInfo in _changes.Values)
            {
                DirectoryEntry de = null;
                switch (changeInfo.ChangeType)
                {
                case ChangeType.Update:
                    de = changeInfo.Entity.DirectoryEntry;
                    foreach (string property in changeInfo.Properties)
                    {
                        PropertyInfo pi = changeInfo.Entity.GetType().GetProperty(property);
                        foreach (object customAttribute in pi.GetCustomAttributes(true))
                        {
                            DirectoryAttributeAttribute attribute = customAttribute as DirectoryAttributeAttribute;
                            if (null != attribute && !attribute.ReadOnly)
                            {
                                object value = pi.GetValue(changeInfo.Entity, null);
                                de.Properties[attribute.Attribute].Value = value;
                                break;
                            }
                        }
                    }
                    break;

                case ChangeType.Insert:
                    string schema = GetEntitySchemaClassType(changeInfo.Entity.GetType());
                    de = changeInfo.Parent.Children.Add(schema + "=" + changeInfo.Entity.Name, GetEntitySchemaClassName(changeInfo.Entity.GetType()));
                    changeInfo.Entity.DirectoryEntry = de;
                    foreach (PropertyInfo property in changeInfo.Entity.GetType().GetProperties())
                    {
                        foreach (object customAttribute in property.GetCustomAttributes(true))
                        {
                            DirectoryAttributeAttribute attribute = customAttribute as DirectoryAttributeAttribute;
                            if (null != attribute && !attribute.ReadOnly)
                            {
                                object value = property.GetValue(changeInfo.Entity, null);
                                if (null != value)
                                {
                                    de.Properties[attribute.Attribute].Value = value;
                                }
                            }
                        }
                    }

                    foreach (PropertyInfo property in changeInfo.Entity.GetType().GetProperties())
                    {
                        foreach (object customAttribute in property.GetCustomAttributes(true))
                        {
                            DirectoryAttributeAttribute attribute = customAttribute as DirectoryAttributeAttribute;
                            if (null != attribute)
                            {
                                object value = de.Properties[attribute.Attribute].Value;
                                property.SetValue(changeInfo.Entity, Convertor.ChangeType(de.Properties[attribute.Attribute].Value, property.PropertyType), null);
                            }
                        }
                    }
                    changeInfo.Entity.ParentGuid       = de.Parent.Guid;
                    changeInfo.Entity.ParentPath       = de.Parent.Path;
                    changeInfo.Entity.Path             = de.Path;
                    changeInfo.Entity.DirectoryEntry   = de;
                    changeInfo.Entity.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(entity_PropertyChanged);
                    break;

                case ChangeType.Delete:
                    de = changeInfo.Entity.DirectoryEntry;
                    de.DeleteTree();
                    break;
                }
                de.CommitChanges();
                de.Close();
            }
            _changes.Clear();
        }