using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
namespace MarvellousWorks
{
    
/// <summary>
    
/// 安全信息上下文
    
/// </summary>
    
/// <remarks>
    
///     为了同时满足Binary调用和Soap调用的需要,完全定制化了二进制序列化和SOAP序列化的过程
    
/// </remarks>

    [XmlRoot("securityContext")]
    [Serializable]
    
public class SecurityContext : IXmlSerializable, ISerializable
    
{
        
Protected Consts

        
Protected Fields

        
Constructors


        
/// <summary>
        
/// 访问Context内容项
        
/// </summary>
        
/// <param name="key"></param>
        
/// <returns></returns>
        
/// <remarks>设计上,这个类型不会随便抛出异常,除非提供的key值为空串</remarks>

        public virtual string this[string key]
        
{
            
get
            
{
                
if (string.IsNullOrEmpty(key))
                    
throw new ArgumentNullException("key");

                
string value;
                
if (dictionary.TryGetValue(key, out value))
                    
return value;
                
else
                    
return string.Empty;
            }

            
set
            
{
                
if (string.IsNullOrEmpty(key))
                    
throw new ArgumentNullException("key");

                
if (dictionary.ContainsKey(key))
                    dictionary[key] 
= value;
                
else
                    dictionary.Add(key, value);
            }

        }


        
IXmlSerializable Members


        
ISerializable Members        

        
Helper Methods
    }

}


相应的UnitTest

 1  /// <summary>
 2    /// 测试安全上下文
 3    /// </summary>

 4    [TestClass]
 5    public class SecurityContextFixture
 6    {
 7        /// <summary>
 8        /// Prepare
 9        /// </summary>
10        /// <returns></returns>

11        private SecurityContext CreateContext()
12        {
13            SecurityContext context = new SecurityContext();
14            context["hello"= "world";
15            context["1"= "first";
16            return context;
17        }

18
19        /// <summary>
20        /// 访问SecurityContext
21        /// </summary>

22        [TestMethod]
23        public void TestContextAccess()
24        {
25            SecurityContext context = CreateContext();
26
27            Assert.AreEqual<string>("world", context["hello"]);
28            Assert.AreEqual<string>("first", context["1"]);
29            Assert.IsTrue(string.IsNullOrEmpty(context["2"]));
30
31            context["1"= "First";
32