Skip to content

Commit d10f7eb

Browse files
committed
Add solution code for Ch7
1 parent 20709d7 commit d10f7eb

20 files changed

+449
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<PublishAot>true</PublishAot>
9+
<InvariantGlobalization>true</InvariantGlobalization>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<Using Include="System.Console" Static="true" />
14+
</ItemGroup>
15+
16+
</Project>

code/Chapter07/AotConsole/Program.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Globalization; // To use CultureInfo.
2+
using System.Reflection; // To use AssemblyName.
3+
using System.Reflection.Emit; // To use AssemblyBuilder.
4+
5+
WriteLine("This is an ahead-of-time (AOT) compiled console app.");
6+
WriteLine("Current culture: {0}", CultureInfo.CurrentCulture.DisplayName);
7+
WriteLine("OS version: {0}", Environment.OSVersion);
8+
9+
// We cannot use this method in an AOT project.
10+
//AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(
11+
// new AssemblyName("MyAssembly"), AssemblyBuilderAccess.Run);
12+
13+
Write("Press any key to exit.");
14+
ReadKey(intercept: true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Using Include="System.Console" Static="true" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<Using Remove="System" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
20+
<PackageReference Include="Packt.CSdotnet.SharedLibrary" Version="8.0.0" />
21+
</ItemGroup>
22+
23+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Xml.Linq; // To use XDocument.
2+
using System; // To use String.
3+
using Packt.Shared;
4+
5+
XDocument doc = new();
6+
7+
#region Relating C# keywords to .NET types
8+
9+
string s1 = "Hello";
10+
String s2 = "World";
11+
WriteLine($"{s1} {s2}");
12+
13+
#endregion
14+
15+
#region Understanding native-sized integers
16+
17+
WriteLine($"Environment.Is64BitProcess = {Environment.Is64BitProcess}");
18+
WriteLine($"int.MaxValue = {int.MaxValue:N0}");
19+
WriteLine($"nint.MaxValue = {nint.MaxValue:N0}");
20+
21+
#endregion
22+
23+
#region Testing your class library package
24+
25+
Write("Enter a color value in hex: ");
26+
string? hex = ReadLine();
27+
WriteLine("Is {0} a valid color value? {1}",
28+
arg0: hex, arg1: hex.IsValidHex());
29+
30+
Write("Enter a XML element: ");
31+
string? xmlTag = ReadLine();
32+
WriteLine("Is {0} a valid XML element? {1}",
33+
arg0: xmlTag, arg1: xmlTag.IsValidXmlTag());
34+
35+
Write("Enter a password: ");
36+
string? password = ReadLine();
37+
WriteLine("Is {0} a valid password? {1}",
38+
arg0: password, arg1: password.IsValidPassword());
39+
40+
#endregion

code/Chapter07/Chapter07.sln

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33815.320
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssembliesAndNamespaces", "AssembliesAndNamespaces\AssembliesAndNamespaces.csproj", "{7202C52C-2C4F-45F7-8F19-63C86F270E9B}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharedLibrary", "SharedLibrary\SharedLibrary.csproj", "{303B4F18-C8FF-4F42-A5F5-0BDE27C6306C}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetEverywhere", "DotNetEverywhere\DotNetEverywhere.csproj", "{6F7C8D21-834D-4108-9F48-51F4176FC793}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Packt.SourceGeneration", "Packt.SourceGeneration\Packt.SourceGeneration.csproj", "{0A419022-3D0B-4F90-ABCB-1F14E28EE6C4}"
13+
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp.SourceGeneration", "ConsoleApp.SourceGeneration\ConsoleApp.SourceGeneration.csproj", "{B7AAD812-1EDA-4827-AECC-13F12C58C385}"
15+
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlSDK", "ControlSDK\ControlSDK.csproj", "{A8B0D555-C3A8-47C2-B3DC-56F118B6A849}"
17+
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AotConsole", "AotConsole\AotConsole.csproj", "{8DD7C69F-BB04-476C-84F7-1597477B6047}"
19+
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceLinks", "SourceLinks\SourceLinks.csproj", "{6E93C8EE-1F5D-4CF5-9B9C-5D2D1A5B363C}"
21+
EndProject
22+
Global
23+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
24+
Debug|Any CPU = Debug|Any CPU
25+
Release|Any CPU = Release|Any CPU
26+
EndGlobalSection
27+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
28+
{7202C52C-2C4F-45F7-8F19-63C86F270E9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{7202C52C-2C4F-45F7-8F19-63C86F270E9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{7202C52C-2C4F-45F7-8F19-63C86F270E9B}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{7202C52C-2C4F-45F7-8F19-63C86F270E9B}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{303B4F18-C8FF-4F42-A5F5-0BDE27C6306C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{303B4F18-C8FF-4F42-A5F5-0BDE27C6306C}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{303B4F18-C8FF-4F42-A5F5-0BDE27C6306C}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{303B4F18-C8FF-4F42-A5F5-0BDE27C6306C}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{6F7C8D21-834D-4108-9F48-51F4176FC793}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{6F7C8D21-834D-4108-9F48-51F4176FC793}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{6F7C8D21-834D-4108-9F48-51F4176FC793}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{6F7C8D21-834D-4108-9F48-51F4176FC793}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{0A419022-3D0B-4F90-ABCB-1F14E28EE6C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{0A419022-3D0B-4F90-ABCB-1F14E28EE6C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{0A419022-3D0B-4F90-ABCB-1F14E28EE6C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{0A419022-3D0B-4F90-ABCB-1F14E28EE6C4}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{B7AAD812-1EDA-4827-AECC-13F12C58C385}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{B7AAD812-1EDA-4827-AECC-13F12C58C385}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{B7AAD812-1EDA-4827-AECC-13F12C58C385}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{B7AAD812-1EDA-4827-AECC-13F12C58C385}.Release|Any CPU.Build.0 = Release|Any CPU
48+
{A8B0D555-C3A8-47C2-B3DC-56F118B6A849}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49+
{A8B0D555-C3A8-47C2-B3DC-56F118B6A849}.Debug|Any CPU.Build.0 = Debug|Any CPU
50+
{A8B0D555-C3A8-47C2-B3DC-56F118B6A849}.Release|Any CPU.ActiveCfg = Release|Any CPU
51+
{A8B0D555-C3A8-47C2-B3DC-56F118B6A849}.Release|Any CPU.Build.0 = Release|Any CPU
52+
{8DD7C69F-BB04-476C-84F7-1597477B6047}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
53+
{8DD7C69F-BB04-476C-84F7-1597477B6047}.Debug|Any CPU.Build.0 = Debug|Any CPU
54+
{8DD7C69F-BB04-476C-84F7-1597477B6047}.Release|Any CPU.ActiveCfg = Release|Any CPU
55+
{8DD7C69F-BB04-476C-84F7-1597477B6047}.Release|Any CPU.Build.0 = Release|Any CPU
56+
{6E93C8EE-1F5D-4CF5-9B9C-5D2D1A5B363C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{6E93C8EE-1F5D-4CF5-9B9C-5D2D1A5B363C}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{6E93C8EE-1F5D-4CF5-9B9C-5D2D1A5B363C}.Release|Any CPU.ActiveCfg = Release|Any CPU
59+
{6E93C8EE-1F5D-4CF5-9B9C-5D2D1A5B363C}.Release|Any CPU.Build.0 = Release|Any CPU
60+
EndGlobalSection
61+
GlobalSection(SolutionProperties) = preSolution
62+
HideSolutionNode = FALSE
63+
EndGlobalSection
64+
GlobalSection(ExtensibilityGlobals) = postSolution
65+
SolutionGuid = {C1C10A6F-4DF6-4569-A295-67F2B0936B71}
66+
EndGlobalSection
67+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
9+
<!--For use with Visual Studio Code and command-line tools-->
10+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<Using Include="System.Console" Static="true" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include=
19+
"..\Packt.SourceGeneration\Packt.SourceGeneration.csproj"
20+
OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
21+
</ItemGroup>
22+
23+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//ConfigureConsole();
2+
ConfigureConsole(culture: "fr-FR");
3+
//ConfigureConsole(useComputerCulture: true);
4+
5+
decimal price = 19.99M;
6+
DateTimeOffset today = DateTimeOffset.Now;
7+
8+
WriteLine($"Today, {today:D}, the price is {price:C}.");
9+

code/Chapter07/ControlSDK/Class1.cs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace ControlSDK;
2+
public class Class1
3+
{
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

code/Chapter07/ControlSDK/global.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "6.0.314"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<RuntimeIdentifiers>
9+
win10-x64;osx-x64;osx.11.0-arm64;linux-x64;linux-arm64
10+
</RuntimeIdentifiers>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<Using Include="System.Console" Static="true" />
15+
</ItemGroup>
16+
17+
</Project>
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
WriteLine("I can run everywhere!");
2+
WriteLine($"OS Version is {Environment.OSVersion}.");
3+
4+
if (OperatingSystem.IsMacOS())
5+
{
6+
WriteLine("I am macOS.");
7+
}
8+
else if (OperatingSystem.IsWindowsVersionAtLeast(major: 10, build: 22000))
9+
{
10+
WriteLine("I am Windows 11.");
11+
}
12+
else if (OperatingSystem.IsWindowsVersionAtLeast(major: 10))
13+
{
14+
WriteLine("I am Windows 10.");
15+
}
16+
else
17+
{
18+
WriteLine("I am some other mysterious OS.");
19+
}
20+
WriteLine("Press any key to stop me.");
21+
ReadKey(intercept: true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
6+
<!--Must use C# 10 or later to support 'global using' statements.-->
7+
<LangVersion>preview</LangVersion>
8+
9+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
10+
<Nullable>enable</Nullable>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<Using Include="System.Console" Static="true" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers"
19+
Version="3.3.4">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>
22+
runtime; build; native; contentfiles; analyzers;
23+
buildtransitive
24+
</IncludeAssets>
25+
</PackageReference>
26+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp"
27+
Version="4.6.0" />
28+
</ItemGroup>
29+
30+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// To use [Generator], ISourceGenerator, and so on.
2+
using Microsoft.CodeAnalysis;
3+
4+
namespace Packt.Shared;
5+
6+
[Generator]
7+
public class ProgramSourceGenerator : ISourceGenerator
8+
{
9+
public void Execute(GeneratorExecutionContext execContext)
10+
{
11+
IMethodSymbol? mainMethod = execContext.Compilation
12+
.GetEntryPoint(execContext.CancellationToken);
13+
14+
string sourceCode = $@"// Source-generated class.
15+
#nullable enable
16+
17+
using System.Globalization; // To use CultureInfo.
18+
19+
partial class {mainMethod?.ContainingType.Name}
20+
{{
21+
private static CultureInfo? _computerCulture = null;
22+
23+
public static void ConfigureConsole(
24+
string culture = ""en-US"",
25+
bool useComputerCulture = false,
26+
bool showCulture = true)
27+
{{
28+
// Store the original computer culture so we can reset it later.
29+
if (_computerCulture is null)
30+
{{
31+
_computerCulture = CultureInfo.CurrentCulture;
32+
}}
33+
34+
// Enable special characters like Euro currency symbol.
35+
OutputEncoding = System.Text.Encoding.UTF8;
36+
37+
if (useComputerCulture)
38+
{{
39+
CultureInfo.CurrentCulture = _computerCulture;
40+
}}
41+
else
42+
{{
43+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
44+
}}
45+
46+
if (showCulture)
47+
{{
48+
WriteLine($""Current culture: {{CultureInfo.CurrentCulture.DisplayName}}."");
49+
}}
50+
}}
51+
}}
52+
";
53+
54+
string fileName = $"{mainMethod?.ContainingType.Name}.Methods.g.cs";
55+
execContext.AddSource(fileName, sourceCode);
56+
}
57+
58+
public void Initialize(GeneratorInitializationContext initContext)
59+
{
60+
// This source generator does not need any initialization.
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>preview</LangVersion>
6+
7+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8+
<PackageId>Packt.CSdotnet.SharedLibrary</PackageId>
9+
<PackageVersion>8.0.0.0</PackageVersion>
10+
<Title>C# 12 and .NET 8 Shared Library</Title>
11+
<Authors>Mark J Price</Authors>
12+
<PackageLicenseExpression>
13+
MS-PL
14+
</PackageLicenseExpression>
15+
<PackageProjectUrl>
16+
https://github.com/markjprice/cs12dotnet8
17+
</PackageProjectUrl>
18+
<PackageReadmeFile>readme.md</PackageReadmeFile>
19+
<PackageIcon>packt-csdotnet-sharedlibrary.png</PackageIcon>
20+
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
21+
<PackageReleaseNotes>
22+
Example shared library packaged for NuGet.
23+
</PackageReleaseNotes>
24+
<Description>
25+
Three extension methods to validate a string value.
26+
</Description>
27+
<Copyright>
28+
Copyright © 2016-2023 Packt Publishing Limited
29+
</Copyright>
30+
<PackageTags>string extensions packt csharp dotnet</PackageTags>
31+
</PropertyGroup>
32+
33+
<ItemGroup>
34+
<None Include="packt-csdotnet-sharedlibrary.png"
35+
PackagePath="\" Pack="true" />
36+
<None Include="readme.md"
37+
PackagePath="\" Pack="true" />
38+
</ItemGroup>
39+
40+
</Project>

0 commit comments

Comments
 (0)