Skip to content

Commit 34bd768

Browse files
committed
add C# 6.0 samples.
1 parent f6a7a0a commit 34bd768

6 files changed

Lines changed: 156 additions & 20 deletions

File tree

TryCSharp.Common/IExecutor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ public interface IExecutor
1010
/// </summary>
1111
/// <param name="target">実行可能なもの</param>
1212
void Execute(IExecutable target);
13+
14+
/// <summary>
15+
/// 非同期実行します。
16+
/// </summary>
17+
/// <param name="target">実行可能なもの</param>
18+
void Execute(IAsyncExecutable target);
1319
}
1420
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using TryCSharp.Common;
2+
3+
namespace TryCSharp.Samples.CSharp6
4+
{
5+
/// <summary>
6+
/// C# 6.0 新機能についてのサンプルです。
7+
/// </summary>
8+
/// <remarks>
9+
/// Auto-Property enhancements (自動プロパティの機能強化) について
10+
/// </remarks>
11+
[Sample]
12+
public class AutoPropSamples01 : IExecutable
13+
{
14+
/// <summary>
15+
/// 自動プロパティのサンプル。初期化付き。
16+
/// </summary>
17+
private string AutoProp1 { get; } = "Hello World";
18+
19+
/// <summary>
20+
/// 自動プロパティのサンプル。初期化付き。
21+
/// </summary>
22+
private string AutoProp2 { get; set; } = "Initial Value";
23+
24+
/// <summary>
25+
/// 処理を実行します。
26+
/// </summary>
27+
public void Execute()
28+
{
29+
// --------------------------------------------------
30+
// C# 6.0 より 自動プロパティの初期化が行えるようになった。
31+
// 以前のバージョンでは、コンストラクタで行っていた部分を
32+
// 宣言時に行える様になっている。
33+
// なので、初期値から変更されることのない値を持つ
34+
// 自動プロパティの場合に、private set; を持つ必要がなく
35+
// get; のみで宣言出来る。
36+
// --------------------------------------------------
37+
Output.WriteLine(AutoProp1);
38+
Output.WriteLine(AutoProp2);
39+
40+
AutoProp2 = "Updated";
41+
Output.WriteLine(AutoProp2);
42+
}
43+
}
44+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using TryCSharp.Common;
4+
5+
namespace TryCSharp.Samples.CSharp6
6+
{
7+
/// <summary>
8+
/// C# 6.0 新機能についてのサンプルです。
9+
/// </summary>
10+
/// <remarks>
11+
/// Expression bodied function members(ラムダ式本体によるメンバーの記述)について
12+
/// </remarks>
13+
[Sample]
14+
public class ExpressionBodySamples01 : IAsyncExecutable
15+
{
16+
/// <summary>
17+
/// 処理を実行します。
18+
/// </summary>
19+
public async Task Execute()
20+
{
21+
// ---------------------------------------------------------
22+
// C# 6.0 より メンバーの記述にてラムダ式を利用できるようになった。
23+
// 注意点として、ステートメントではなく、あくまで「式」なので
24+
// {} が必要なレベルの記述は出来ない。pythonのlambdaと同じ感じ。
25+
// メソッドとプロパティの両方に適用できる。
26+
// ---------------------------------------------------------
27+
var str = "hello world";
28+
29+
Output.WriteLine(this.Upper(str));
30+
await this.AsyncUpper(str);
31+
Output.WriteLine(Now);
32+
}
33+
34+
// プロパティにラムダ式を適用した場合、このプロパティは自動的に getter のみとなる。
35+
private string Now => DateTime.Now.ToString("yyyy/MM/dd");
36+
37+
// メソッドのラムダ式記述
38+
private string Upper(string original) => original.ToUpper();
39+
40+
// asyncなメソッドのラムダ式記述。複数行に渡って書くことも出来るけど
41+
// ここまでするなら、ラムダ式にする意味が正直ない。。。。普通に書いたほうがいい。
42+
private async Task AsyncUpper(string original) => await Task.Run(async () =>
43+
{
44+
await Task.Delay(TimeSpan.FromSeconds(3));
45+
Output.WriteLine(this.Upper(original));
46+
});
47+
}
48+
}

TryCSharp.Samples/TryCSharp.Samples.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
<Compile Include="Commons\Data\Persons.cs" />
145145
<Compile Include="Commons\Extensions\PersonExtensions.cs" />
146146
<Compile Include="Commons\Extensions\StringExtensions.cs" />
147+
<Compile Include="CSharp6\AutoPropSamples01.cs" />
148+
<Compile Include="CSharp6\ExpressionBodySamples01.cs" />
147149
<Compile Include="Dummy.cs" />
148150
<Compile Include="Helpers\CompareResultHelper.cs" />
149151
<Compile Include="IO\GetInvalidPathCharsAndGetInvalidFileNameCharsSamples01.cs" />

TryCSharp.Tools.Cui/CuiAppProcessExecutor.cs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ namespace TryCSharp.Tools.Cui
99
/// </summary>
1010
public class CuiAppProcessExecutor : IExecutor
1111
{
12-
public string StartLogMessage { get; set; }
13-
public string EndLogMessage { get; set; }
12+
/// <summary>
13+
/// 開始ログ
14+
/// </summary>
15+
private string StartLogMessage { get; } = "================== START ==================";
1416

15-
public CuiAppProcessExecutor()
16-
{
17-
StartLogMessage = "================== START ==================";
18-
EndLogMessage = "================== END ==================";
19-
}
17+
/// <summary>
18+
/// 終了ログ
19+
/// </summary>
20+
private string EndLogMessage { get; } = "================== END ==================";
2021

22+
/// <summary>
23+
/// 実行します。
24+
/// </summary>
25+
/// <param name="target">実行可能なもの</param>
2126
public void Execute(IExecutable target)
2227
{
2328
if (target == null)
@@ -27,9 +32,34 @@ public void Execute(IExecutable target)
2732

2833
using (new TimeTracer())
2934
{
30-
Output.WriteLine(StartLogMessage);
35+
Output.WriteLine(this.StartLogMessage);
3136
target.Execute();
32-
Output.WriteLine(EndLogMessage);
37+
Output.WriteLine(this.EndLogMessage);
38+
}
39+
}
40+
41+
/// <summary>
42+
/// 非同期実行します。
43+
/// </summary>
44+
/// <param name="target">実行可能なもの</param>
45+
public void Execute(IAsyncExecutable target)
46+
{
47+
if (target == null)
48+
{
49+
throw new ArgumentNullException(nameof(target));
50+
}
51+
52+
using (new TimeTracer())
53+
{
54+
Output.WriteLine(this.StartLogMessage);
55+
56+
var success = target.Execute().Wait(TimeSpan.FromSeconds(15));
57+
if (!success)
58+
{
59+
Output.WriteLine("time out.....");
60+
}
61+
62+
Output.WriteLine(this.EndLogMessage);
3363
}
3464
}
3565
}
@@ -40,17 +70,13 @@ internal class TimeTracer : IDisposable
4070

4171
public TimeTracer()
4272
{
43-
_watch = Stopwatch.StartNew();
73+
this._watch = Stopwatch.StartNew();
4474
}
4575

46-
#region IDisposable メンバー
47-
4876
public void Dispose()
4977
{
50-
_watch.Stop();
51-
Output.WriteLine("処理時間: {0}", _watch.Elapsed);
78+
this._watch.Stop();
79+
Output.WriteLine("Elapsed Time: {0}", this._watch.Elapsed);
5280
}
53-
54-
#endregion
5581
}
5682
}

