Skip to content

Commit

Permalink
Support for overloaded indexers
Browse files Browse the repository at this point in the history
  • Loading branch information
chaquotay committed Jan 20, 2018
1 parent c16206c commit a51c129
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
55 changes: 55 additions & 0 deletions src/PropertyAccess.Test/IndexedClassPropertyAccessTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,60 @@ public void TestSetPropertyGeneric()
sut.SetValue(_testTarget, "Bar", 47);
Assert.AreEqual(47, _testTarget["Bar"]);
}

[Test]
public void TestSetPropertyIndexerOverloaded()
{
var indexed = new OverloadedIndexer();
var sut = PropertyAccessFactory.CreateClassIndexed(typeof(OverloadedIndexer), typeof(long), "Item");
sut.SetValue(indexed, 42L, "Fortytwo");
Assert.AreEqual("Fortytwo", indexed[42]);
}

[Test]
public void TestGetPropertyIndexerOverloaded()
{
var indexed = new OverloadedIndexer();
indexed[42] = "Fortytwo";
var sut = PropertyAccessFactory.CreateClassIndexed(typeof(OverloadedIndexer), typeof(long), "Item");
var actual = sut.GetValue(indexed, 42L);
Assert.AreEqual("Fortytwo", actual);
}

[Test]
public void TestSetPropertyGenericIndexerOverloaded()
{
var indexed = new OverloadedIndexer();
var sut = PropertyAccessFactory.CreateClassIndexed<OverloadedIndexer, long, string>("Item");
sut.SetValue(indexed, 42, "Fortytwo");
Assert.AreEqual("Fortytwo", indexed[42]);
}

[Test]
public void TestGetPropertyGenericIndexerOverloaded()
{
var indexed = new OverloadedIndexer();
indexed[42] = "Fortytwo";
var sut = PropertyAccessFactory.CreateClassIndexed<OverloadedIndexer, long, string>("Item");
var actual = sut.GetValue(indexed, 42);
Assert.AreEqual("Fortytwo", actual);
}

public class OverloadedIndexer
{
private readonly Dictionary<long, string> _values = new Dictionary<long, string>();

public string this[int key]
{
get { return _values[key]; }
set { _values[key] = value; }
}

public string this[long key]
{
get { return _values[key]; }
set { _values[key] = value; }
}
}
}
}
12 changes: 9 additions & 3 deletions src/PropertyAccess/PropertyAccessFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;

namespace PropertyAccess
Expand Down Expand Up @@ -80,17 +81,22 @@ public static DelegateValuePropertyAccess<TTarget, TResult> CreateForValue<TTarg

public static IIndexedClassPropertyAccess CreateClassIndexed(Type targetType, Type indexType, string name)
{
var propertyInfo = targetType.GetProperty(name);
if (propertyInfo == null)
var properties = (from property in targetType.GetProperties()
let indexerParameters = property.GetIndexParameters()
where indexerParameters.Length == 1 && indexerParameters[0].ParameterType == indexType
select property).ToList();

if (properties.Count != 1)
return null;

var propertyInfo = properties.Single();
var type = typeof(IndexedDelegateClassPropertyAccess<,,>).MakeGenericType(targetType, indexType, propertyInfo.PropertyType);
return (IIndexedClassPropertyAccess)Activator.CreateInstance(type, propertyInfo);
}

public static IndexedDelegateClassPropertyAccess<TTarget, TIndex, TResult> CreateClassIndexed<TTarget, TIndex, TResult>(string name) where TTarget : class
{
var propertyInfo = typeof(TTarget).GetProperty(name);
var propertyInfo = typeof(TTarget).GetProperty(name, typeof(TResult), new []{ typeof(TIndex) });
return new IndexedDelegateClassPropertyAccess<TTarget, TIndex, TResult>(propertyInfo);
}

Expand Down

0 comments on commit a51c129

Please sign in to comment.