创建用于测试的目录条目实例

本文关键字:实例 用于 测试 创建 | 更新日期: 2023-09-27 17:56:00

>我正在尝试创建一个DirectoryEntry的实例,以便我可以使用它来测试一些将传递DirectoryEntry的代码。 但是,尽管进行了大量尝试,但我找不到实例化 DE 并初始化其 PropertyCollection 的方法。

我有以下代码,它是从 SO 上的另一个答案中获取和修改的,该答案正在执行相同的过程,但用于 SearchResult 对象。 似乎 Add 方法已被完全禁用,我找不到在 PropertyCollection 上调用构造函数以传入某些属性的方法。

using System.Collections;
using System.DirectoryServices;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
public static class DirectoryEntryFactory
{
    const BindingFlags nonPublicInstance = BindingFlags.NonPublic | BindingFlags.Instance;
    const BindingFlags publicInstance = BindingFlags.Public | BindingFlags.Instance;
    public static DirectoryEntry Construct<T>(T anonInstance)
    {
        var e = GetUninitializedObject<DirectoryEntry>();
        SetPropertiesField(e);
        var dictionary = (IDictionary)e.Properties;
        var type = typeof(T);
        var propertyInfos = type.GetProperties(publicInstance);
        foreach (var propertyInfo in propertyInfos)
        {
            var value = propertyInfo.GetValue(anonInstance, null);
            var valueCollection = GetUninitializedObject<PropertyValueCollection>();
            var innerList = GetInnerList(valueCollection);
            innerList.Add(value);
            var lowerKey = propertyInfo.Name.ToLower(CultureInfo.InvariantCulture);
            // These both throw exceptions saying you can't add to a PropertyCollection
            //(typeof(PropertyCollection)).InvokeMember("System.Collections.IDictionary.Add", nonPublicInstance | BindingFlags.InvokeMethod, null, dictionary, new object[] { propertyInfo.Name, value });
            //dictionary.Add(lowerKey, propertyCollection);
        }
        return e;
    }
    private static ArrayList GetInnerList(object propertyCollection)
    {
        var propertyInfo = typeof(PropertyValueCollection).GetProperty("InnerList", nonPublicInstance);
        return (ArrayList)propertyInfo.GetValue(propertyCollection, null);
    }
    private static void SetPropertiesField(DirectoryEntry e)
    {
        var propertiesField = typeof(DirectoryEntry).GetField("propertyCollection", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        propertiesField.SetValue(e, GetUninitializedObject<PropertyCollection>());
    }
    private static T GetUninitializedObject<T>()
    {
        return (T)FormatterServices.GetUninitializedObject(typeof(T));
    }
}

用法旨在

DirectoryEntry e = DirectoryEntryFactory.Construct(new { attr1 = "Hello", attr2 = "World"});

我希望我错过了一些东西,因为我对在愤怒中使用反思很陌生。

创建用于测试的目录条目实例

我不熟悉 DirectoryEntry 本身,但我是用于测试的适配器模式的忠实粉丝。你必须做这样的事情很烦人,但代码本身是微不足道的,可以将类放在项目文件夹中以将它们隐藏在远离主项目的地方。

例如,我有一个FileInfoAdapter和DirectoryInfoAdapter,它们包装那些接触文件系统的类。

文件信息适配器:

public class FileInfoAdapter : IFileInfo
{
    private readonly FileSystemInfo _fi;
    public FileInfoAdapter(string fileName)
        : this(new FileInfo(fileName))
    { }
    public FileInfoAdapter(FileSystemInfo fi)
    {
        _fi = fi;
    }
    public string Name { get { return _fi.Name; } }
    public string FullName { get { return _fi.FullName; } }
    public bool Exists { get { return _fi.Exists; } }
}