Skip to content

Commit cbef527

Browse files
authored
Merge pull request #12 from jacqueskang/bugfix-enum-conversion
support enum type in service contract
2 parents d3385a1 + 97b59ee commit cbef527

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

src/IpcServiceSample.ConsoleClient/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ private static async Task MainAsync(string[] args)
4040
// test 4: call IPC service method without parameter or return
4141
await client.InvokeAsync(x => x.DoNothing());
4242
Console.WriteLine($"invoked DoNothing()");
43+
44+
// test 5: call IPC service method with enum parameter
45+
string text = await client.InvokeAsync(x => x.ConvertText("hEllO woRd!", TextStyle.Upper));
46+
Console.WriteLine(text);
4347
}
4448
catch (Exception ex)
4549
{

src/IpcServiceSample.ConsoleServer/ComputingService.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Globalization;
23
using IpcServiceSample.ServiceContracts;
34
using Microsoft.Extensions.Logging;
45

@@ -36,6 +37,19 @@ public float AddFloat(float x, float y)
3637
return x + y;
3738
}
3839

40+
public string ConvertText(string text, TextStyle style)
41+
{
42+
switch (style)
43+
{
44+
case TextStyle.TitleCase:
45+
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(text);
46+
case TextStyle.Upper:
47+
return CultureInfo.InvariantCulture.TextInfo.ToUpper(text);
48+
default:
49+
return text;
50+
}
51+
}
52+
3953
public void DoNothing()
4054
{ }
4155
}

src/IpcServiceSample.ServiceContracts/IComputingService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IComputingService
88
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
99
ComplexNumber AddComplexNumbers(IEnumerable<ComplexNumber> numbers);
1010
void DoNothing();
11+
string ConvertText(string text, TextStyle style);
1112
}
1213

1314
public class ComplexNumber
@@ -21,4 +22,10 @@ public ComplexNumber(float a, float b)
2122
B = b;
2223
}
2324
}
25+
26+
public enum TextStyle
27+
{
28+
TitleCase,
29+
Upper
30+
}
2431
}

src/JKang.IpcServiceFramework.Core.Tests/DefaultValueConverterTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ public void TryConvert_DerivedTypeToBaseType()
140140
Assert.IsInstanceOfType(actual, typeof(ComplexType));
141141
}
142142

143+
[TestMethod]
144+
public void TryConvert_StringToEnum()
145+
{
146+
EnumType expected = EnumType.SecondOption;
147+
148+
bool succeed = _sut.TryConvert(expected.ToString(), typeof(EnumType), out object actual);
149+
150+
Assert.IsTrue(succeed);
151+
Assert.IsInstanceOfType(actual, typeof(EnumType));
152+
Assert.AreEqual(expected, actual);
153+
}
154+
155+
[TestMethod]
156+
public void TryConvert_Int32ToEnum()
157+
{
158+
EnumType expected = EnumType.SecondOption;
159+
160+
bool succeed = _sut.TryConvert((int)expected, typeof(EnumType), out object actual);
161+
162+
Assert.IsTrue(succeed);
163+
Assert.IsInstanceOfType(actual, typeof(EnumType));
164+
Assert.AreEqual(expected, actual);
165+
}
166+
143167
interface IComplexType
144168
{
145169
int Int32Value { get; }
@@ -151,5 +175,11 @@ class ComplexType : IComplexType
151175
public int Int32Value { get; set; }
152176
public string StringValue { get; set; }
153177
}
178+
179+
enum EnumType
180+
{
181+
FirstOption,
182+
SecondOption
183+
}
154184
}
155185
}

src/JKang.IpcServiceFramework.Core/Services/DefaultValueConverter.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ public bool TryConvert(object origValue, Type destType, out object destValue)
1414
return true;
1515
}
1616

17+
if (destType.IsEnum)
18+
{
19+
if (origValue is string str)
20+
{
21+
try
22+
{
23+
destValue = Enum.Parse(destType, str, ignoreCase: true);
24+
return true;
25+
}
26+
catch
27+
{ }
28+
}
29+
else
30+
{
31+
try
32+
{
33+
destValue = Enum.ToObject(destType, origValue);
34+
return true;
35+
}
36+
catch
37+
{ }
38+
}
39+
}
40+
1741
if (origValue is JObject jObj)
1842
{
1943
// rely on JSON.Net to convert complexe type

0 commit comments

Comments
 (0)