TryCSharp.Tools.Cui/Program.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ private static void Main()
6969

7070
if (filtered.Count == 0)
7171
{
72-
Output.WriteLine("指定されたサンプルが見つかりません...[{0}]", userInput);
72+
Output.WriteLine("not found...[{0}]", userInput);
7373
continue;
7474
}
7575

7676
if (!optInfo["fullMatched"])
7777
{
7878
if (filtered.Count > 1)
7979
{
80-
Output.WriteLine("候補が複数存在します。");
80+
Output.WriteLine("There are multiple candidates.");
8181
foreach (var item in filtered)
8282
{
8383
Output.WriteLine("**** {0}", item);
@@ -95,12 +95,22 @@ private static void Main()
9595
continue;
9696
}
9797

98+
// FIXME: 以下の処理がダサい。そのうち直す。
9899
var executor = new CuiAppProcessExecutor();
99-
executor.Execute(clazz as IExecutable);
100+
var target = clazz as IExecutable;
101+
if (target != null)
102+
{
103+
executor.Execute(target);
104+
}
105+
else
106+
{
107+
var asyncTarget = clazz as IAsyncExecutable;
108+
executor.Execute(asyncTarget);
109+
}
100110
}
101111
catch (TypeLoadException)
102112
{
103-
Output.WriteLine("指定されたサンプルが見つかりません...[{0}]", ClassName);
113+
Output.WriteLine("not found...[{0}]", ClassName);
104114
}
105115
catch (Exception ex)
106116
{

0 commit comments

Comments
 (0)