Skip to content

Commit b068fd3

Browse files
authored
update delegate and events samples (#80)
For dotnet/docs#87 There were two major changes to update these samples. 1. UPgrade from project.json to csproj files. 1. Add snippet tags for including into the main topics.
1 parent c5b8f66 commit b068fd3

File tree

10 files changed

+149
-91
lines changed

10 files changed

+149
-91
lines changed

csharp/delegates-and-events/FileLogger.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,38 @@
33

44
namespace DelegatesAndEvents
55
{
6-
public class FileLogger
7-
{
8-
private readonly string logPath;
9-
public FileLogger(string path)
10-
{
11-
logPath = path;
12-
Logger.WriteMessage += LogMessage;
13-
}
14-
15-
public void DetachLog() => Logger.WriteMessage -= LogMessage;
16-
// make sure this can't throw.
17-
private void LogMessage(string msg)
6+
// <SnippetFileLogger>
7+
public class FileLogger
188
{
19-
try {
20-
using (var log = File.AppendText(logPath))
9+
private readonly string logPath;
10+
public FileLogger(string path)
11+
{
12+
logPath = path;
13+
Logger.WriteMessage += LogMessage;
14+
}
15+
16+
public void DetachLog() => Logger.WriteMessage -= LogMessage;
17+
// make sure this can't throw.
18+
private void LogMessage(string msg)
19+
{
20+
try
2121
{
22-
log.WriteLine(msg);
23-
log.Flush();
22+
using (var log = File.AppendText(logPath))
23+
{
24+
log.WriteLine(msg);
25+
log.Flush();
26+
}
27+
}
28+
catch (Exception)
29+
{
30+
// Hmm. We caught an exception while
31+
// logging. We can't really log the
32+
// problem (since it's the log that's failing).
33+
// So, while normally, catching an exception
34+
// and doing nothing isn't wise, it's really the
35+
// only reasonable option here.
2436
}
25-
} catch (Exception)
26-
{
27-
// Hmm. We caught an exception while
28-
// logging. We can't really log the
29-
// problem (since it's the log that's failing).
30-
// So, while normally, catching an exception
31-
// and doing nothing isn't wise, it's really the
32-
// only reasonable option here.
3337
}
3438
}
39+
// </SnippetFileLogger>
3540
}
36-
}

csharp/delegates-and-events/Logger.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace DelegatesAndEvents
44
{
55

66
// Logger implementation two
7+
// <SnippetSeverity>
78
public enum Severity
89
{
910
Verbose,
@@ -13,7 +14,9 @@ public enum Severity
1314
Error,
1415
Critical
1516
}
17+
// </SnippetSeverity>
1618

19+
// <SnippetLoggerFinal>
1720
public static class Logger
1821
{
1922
public static Action<string> WriteMessage;
@@ -26,11 +29,42 @@ public static void LogMessage(Severity s, string component, string msg)
2629
return;
2730

2831
var outputMsg = $"{DateTime.Now}\t{s}\t{component}\t{msg}";
29-
// Assumes that the WriteMessage delegate must not be null:
3032
WriteMessage(outputMsg);
31-
// Alternative invoke syntax, for when the delegate
32-
// may not have any methods attached:
33-
WriteMessage?.Invoke(outputMsg);
3433
}
3534
}
36-
}
35+
// </SnippetLoggerFinal>
36+
}
37+
38+
namespace ImplementationOne
39+
{
40+
// <SnippetFirstImplementation>
41+
public static class Logger
42+
{
43+
public static Action<string> WriteMessage;
44+
45+
public static void LogMessage(string msg)
46+
{
47+
WriteMessage(msg);
48+
}
49+
}
50+
// </SnippetFirstImplementation>
51+
52+
}
53+
54+
namespace ImplementationTwo
55+
{
56+
using DelegatesAndEvents;
57+
58+
// <SnippetLoggerTwo>
59+
public static class Logger
60+
{
61+
public static Action<string> WriteMessage;
62+
63+
public static void LogMessage(Severity s, string component, string msg)
64+
{
65+
var outputMsg = $"{DateTime.Now}\t{s}\t{component}\t{msg}";
66+
WriteMessage(outputMsg);
67+
}
68+
}
69+
// </SnippetLoggerTwo>
70+
}

