首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何正确地在HttpSessionStateBase中模拟HttpSessionStateBase?

我如何正确地在HttpSessionStateBase中模拟HttpSessionStateBase?
EN

Stack Overflow用户
提问于 2011-08-09 00:04:41
回答 3查看 4.8K关注 0票数 8

我从下面的示例中设置了这个模拟会话对象:How to MOQ an Indexed property

代码语言:javascript
运行
复制
/// <summary>
/// HTTP session mockup.
/// </summary>
internal sealed class HttpSessionMock : HttpSessionStateBase
{
    private readonly Dictionary<string, object> objects = new Dictionary<string, object>();

    public override object this[string name]
    {
        get { return (objects.ContainsKey(name)) ? objects[name] : null; }
        set { objects[name] = value; }
    }
}

一些产生错误的示例代码..。

代码语言:javascript
运行
复制
var mockSession = new HttpSessionMock();
var keys = mockSession.Keys;

错误:方法或操作未实现。

我需要实现Keys属性,但无法创建KeysCollection对象。

做这件事最好的方法是什么?

编辑:解决方案

最后,我根据给出的答案更改了HttpSessionMock。这就是我最后的下场。(我还添加了对System.Linq的引用)。

代码语言:javascript
运行
复制
internal sealed class HttpSessionMock : HttpSessionStateBase
{
    private readonly NameValueCollection objects = new NameValueCollection();

    public override object this[string name]
    {
        get { return (objects.AllKeys.Contains(name)) ? objects[name] : null; }
        set { objects[name] = (string)value; }
    }

    public override NameObjectCollectionBase.KeysCollection Keys
    {
        get { return objects.Keys; }
    }
}

注意:这个模拟会话只存储字符串,而不是对象。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-07 20:11:46

我发现了原始方法和接受的解决方案的结合,既允许存储对象,也可以实现键属性:

代码语言:javascript
运行
复制
public class HttpSessionMock : HttpSessionStateBase
{
    private readonly NameValueCollection keyCollection = new NameValueCollection();
    private readonly Dictionary<string, object> objects = new Dictionary<string, object>();

    public override object this[string name]
    {
        get
        {
            object result = null;

            if (objects.ContainsKey(name))
            {
                result = objects[name];
            }

            return result;

        }
        set
        {
            objects[name] = value;
            keyCollection[name] = null;
        }
    }

    public override NameObjectCollectionBase.KeysCollection Keys
    {
        get { return keyCollection.Keys; }
    }
}
票数 16
EN

Stack Overflow用户

发布于 2011-08-09 01:43:54

单程:

代码语言:javascript
运行
复制
internal sealed class HttpSessionMock : HttpSessionStateBase
{
    public override NameObjectCollectionBase.KeysCollection Keys
    {
        get { return _collection.Keys; }
    }

    private readonly NameValueCollection _collection = new NameValueCollection();
}
票数 4
EN

Stack Overflow用户

发布于 2011-08-09 00:10:46

更新:下面是来自KeysCollection框架的.NET源代码,供您参考:

代码语言:javascript
运行
复制
public class KeysCollection : ICollection, IEnumerable
{
    // Fields
    private NameObjectCollectionBase _coll;

    // Methods
    internal KeysCollection(NameObjectCollectionBase coll)
    {
        this._coll = coll;
    }

    public virtual string Get(int index)
    {
        return this._coll.BaseGetKey(index);
    }

    public IEnumerator GetEnumerator()
    {
        return new NameObjectCollectionBase.NameObjectKeysEnumerator(this._coll);
    }

    void ICollection.CopyTo(Array array, int index)
    {
        if (array == null)
        {
            throw new ArgumentNullException("array");
        }
        if (array.Rank != 1)
        {
            throw new ArgumentException(SR.GetString("Arg_MultiRank"));
        }
        if (index < 0)
        {
            throw new ArgumentOutOfRangeException("index", SR.GetString("IndexOutOfRange", new object[] { index.ToString(CultureInfo.CurrentCulture) }));
        }
        if ((array.Length - index) < this._coll.Count)
        {
            throw new ArgumentException(SR.GetString("Arg_InsufficientSpace"));
        }
        IEnumerator enumerator = this.GetEnumerator();
        while (enumerator.MoveNext())
        {
            array.SetValue(enumerator.Current, index++);
        }
    }

    // Properties
    public int Count
    {
        get
        {
            return this._coll.Count;
        }
    }

    public string this[int index]
    {
        get
        {
            return this.Get(index);
        }
    }

    bool ICollection.IsSynchronized
    {
        get
        {
            return false;
        }
    }

    object ICollection.SyncRoot
    {
        get
        {
            return ((ICollection) this._coll).SyncRoot;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6990019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档