Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ public class Program
{
public static void Main()
{
#region INCLUDE
DateTime dateTime = new DateTime();

Type type = dateTime.GetType();
foreach(
System.Reflection.PropertyInfo property in
type.GetTypeInfo().GetProperties())
type.GetProperties())
{
Console.WriteLine(property.Name);
}
#endregion INCLUDE
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_02
{
using System;
#region INCLUDE
using System.Diagnostics;

#region EXCLUDE
public class Program
{
public static void Main()
{
#endregion EXCLUDE
ThreadPriorityLevel priority;
priority = (ThreadPriorityLevel)Enum.Parse(
typeof(ThreadPriorityLevel), "Idle");
//...
#endregion INCLUDE
}
}
}
15 changes: 10 additions & 5 deletions src/Chapter18/Listing18.03.DynamicallyInvokingAMember.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_03
{
#region INCLUDE
using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -16,7 +17,7 @@ public static void Main(string[] args)
Console.WriteLine(errorMessage);
DisplayHelp();
}
else if(commandLine.Help || string.IsNullOrWhiteSpace(commandLine.Out))
else if (commandLine.Help || string.IsNullOrWhiteSpace(commandLine.Out))
{
DisplayHelp();
}
Expand All @@ -27,14 +28,15 @@ public static void Main(string[] args)
{
// Change thread priority
}
#region EXCLUDE
Console.WriteLine(
@$"Running {
Path.GetFileName(Environment.GetCommandLineArgs()[0])} /Out:{
commandLine.Out} /Priority:{
commandLine.Priority}");

#endregion EXCLUDE
}
// ...
}

private static void DisplayHelp()
Expand Down Expand Up @@ -66,9 +68,9 @@ public class CommandLineHandler
{
public static void Parse(string[] args, object commandLine)
{
if(!TryParse(args, commandLine, out string? errorMessage))
if (!TryParse(args, commandLine, out string? errorMessage))
{
throw new Exception(errorMessage);
throw new InvalidOperationException(errorMessage);
}
}

Expand All @@ -87,6 +89,7 @@ public static bool TryParse(string[] args, object commandLine,

// Remove the slash|dash
option = optionParts[0].Remove(0, 1);
#region HIGHLIGHT
PropertyInfo? property =
commandLine.GetType().GetProperty(option,
BindingFlags.IgnoreCase |
Expand Down Expand Up @@ -122,7 +125,8 @@ public static bool TryParse(string[] args, object commandLine,
null);
success = true;
}
catch(ArgumentException)
#endregion HIGHLIGHT
catch (ArgumentException)
{
success = false;
errorMessage =
Expand Down Expand Up @@ -152,4 +156,5 @@ public static bool TryParse(string[] args, object commandLine,
return success;
}
}
#endregion INCLUDE
}
3 changes: 2 additions & 1 deletion src/Chapter18/Listing18.04.DeclaringTheStackClass.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_04
{
using System;

#region INCLUDE
public class Stack<T>
{
//...
Expand All @@ -13,4 +13,5 @@ public void Add(T i)
}
//...
}
#endregion INCLUDE
}
11 changes: 6 additions & 5 deletions src/Chapter18/Listing18.05.ReflectionWithGenerics.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_05
{
#region INCLUDE
using System;
using System.Reflection;

public class Program
{
public static void Main()
{
Type type;
type = typeof(System.Nullable<>);
Console.WriteLine(type.GetTypeInfo().ContainsGenericParameters);
Console.WriteLine(type.GetTypeInfo().IsGenericType);
Console.WriteLine(type.ContainsGenericParameters);
Console.WriteLine(type.IsGenericType);

type = typeof(System.Nullable<DateTime>);
Console.WriteLine(type.GetTypeInfo().ContainsGenericParameters);
Console.WriteLine(type.GetTypeInfo().IsGenericType);
Console.WriteLine(type.ContainsGenericParameters);
Console.WriteLine(type.IsGenericType);
}
}
#endregion INCLUDE
}
6 changes: 4 additions & 2 deletions src/Chapter18/Listing18.06.UsingReflectionWithGenericTypes.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_06
{
#region INCLUDE
using System;
using System.Collections.Generic;
using System.Reflection;

public class Program
{
Expand All @@ -12,11 +12,13 @@ public static void Main()

Type t = s.GetType();

foreach(Type type in t.GetTypeInfo().GetGenericArguments())
foreach(Type type in t.GetGenericArguments())
{
System.Console.WriteLine(
"Type parameter: " + type.FullName);
}
//...
}
}
#endregion INCLUDE
}
5 changes: 4 additions & 1 deletion src/Chapter18/Listing18.07.DynamicallyInvokingAMember.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_07
{
#region INCLUDE
using System.ComponentModel;

public class Person : INotifyPropertyChanged
{
// TODO: Update listing in Manuscript
public event PropertyChangedEventHandler? PropertyChanged;
public Person(string name)
{
Expand All @@ -22,11 +22,14 @@ public string Name
// Using C# 6.0 conditional null reference
PropertyChanged?.Invoke(
this,
#region HIGHLIGHT
new PropertyChangedEventArgs(
nameof(Name)));
#endregion HIGHLIGHT
}
}
}
// ...
}
#endregion INCLUDE
}
13 changes: 8 additions & 5 deletions src/Chapter18/Listing18.08.DecoratingAPropertyWithAnAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_08
{
using System;

class CommandLineInfo
#region INCLUDE
public class CommandLineInfo
{
#region HIGHLIGHT
[CommandLineSwitchAlias("?")]
#endregion HIGHLIGHT
public bool Help { get; set; }

#region HIGHLIGHT
[CommandLineSwitchRequired]
#endregion HIGHLIGHT
public string? Out { get; set; }

public System.Diagnostics.ProcessPriorityClass Priority
{ get; set; } =
System.Diagnostics.ProcessPriorityClass.Normal;


}
#endregion INCLUDE
// Disabling warning since it is not implemented or shown in manuscript
#pragma warning disable CA1018 // Mark attributes with AttributeUsageAttribute
#pragma warning disable CA1018 // Mark attributes with AttributeUsageAttribute
internal class CommandLineSwitchRequiredAttribute : Attribute
{
//not implemented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_09
{
using System;

class CommandLineInfo
public class CommandLineInfo
{
[CommandLineSwitchAlias("?")]
public bool Help { get; set; }

//Two Ways to do it
//[CommandLineSwitchRequired]
//[CommandLineSwitchAlias("FileName")]
#region INCLUDE
[CommandLineSwitchRequired,
CommandLineSwitchAlias("FileName")]
public string? Out { get; set; }
Expand All @@ -18,9 +19,10 @@ public System.Diagnostics.ProcessPriorityClass Priority
{ get; set; } =
System.Diagnostics.ProcessPriorityClass.Normal;
}
#endregion INCLUDE

// Disabling warning since it is not implemented or shown in manuscript
#pragma warning disable CA1018 // Mark attributes with AttributeUsageAttribute
#pragma warning disable CA1018 // Mark attributes with AttributeUsageAttribute
internal class CommandLineSwitchRequiredAttribute : Attribute
{
//not implimented
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
#region INCLUDE
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -39,3 +40,4 @@
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
#endregion INCLUDE
8 changes: 6 additions & 2 deletions src/Chapter18/Listing18.11.SpecifyingAReturnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_11
{
public class Program
{
#pragma warning disable CA1822 // Mark members as static
#region INCLUDE
[return: Description(
"Returns true if the object is in a valid state.")]
public static bool IsValid()
public bool IsValid()
{
// ...
return true;
}
#endregion INCLUDE
#pragma warning restore CA1822 // Mark members as static
}

public class DescriptionAttribute : Attribute
{
// TODO: Update listing in Manuscript
Expand Down
2 changes: 2 additions & 0 deletions src/Chapter18/Listing18.12.csproj.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#region INCLUDE
<Project>
<PropertyGroup>
<Company>Addison Wesley</Company>
Expand All @@ -6,3 +7,4 @@
<Version>8.0</Version>
</PropertyGroup>
</Project>
#endregion INCLUDE
3 changes: 2 additions & 1 deletion src/Chapter18/Listing18.13.DefiningACustomAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_13
{
using System;

#region INCLUDE
public class CommandLineSwitchRequiredAttribute : Attribute
{
}
#endregion INCLUDE
}
6 changes: 4 additions & 2 deletions src/Chapter18/Listing18.14.RetrievingACustomAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_14
{
#region INCLUDE
using System;
using System.Reflection;
using System.Collections.Generic;
Expand All @@ -19,13 +20,14 @@ public static string[] GetMissingRequiredOptions(
(Attribute[])property.GetCustomAttributes(
typeof(CommandLineSwitchRequiredAttribute),
false);
if((attributes.Length > 0) &&
(property.GetValue(commandLine, null) == null))
if (attributes.Length > 0 &&
property.GetValue(commandLine, null) == null)
{
missingOptions.Add(property.Name);
}
}
return missingOptions.ToArray();
}
}
#endregion INCLUDE
}
11 changes: 8 additions & 3 deletions src/Chapter18/Listing18.15.ProvidingAnAttributeConstructor.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_15
{
using System;

#region INCLUDE
public class CommandLineSwitchAliasAttribute : Attribute
{
#region HIGHLIGHT
public CommandLineSwitchAliasAttribute(string alias)
{
Alias = alias;
}
public string Alias { get; private set; }
#endregion HIGHLIGHT
public string Alias { get; }
}
class CommandLineInfo
public class CommandLineInfo
{
#region HIGHLIGHT
[CommandLineSwitchAlias("?")]
#endregion HIGHLIGHT
public bool Help { get; set; }

// ...
}
#endregion INCLUDE
}
Loading