Skip to content

Commit 6bd8500

Browse files
committed
Fix exception message for type releated error of MPO.
1 parent b82627a commit 6bd8500

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/MsgPack/MessagePackObject.Utilities.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ public string AsString( Encoding encoding )
13631363
return null;
13641364
}
13651365

1366-
VerifyUnderlyingType<MessagePackString>( this, null );
1366+
VerifyUnderlyingRawType<string>( this, null );
13671367

13681368
try
13691369
{
@@ -1403,7 +1403,7 @@ public string AsStringUtf8()
14031403
/// </remarks>
14041404
public string AsStringUtf16()
14051405
{
1406-
VerifyUnderlyingType<byte[]>( this, null );
1406+
VerifyUnderlyingRawType<string>( this, null );
14071407
Contract.EndContractBlock();
14081408

14091409
if ( this.IsNil )
@@ -1517,6 +1517,10 @@ public MessagePackObjectDictionary AsDictionary()
15171517

15181518
private static void VerifyUnderlyingType<T>( MessagePackObject instance, string parameterName )
15191519
{
1520+
#if DEBUG
1521+
Contract.Assert( typeof( T ) != typeof( MessagePackString ), "Should use VerifyUnderlyingRawType()" );
1522+
#endif // DEBUG
1523+
15201524
if ( instance.IsNil )
15211525
{
15221526
if ( !typeof( T ).GetIsValueType() || Nullable.GetUnderlyingType( typeof( T ) ) != null )
@@ -1548,6 +1552,24 @@ private static void VerifyUnderlyingType<T>( MessagePackObject instance, string
15481552
}
15491553
}
15501554

1555+
private static void VerifyUnderlyingRawType<T>( MessagePackObject instance, string parameterName )
1556+
{
1557+
if ( instance._handleOrTypeCode == null || instance._handleOrTypeCode is MessagePackString )
1558+
{
1559+
// nil or MPS (eventually string or byte[])
1560+
return;
1561+
}
1562+
1563+
if ( parameterName != null )
1564+
{
1565+
throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, "Do not convert {0} MessagePackObject to {1}.", instance.UnderlyingType, typeof( T ) ), parameterName );
1566+
}
1567+
else
1568+
{
1569+
ThrowInvalidTypeAs<T>( instance );
1570+
}
1571+
}
1572+
15511573
private static void ThrowCannotBeNilAs<T>()
15521574
{
15531575
throw new InvalidOperationException( String.Format( CultureInfo.CurrentCulture, "Do not convert nil MessagePackObject to {0}.", typeof( T ) ) );
@@ -1853,7 +1875,7 @@ public static implicit operator MessagePackObject( MessagePackObject[] value )
18531875
return new MessagePackObject( value, false );
18541876
}
18551877

1856-
#endregion -- Conversion Operator Overloads --
1878+
#endregion -- Conversion Operator Overloads --
18571879

18581880
#if DEBUG
18591881
internal string DebugDump()

src/MsgPack/MessagePackObject.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ public Double AsDouble()
774774
/// <returns><see cref="String" /> instance corresponds to this instance.</returns>
775775
public String AsString()
776776
{
777-
VerifyUnderlyingType<MessagePackString>( this, null );
777+
VerifyUnderlyingRawType<string>( this, null );
778778

779779
if( this._handleOrTypeCode == null )
780780
{
@@ -793,7 +793,7 @@ public String AsString()
793793
/// <returns><see cref="Byte" />[] instance corresponds to this instance.</returns>
794794
public Byte[] AsBinary()
795795
{
796-
VerifyUnderlyingType<MessagePackString>( this, null );
796+
VerifyUnderlyingRawType<byte[]>( this, null );
797797

798798
if( this._handleOrTypeCode == null )
799799
{
@@ -1603,7 +1603,7 @@ public static explicit operator Double( MessagePackObject value )
16031603
/// <returns><see cref="String" /> instance corresponds to <paramref name="value"/>.</returns>
16041604
public static explicit operator String( MessagePackObject value )
16051605
{
1606-
VerifyUnderlyingType<MessagePackString>( value, "value" );
1606+
VerifyUnderlyingRawType<string>( value, "value" );
16071607

16081608
if( value._handleOrTypeCode == null )
16091609
{
@@ -1623,7 +1623,7 @@ public static explicit operator String( MessagePackObject value )
16231623
/// <returns><see cref="Byte" />[] instance corresponds to <paramref name="value"/>.</returns>
16241624
public static explicit operator Byte[]( MessagePackObject value )
16251625
{
1626-
VerifyUnderlyingType<MessagePackString>( value, "value" );
1626+
VerifyUnderlyingRawType<byte[]>( value, "value" );
16271627

16281628
if( value._handleOrTypeCode == null )
16291629
{

src/MsgPack/MessagePackObject.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ private void GenerateAsT( string val, Object typeOrTypeName, bool passParameterN
408408
else if ( t == typeof( byte[] ) )
409409
{
410410
#>
411-
VerifyUnderlyingType<MessagePackString>( <#= val #>, <#= passParameterName ? "\"" + val + "\"" : "null" #> );
411+
VerifyUnderlyingRawType<byte[]>( <#= val #>, <#= passParameterName ? "\"" + val + "\"" : "null" #> );
412412

413413
if( <#= val #>._handleOrTypeCode == null )
414414
{
@@ -424,7 +424,7 @@ private void GenerateAsT( string val, Object typeOrTypeName, bool passParameterN
424424
else if ( t == typeof( string ) )
425425
{
426426
#>
427-
VerifyUnderlyingType<MessagePackString>( <#= val #>, <#= passParameterName ? "\"" + val + "\"" : "null" #> );
427+
VerifyUnderlyingRawType<string>( <#= val #>, <#= passParameterName ? "\"" + val + "\"" : "null" #> );
428428

429429
if( <#= val #>._handleOrTypeCode == null )
430430
{

0 commit comments

Comments
 (0)