Skip to content

Commit 9568d3a

Browse files
committed
Fix failed to create serializer for type which have indexer(s) and do not implement IDictionary. Issue #49
1 parent 15d6f79 commit 9568d3a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/MsgPack/Serialization/SerializationTarget.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ private static bool CheckTargetEligibility( MemberInfo member )
203203

204204
if ( asProperty != null )
205205
{
206+
if ( asProperty.GetIndexParameters().Length > 0 )
207+
{
208+
// Indexer cannot be target except the type itself implements IDictionary or IDictionary<TKey,TValue>
209+
return false;
210+
}
211+
206212
#if !NETFX_CORE
207213
if ( asProperty.GetSetMethod( true ) != null )
208214
#else

test/MsgPack.UnitTest/Serialization/SerializationTargetTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ public void TestAliasInDataMember()
107107
Assert.That( target.Any( m => m.Contract.Name == "Alias" && m.Contract.Name != m.Member.Name ) );
108108
}
109109

110+
[Test]
111+
public void TestIndexerOverload()
112+
{
113+
Assert.That( SerializationTarget.GetTargetMembers( typeof( WithIndexerOverload ) ).Any(), Is.False );
114+
}
115+
110116
[Test]
111117
public void TestDuplicatedKey()
112118
{

test/MsgPack.UnitTest/Serialization/SerializationTargets.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,22 @@ public DataMamberClass()
247247
#endif // !NETFX_CORE && !SILVERLIGHT
248248
}
249249
}
250+
251+
public class WithIndexerOverload
252+
{
253+
public string this[ int index ]
254+
{
255+
get { return null; }
256+
set { }
257+
}
258+
259+
public string this[ string key ]
260+
{
261+
get { return null; }
262+
set { }
263+
}
264+
}
265+
250266
public class WithKeyDuplicate
251267
{
252268
[MessagePackMember( 0, Name = "Boo" )]

0 commit comments

Comments
 (0)