Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
ad04b042
Commit
ad04b042
authored
Oct 03, 2014
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement IConvertible
parent
dd9e9c8d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
143 additions
and
1 deletion
+143
-1
KeysAndValues.cs
StackExchange.Redis.Tests/KeysAndValues.cs
+35
-0
RedisValue.cs
StackExchange.Redis/StackExchange/Redis/RedisValue.cs
+108
-1
No files found.
StackExchange.Redis.Tests/KeysAndValues.cs
View file @
ad04b042
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
using
System.Text
;
using
System.Text
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
NUnit.Framework
;
using
NUnit.Framework
;
using
System.Globalization
;
namespace
StackExchange.Redis.Tests
namespace
StackExchange.Redis.Tests
{
{
...
@@ -113,5 +114,39 @@ private void CheckNull(RedisValue value)
...
@@ -113,5 +114,39 @@ private void CheckNull(RedisValue value)
CheckSame
(
value
,
(
string
)
null
);
CheckSame
(
value
,
(
string
)
null
);
CheckSame
(
value
,
(
byte
[])
null
);
CheckSame
(
value
,
(
byte
[])
null
);
}
}
[
Test
]
public
void
ValuesAreConvertible
()
{
RedisValue
val
=
123
;
object
o
=
val
;
byte
[]
blob
=
(
byte
[])
Convert
.
ChangeType
(
o
,
typeof
(
byte
[]));
Assert
.
AreEqual
(
3
,
blob
.
Length
);
Assert
.
AreEqual
((
byte
)
'1'
,
blob
[
0
]);
Assert
.
AreEqual
((
byte
)
'2'
,
blob
[
1
]);
Assert
.
AreEqual
((
byte
)
'3'
,
blob
[
2
]);
Assert
.
AreEqual
((
double
)
123
,
Convert
.
ToDouble
(
o
));
IConvertible
c
=
(
IConvertible
)
o
;
Assert
.
AreEqual
((
short
)
123
,
c
.
ToInt16
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
((
int
)
123
,
c
.
ToInt32
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
((
long
)
123
,
c
.
ToInt64
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
((
float
)
123
,
c
.
ToSingle
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
(
"123"
,
c
.
ToString
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
((
double
)
123
,
c
.
ToDouble
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
((
decimal
)
123
,
c
.
ToDecimal
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
((
ushort
)
123
,
c
.
ToUInt16
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
((
uint
)
123
,
c
.
ToUInt32
(
CultureInfo
.
InvariantCulture
));
Assert
.
AreEqual
((
ulong
)
123
,
c
.
ToUInt64
(
CultureInfo
.
InvariantCulture
));
blob
=
(
byte
[])
c
.
ToType
(
typeof
(
byte
[]),
CultureInfo
.
InvariantCulture
);
Assert
.
AreEqual
(
3
,
blob
.
Length
);
Assert
.
AreEqual
((
byte
)
'1'
,
blob
[
0
]);
Assert
.
AreEqual
((
byte
)
'2'
,
blob
[
1
]);
Assert
.
AreEqual
((
byte
)
'3'
,
blob
[
2
]);
}
}
}
}
}
StackExchange.Redis/StackExchange/Redis/RedisValue.cs
View file @
ad04b042
...
@@ -6,7 +6,7 @@ namespace StackExchange.Redis
...
@@ -6,7 +6,7 @@ namespace StackExchange.Redis
/// <summary>
/// <summary>
/// Represents values that can be stored in redis
/// Represents values that can be stored in redis
/// </summary>
/// </summary>
public
struct
RedisValue
:
IEquatable
<
RedisValue
>,
IComparable
<
RedisValue
>,
IComparable
public
struct
RedisValue
:
IEquatable
<
RedisValue
>,
IComparable
<
RedisValue
>,
IComparable
,
IConvertible
{
{
internal
static
readonly
RedisValue
[]
EmptyArray
=
new
RedisValue
[
0
];
internal
static
readonly
RedisValue
[]
EmptyArray
=
new
RedisValue
[
0
];
...
@@ -530,5 +530,112 @@ static bool TryParseDouble(byte[] blob, out double value)
...
@@ -530,5 +530,112 @@ static bool TryParseDouble(byte[] blob, out double value)
}
}
return
valueBlob
;
return
valueBlob
;
}
}
TypeCode
IConvertible
.
GetTypeCode
()
{
return
TypeCode
.
Object
;
}
bool
IConvertible
.
ToBoolean
(
IFormatProvider
provider
)
{
return
(
bool
)
this
;
}
byte
IConvertible
.
ToByte
(
IFormatProvider
provider
)
{
return
(
byte
)
this
;
}
char
IConvertible
.
ToChar
(
IFormatProvider
provider
)
{
return
(
char
)
this
;
}
DateTime
IConvertible
.
ToDateTime
(
IFormatProvider
provider
)
{
return
DateTime
.
Parse
((
string
)
this
,
provider
);
}
decimal
IConvertible
.
ToDecimal
(
IFormatProvider
provider
)
{
return
(
decimal
)
this
;
}
double
IConvertible
.
ToDouble
(
IFormatProvider
provider
)
{
return
(
double
)
this
;
}
short
IConvertible
.
ToInt16
(
IFormatProvider
provider
)
{
return
(
short
)
this
;
}
int
IConvertible
.
ToInt32
(
IFormatProvider
provider
)
{
return
(
int
)
this
;
}
long
IConvertible
.
ToInt64
(
IFormatProvider
provider
)
{
return
(
long
)
this
;
}
sbyte
IConvertible
.
ToSByte
(
IFormatProvider
provider
)
{
return
(
sbyte
)
this
;
}
float
IConvertible
.
ToSingle
(
IFormatProvider
provider
)
{
return
(
float
)
this
;
}
string
IConvertible
.
ToString
(
IFormatProvider
provider
)
{
return
(
string
)
this
;
}
object
IConvertible
.
ToType
(
Type
conversionType
,
IFormatProvider
provider
)
{
if
(
conversionType
==
null
)
throw
new
ArgumentNullException
(
"conversionType"
);
if
(
conversionType
==
typeof
(
byte
[]))
return
(
byte
[])
this
;
if
(
conversionType
==
typeof
(
RedisValue
))
return
this
;
switch
(
Type
.
GetTypeCode
(
conversionType
))
{
case
TypeCode
.
Boolean
:
return
(
bool
)
this
;
case
TypeCode
.
Byte
:
return
(
byte
)
this
;
case
TypeCode
.
Char
:
return
(
char
)
this
;
case
TypeCode
.
DateTime
:
return
DateTime
.
Parse
((
string
)
this
,
provider
);
case
TypeCode
.
Decimal
:
return
(
decimal
)
this
;
case
TypeCode
.
Double
:
return
(
double
)
this
;
case
TypeCode
.
Int16
:
return
(
short
)
this
;
case
TypeCode
.
Int32
:
return
(
int
)
this
;
case
TypeCode
.
Int64
:
return
(
long
)
this
;
case
TypeCode
.
SByte
:
return
(
sbyte
)
this
;
case
TypeCode
.
Single
:
return
(
float
)
this
;
case
TypeCode
.
String
:
return
(
string
)
this
;
case
TypeCode
.
UInt16
:
return
(
ushort
)
this
;
case
TypeCode
.
UInt32
:
return
(
uint
)
this
;
case
TypeCode
.
UInt64
:
return
(
long
)
this
;
default
:
throw
new
NotSupportedException
();
}
}
ushort
IConvertible
.
ToUInt16
(
IFormatProvider
provider
)
{
return
(
ushort
)
this
;
}
uint
IConvertible
.
ToUInt32
(
IFormatProvider
provider
)
{
return
(
uint
)
this
;
}
ulong
IConvertible
.
ToUInt64
(
IFormatProvider
provider
)
{
return
(
ulong
)
this
;
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment