Skip to content

Commit

Permalink
- added dynamic dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
kkozmic committed Oct 13, 2011
1 parent bbf9d30 commit fd6de51
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Castle.Core.Tests/Castle.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\net35\adodb.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Rhino.Mocks.CPP.Interfaces">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\net35\Rhino.Mocks.CPP.Interfaces.dll</HintPath>
Expand Down Expand Up @@ -205,6 +206,7 @@
<Compile Include="Components.DictionaryAdapter.Tests\AdaptingGenericDictionaryTestCase.cs" />
<Compile Include="Components.DictionaryAdapter.Tests\Address.cs" />
<Compile Include="Components.DictionaryAdapter.Tests\Color.cs" />
<Compile Include="Components.DictionaryAdapter.Tests\DynamicDictionaryTests.cs" />
<Compile Include="Components.DictionaryAdapter.Tests\IEmptyTest.cs" />
<Compile Include="Components.DictionaryAdapter.Tests\IBook.cs" />
<Compile Include="Components.DictionaryAdapter.Tests\MemberwiseEquaityHashCodeStrategyTestCase.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Components.DictionaryAdapter.Tests
{
using System.Collections.Generic;

using NUnit.Framework;

#if !(SILVERLIGHT || DOTNET35)
public class DynamicDictionaryTests
{
[Test]
public void Can_add_to_dictionary_via_dynamic_object()
{
var dictionary = new Dictionary<string, object>();
dynamic adapter = new DynamicDictionary(dictionary);
adapter.Name = "stefan mucha";

Assert.AreEqual("stefan mucha", dictionary["Name"]);
}

[Test]
public void Can_override_value_from_dictionary_via_dynamic_object()
{
var dictionary = new Dictionary<string, object>();
dynamic adapter = new DynamicDictionary(dictionary);
dictionary["Name"] = "adam mickiewicz";
adapter.Name = "stefan mucha";

Assert.AreEqual("stefan mucha", adapter.Name);
}

[Test]
public void Can_read_from_dictionary_via_dynamic_object()
{
var dictionary = new Dictionary<string, object>();
dynamic adapter = new DynamicDictionary(dictionary);
dictionary["Name"] = "stefan mucha";

Assert.AreEqual("stefan mucha", adapter.Name);
}

[Test]
public void Can_read_non_existing_value_from_dictionary_via_dynamic_object()
{
var dictionary = new Dictionary<string, object>();
dynamic adapter = new DynamicDictionary(dictionary);

Assert.IsNull(adapter.Name);
}
}
#endif
}
1 change: 1 addition & 0 deletions src/Castle.Core/Castle.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<Compile Include="Components.DictionaryAdapter\Attributes\RemoveIfAttribute.cs" />
<Compile Include="Components.DictionaryAdapter\Attributes\XmlDefaultsAttribute.cs" />
<Compile Include="Components.DictionaryAdapter\Attributes\StringStorageAttribute.cs" />
<Compile Include="Components.DictionaryAdapter\DynamicDictionary.cs" />
<Compile Include="Components.DictionaryAdapter\MemberwiseEqualityHashCodeStrategy.cs" />
<Compile Include="Components.DictionaryAdapter\DictionaryAdapterBase.Copy.cs" />
<Compile Include="Components.DictionaryAdapter\IDictionaryCopyStrategy.cs" />
Expand Down
54 changes: 54 additions & 0 deletions src/Castle.Core/Components.DictionaryAdapter/DynamicDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Components.DictionaryAdapter
{
#if !(SILVERLIGHT || DOTNET35)
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;

/// <summary>
/// Wraps a <see cref="IDictionary"/> with a dynamic object to expose a bit better looking API.
/// The implementation is trivial and assumes keys are <see cref="string"/>s.
/// </summary>
public class DynamicDictionary : DynamicObject
{
private readonly IDictionary dictionary;

public DynamicDictionary(IDictionary dictionary)
{
this.dictionary = dictionary;
}

public override IEnumerable<string> GetDynamicMemberNames()
{
return from object key in dictionary.Keys select key.ToString();
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = dictionary[binder.Name];
return true;
}

public override bool TrySetMember(SetMemberBinder binder, object value)
{
dictionary[binder.Name] = value;
return true;
}
}
#endif
}

0 comments on commit fd6de51

Please sign in to comment.