ch. 18 code changes
MODIFY
src/Chapter18/Listing18.01.UsingTypeGetPropertiesToObtainAnObjectsPublicProperties.cs
src/Chapter18/Listing18.01.UsingTypeGetPropertiesToObtainAnObjectsPublicProperties.cs
@@ -13,7 +13,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_01
Type type = dateTime.GetType();
foreach(
System.Reflection.PropertyInfo property in
- type.GetTypeInfo().GetProperties())
+ type.GetProperties())
{
Console.WriteLine(property.Name);
}

MODIFY
src/Chapter18/Listing18.03.DynamicallyInvokingAMember.cs
src/Chapter18/Listing18.03.DynamicallyInvokingAMember.cs
@@ -35,9 +35,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_03
commandLine.Out} /Priority:{
commandLine.Priority}");
- }
#endregion EXCLUDE
}
+ }
private static void DisplayHelp()
{
@@ -70,7 +70,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_03
{
if (!TryParse(args, commandLine, out string? errorMessage))
{
- throw new Exception(errorMessage);
+ throw new InvalidOperationException(errorMessage);
}
}

MODIFY
src/Chapter18/Listing18.05.ReflectionWithGenerics.cs
src/Chapter18/Listing18.05.ReflectionWithGenerics.cs
@@ -2,7 +2,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_05
{
#region INCLUDE
using System;
- using System.Reflection;
public class Program
{
@@ -10,12 +9,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_05
{
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

MODIFY
src/Chapter18/Listing18.06.UsingReflectionWithGenericTypes.cs
src/Chapter18/Listing18.06.UsingReflectionWithGenericTypes.cs
@@ -3,7 +3,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_06
#region INCLUDE
using System;
using System.Collections.Generic;
- using System.Reflection;
public class Program
{
@@ -13,7 +12,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_06
Type t = s.GetType();
- foreach(Type type in t.GetTypeInfo().GetGenericArguments())
+ foreach(Type type in t.GetGenericArguments())
{
System.Console.WriteLine(
"Type parameter: " + type.FullName);

MODIFY
src/Chapter18/Listing18.11.SpecifyingAReturnAttribute.cs
src/Chapter18/Listing18.11.SpecifyingAReturnAttribute.cs
@@ -4,15 +4,17 @@ 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

MODIFY
src/Chapter18/Listing18.14.RetrievingACustomAttribute.cs
src/Chapter18/Listing18.14.RetrievingACustomAttribute.cs
@@ -20,8 +20,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_14
(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);
}

MODIFY
src/Chapter18/Listing18.15.ProvidingAnAttributeConstructor.cs
src/Chapter18/Listing18.15.ProvidingAnAttributeConstructor.cs
@@ -10,7 +10,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_15
Alias = alias;
}
#endregion HIGHLIGHT
- public string Alias { get; private set; }
+ public string Alias { get; }
}
class CommandLineInfo
{

MODIFY
src/Chapter18/Listing18.16.RetrievingASpecificAttributeAndCheckingItsInitialization.cs
src/Chapter18/Listing18.16.RetrievingASpecificAttributeAndCheckingItsInitialization.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_16
{
+ using AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_15;
using System;
using System.Reflection;
- using System.Linq;
- using AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_15;
public class Program
{
@@ -12,11 +11,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_16
#region INCLUDE
PropertyInfo property =
typeof(CommandLineInfo).GetProperty("Help")!;
- CommandLineSwitchAliasAttribute attribute =
- (CommandLineSwitchAliasAttribute)
- property.GetCustomAttributes(
- typeof(CommandLineSwitchAliasAttribute), false).ElementAt(0);
- if(attribute.Alias == "?")
+ CommandLineSwitchAliasAttribute? attribute =
+ (CommandLineSwitchAliasAttribute?)
+ property.GetCustomAttribute(
+ typeof(CommandLineSwitchAliasAttribute), false);
+ if(attribute?.Alias == "?")
{
Console.WriteLine("Help(?)");
};

MODIFY
src/Chapter18/Listing18.18.UpdatingCommandLineHandler.TryParseToHandleAliases.cs
src/Chapter18/Listing18.18.UpdatingCommandLineHandler.TryParseToHandleAliases.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_18
{
+ using Listing18_17;
#region INCLUDE
using System;
using System.Reflection;
using System.Collections.Generic;
- using Listing18_17;
+ using System.Diagnostics;
public class CommandLineHandler
{
@@ -66,7 +67,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_18
else
{
- if((optionParts.Length < 2)
+ if (optionParts.Length < 2
|| optionParts[1] == "")
{
// No setting was provided for the switch.
@@ -81,7 +82,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_18
commandLine, optionParts[1], null);
success = true;
}
- else if(property.PropertyType.GetTypeInfo().IsEnum)
+ else if(
+ //property.PropertyType.IsEnum also available
+ property.PropertyType ==
+ typeof(ProcessPriorityClass))
{
success = TryParseEnumSwitch(
commandLine, optionParts,
@@ -90,10 +94,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_18
else
{
success = false;
- errorMessage = string.Format(
- "Data type '{0}' on {1} is not supported.",
- property.PropertyType.ToString(),
- commandLine.GetType().ToString());
+ errorMessage =
+ $@"Data type '{ property.PropertyType.ToString() }' on {
+ commandLine.GetType().ToString() } is not supported.";
}
}
return success;

MODIFY
src/Chapter18/Listing18.20.AttributeUsageAttributeRestrictingWhereToApplyAnAttribute.cs
src/Chapter18/Listing18.20.AttributeUsageAttributeRestrictingWhereToApplyAnAttribute.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_20
{
+ //will not compile if uncommented
+ /*
#region INCLUDE
// ERROR: The attribute usage is restricted to properties
- //[CommandLineSwitchAlias("?")] //will not compile if uncommented
+ [CommandLineSwitchAlias("?")]
class CommandLineInfo
{
}
#endregion INCLUDE
+ */
}

MODIFY
src/Chapter18/Listing18.23.UsingFlagsAttribute.cs
src/Chapter18/Listing18.23.UsingFlagsAttribute.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_23
{
+ /*
+ #region INCLUDE
+ [Flags]
+ public enun FileAttributes
+ {
+ ReadOnly = 0x0001,
+ Hidden = 0x0002,
+ // ...
+ }
+ #endregion INCLUDE
+ */
+
+ #region INCLUDE
using System;
using System.IO;
@@ -27,4 +40,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_23
// ...
}
}
+ #endregion INCLUDE
}

MODIFY
src/Chapter18/Listing18.24.UsingConditionalAttributeToEliminateACall.cs
src/Chapter18/Listing18.24.UsingConditionalAttributeToEliminateACall.cs
@@ -18,13 +18,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_24
}
[Conditional("CONDITION_A")]
- static void MethodA()
+ public static void MethodA()
{
Console.WriteLine("MethodA() executing...");
}
[Conditional("CONDITION_B")]
- static void MethodB()
+ public static void MethodB()
{
Console.WriteLine("MethodB() executing...");
}

MODIFY
src/Chapter18/Listing18.25.UsingObsoleteAttribute.cs
src/Chapter18/Listing18.25.UsingObsoleteAttribute.cs
@@ -11,7 +11,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_25
}
[Obsolete]
- public static void ObsoleteMethod()//if you look through the warnings in the error list the warning will also show up in that list
+ public static void ObsoleteMethod()
{
}
}

MODIFY
src/Chapter18/Listing18.26.DynamicProgrammingUsingReflection.cs
src/Chapter18/Listing18.26.DynamicProgrammingUsingReflection.cs
@@ -14,6 +14,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_26
Console.WriteLine(data);
data = (double)data.Length;
data = data * 3.5 + 28.6;
+ //
if(data == 2.4 + 112 + 26.2)
{
Console.WriteLine(

MODIFY
src/Chapter18/Listing18.28.RuntimeBindingToXMLWithDynamics.cs
src/Chapter18/Listing18.28.RuntimeBindingToXMLWithDynamics.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_28
{
+ using Listing18_29;
#region INCLUDE
using System;
- using Listing18_29;
#region EXCLUDE
public class Program
{

MODIFY
src/Chapter18/Listing18.29.ImplementingACustomDynamicObject.cs
src/Chapter18/Listing18.29.ImplementingACustomDynamicObject.cs
@@ -24,8 +24,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_29
{
bool success = false;
result = null;
- XElement firstDescendant =
- Element.Descendants(binder.Name).First();
+ XElement? firstDescendant =
+ Element.Descendants(binder.Name).FirstOrDefault();
if(firstDescendant != null)
{
if(firstDescendant.Descendants().Any())
@@ -45,8 +45,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_29
SetMemberBinder binder, object? value)
{
bool success = false;
- XElement firstDescendant =
- Element.Descendants(binder.Name).First();
+ XElement? firstDescendant =
+ Element.Descendants(binder.Name).FirstOrDefault();
if(firstDescendant != null)
{
if(value?.GetType() == typeof(XElement))

ch. 18 code changes