Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
Properties
TestFiles
ChartTest.cs
ConversionUtilTest.cs
CryptoTest.cs
DynamicDictionary.cs
DynamicHelperTest.cs
DynamicWrapper.cs
HelperResultTest.cs
JsonTest.cs
ObjectInfoTest.cs
PreComputedGridDataSourceTest.cs
ServerInfoTest.cs
System.Web.Helpers.Test.csproj
WebCacheTest.cs
WebGridDataSourceTest.cs
WebGridTest.cs
WebImageTest.cs
WebMailTest.cs
XhtmlAssert.cs
packages.config
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/aspnetwebstack/test/System.Web.Helpers.Test/DynamicDictionary.cs

106 lines
3.9 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
namespace System.Web.Helpers.Test
{
/// <summary>
/// Dynamic object implementation over a dictionary that doesn't implement anything but the interface.
/// Used for testing our types that consume dynamic objects to make sure they don't make any assumptions on the implementation.
/// </summary>
public class DynamicDictionary : IDynamicMetaObjectProvider
{
private readonly Dictionary<string, object> _values = new Dictionary<string, object>();
public object this[string name]
{
get
{
object result;
_values.TryGetValue(name, out result);
return result;
}
set { _values[name] = value; }
}
public DynamicMetaObject GetMetaObject(Expression parameter)
{
return new DynamicDictionaryMetaObject(parameter, this);
}
private class DynamicDictionaryMetaObject : DynamicMetaObject
{
private static readonly PropertyInfo ItemPropery = typeof(DynamicDictionary).GetProperty("Item");
public DynamicDictionaryMetaObject(Expression expression, object value)
: base(expression, BindingRestrictions.Empty, value)
{
}
private IDictionary<string, object> WrappedDictionary
{
get { return ((DynamicDictionary)Value)._values; }
}
private Expression GetDynamicExpression()
{
return Expression.Convert(Expression, typeof(DynamicDictionary));
}
private Expression GetIndexExpression(string key)
{
return Expression.MakeIndex(
GetDynamicExpression(),
ItemPropery,
new[]
{
Expression.Constant(key)
}
);
}
private Expression GetSetValueExpression(string key, object value)
{
return Expression.Assign(
GetIndexExpression(key),
Expression.Convert(Expression.Constant(value),
typeof(object))
);
}
public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
{
var binderDefault = binder.FallbackGetMember(this);
var expression = Expression.Convert(GetIndexExpression(binder.Name),
typeof(object));
var dynamicSuggestion = new DynamicMetaObject(expression, BindingRestrictions.GetTypeRestriction(Expression, LimitType)
.Merge(binderDefault.Restrictions));
return binder.FallbackGetMember(this, dynamicSuggestion);
}
public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
{
var binderDefault = binder.FallbackSetMember(this, value);
Expression expression = GetSetValueExpression(binder.Name, value.Value);
var dynamicSuggestion = new DynamicMetaObject(expression, BindingRestrictions.GetTypeRestriction(Expression, LimitType)
.Merge(binderDefault.Restrictions));
return binder.FallbackSetMember(this, value, dynamicSuggestion);
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return WrappedDictionary.Keys;
}
}
}
}