csharp/delegates-and-events/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ namespace DelegatesAndEvents
44
{
55
public class Program
66
{
7+
// <SnippetLogToConsole>
78
public static void LogToConsole(string message)
89
{
910
Console.Error.WriteLine(message);
1011
}
12+
// </SnippetLogToConsole>
1113

1214
public static void Main(string[] args)
1315
{
16+
// <SnippetConnectDelegate>
1417
Logger.WriteMessage += LogToConsole;
18+
// </SnippetConnectDelegate>
19+
// <SnippetFileLogger>
1520
var file = new FileLogger("log.txt");
21+
// </SnippetFileLogger>
1622

1723
Logger.LogMessage(Severity.Warning, "Console", "This is a warning message");
1824

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.0</TargetFramework>
5+
<DebugType>portable</DebugType>
6+
<AssemblyName>delegates-and-events</AssemblyName>
7+
<OutputType>Exe</OutputType>
8+
<PackageId>delegates-and-events</PackageId>
9+
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
10+
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
11+
</PropertyGroup>
12+
13+
</Project>

csharp/delegates-and-events/log.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

csharp/delegates-and-events/project.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

csharp/events/Program.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,33 @@ static void Main(string[] args)
1515
var lister = new FileSearcher();
1616
int filesFound = 0;
1717

18+
// <SnippetDeclareEventHandler>
1819
EventHandler<FileFoundArgs> onFileFound = (sender, eventArgs) =>
1920
{
2021
Console.WriteLine(eventArgs.FoundFile);
2122
filesFound++;
22-
//eventArgs.CancelRequested = true;
2323
};
2424

2525
lister.FileFound += onFileFound;
26+
// </SnippetDeclareEventHandler>
2627

28+
// <SnippetSearch>
2729
lister.DirectoryChanged += (sender, eventArgs) =>
2830
{
2931
Console.Write($"Entering '{eventArgs.CurrentSearchDirectory}'.");
3032
Console.WriteLine($" {eventArgs.CompletedDirs} of {eventArgs.TotalDirs} completed...");
3133
};
34+
// </SnippetSearch>
3235

3336
lister.Search(".", "*.dll", true);
3437

38+
// <SnippetRemoveHandler>
3539
lister.FileFound -= onFileFound;
40+
// </SnippetRemoveHandler>
3641
}
3742
}
3843

44+
// <SnippetEventArgs>
3945
public class FileFoundArgs : EventArgs
4046
{
4147
public string FoundFile { get; }
@@ -46,7 +52,9 @@ public FileFoundArgs(string fileName)
4652
FoundFile = fileName;
4753
}
4854
}
55+
// </SnippetEventArg>
4956

57+
// <SnippetSearchDirEventArgs>
5058
internal struct SearchDirectoryArgs
5159
{
5260
internal string CurrentSearchDirectory { get; }
@@ -60,16 +68,23 @@ internal SearchDirectoryArgs(string dir, int totalDirs, int completedDirs)
6068
CompletedDirs = completedDirs;
6169
}
6270
}
71+
// </SnippetSearchDirEventArgs>
72+
6373
public class FileSearcher
6474
{
75+
// <SnippetDeclareEvent>
6576
public event EventHandler<FileFoundArgs> FileFound;
77+
// </SnippetDeclareEvent>
78+
// <SnippetDeclareSearchEvent>
6679
internal event EventHandler<SearchDirectoryArgs> DirectoryChanged
6780
{
6881
add { directoryChanged += value; }
6982
remove { directoryChanged -= value; }
7083
}
7184
private EventHandler<SearchDirectoryArgs> directoryChanged;
85+
// </SnippetDeclareSearchEvent>
7286

87+
// <SnippetFinalImplementation>
7388
public void Search(string directory, string searchPattern, bool searchSubDirs = false)
7489
{
7590
if (searchSubDirs)
@@ -105,5 +120,38 @@ private void SearchDirectory(string directory, string searchPattern)
105120
break;
106121
}
107122
}
123+
// </SnippetFinalImplementation>
124+
}
125+
}
126+
127+
namespace VersionOne
128+
{
129+
// <SnippetEventArgsV1>
130+
public class FileFoundArgs : EventArgs
131+
{
132+
public string FoundFile { get; }
133+
public bool CancelRequested { get; set; }
134+
135+
public FileFoundArgs(string fileName)
136+
{
137+
FoundFile = fileName;
138+
}
108139
}
140+
// </SnippetEventArgV1>
141+
142+
// <SnippetFileSearcherV1>
143+
public class FileSearcher
144+
{
145+
public event EventHandler<FileFoundArgs> FileFound;
146+
147+
public void Search(string directory, string searchPattern)
148+
{
149+
foreach (var file in Directory.EnumerateFiles(directory, searchPattern))
150+
{
151+
FileFound?.Invoke(this, new FileFoundArgs(file));
152+
}
153+
}
154+
}
155+
// </SnippetFileSearcherV1>
156+
109157
}

csharp/events/events.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.0</TargetFramework>
5+
<DebugType>portable</DebugType>
6+
<AssemblyName>events</AssemblyName>
7+
<OutputType>Exe</OutputType>
8+
<PackageId>events</PackageId>
9+
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
10+
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
11+
</PropertyGroup>
12+
13+
</Project>

csharp/events/events.xproj

Lines changed: 0 additions & 18 deletions
This file was deleted.

csharp/events/project.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)