// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
/// <summary>
/// This is the default value.
/// </summary>
- private object _value;
+ private object? _value;
// Delegate ad hoc created 'TypeDescriptor.ConvertFromInvariantString' reflection object cache
- private static object s_convertFromInvariantString;
+ private static object? s_convertFromInvariantString;
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/>
/// class, converting the specified value to the specified type, and using the U.S. English
/// culture as the translation context.
/// </summary>
- public DefaultValueAttribute(Type type, string value)
+ public DefaultValueAttribute(Type? type, string? value)
{
- // The try/catch here is because attributes should never throw exceptions.
+ // The null check and try/catch here are because attributes should never throw exceptions.
// We would fail to load an otherwise normal class.
+
+ if (type == null)
+ {
+ return;
+ }
+
try
{
- if (TryConvertFromInvariantString(type, value, out object convertedValue))
+ if (TryConvertFromInvariantString(type, value, out object? convertedValue))
{
_value = convertedValue;
}
- else if (type.IsSubclassOf(typeof(Enum)))
+ else if (type.IsSubclassOf(typeof(Enum)) && value != null)
{
_value = Enum.Parse(type, value, true);
}
- else if (type == typeof(TimeSpan))
+ else if (type == typeof(TimeSpan) && value != null)
{
_value = TimeSpan.Parse(value);
}
}
// Looking for ad hoc created TypeDescriptor.ConvertFromInvariantString(Type, string)
- bool TryConvertFromInvariantString(Type typeToConvert, string stringValue, out object conversionResult)
+ bool TryConvertFromInvariantString(Type? typeToConvert, string? stringValue, out object? conversionResult)
{
conversionResult = null;
if (s_convertFromInvariantString == null)
{
Type typeDescriptorType = Type.GetType("System.ComponentModel.TypeDescriptor, System.ComponentModel.TypeConverter", throwOnError: false);
- MethodInfo mi = typeDescriptorType?.GetMethod("ConvertFromInvariantString", BindingFlags.NonPublic | BindingFlags.Static);
+ MethodInfo? mi = typeDescriptorType?.GetMethod("ConvertFromInvariantString", BindingFlags.NonPublic | BindingFlags.Static);
Volatile.Write(ref s_convertFromInvariantString, mi == null ? new object() : mi.CreateDelegate(typeof(Func<Type, string, object>)));
}
- if (!(s_convertFromInvariantString is Func<Type, string, object> convertFromInvariantString))
+ if (!(s_convertFromInvariantString is Func<Type?, string?, object> convertFromInvariantString))
return false;
try
/// Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/>
/// class using a <see cref='System.String'/>.
/// </summary>
- public DefaultValueAttribute(string value)
+ public DefaultValueAttribute(string? value)
{
_value = value;
}
/// Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/>
/// class.
/// </summary>
- public DefaultValueAttribute(object value)
+ public DefaultValueAttribute(object? value)
{
_value = value;
}
/// <summary>
/// Gets the default value of the property this attribute is bound to.
/// </summary>
- public virtual object Value => _value;
+ public virtual object? Value => _value;
- public override bool Equals(object obj)
+ public override bool Equals(object? obj)
{
if (obj == this)
{
public override int GetHashCode() => base.GetHashCode();
- protected void SetValue(object value) => _value = value;
+ protected void SetValue(object? value) => _value = value;
}
}
**
===========================================================*/
+#nullable enable
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(
[Conditional("CODE_ANALYSIS")]
public sealed class SuppressMessageAttribute : Attribute
{
- public SuppressMessageAttribute(string category, string checkId)
+ public SuppressMessageAttribute(string? category, string? checkId)
{
Category = category;
CheckId = checkId;
}
- public string Category { get; }
- public string CheckId { get; }
- public string Scope { get; set; }
- public string Target { get; set; }
- public string MessageId { get; set; }
- public string Justification { get; set; }
+ public string? Category { get; }
+ public string? CheckId { get; }
+ public string? Scope { get; set; }
+ public string? Target { get; set; }
+ public string? MessageId { get; set; }
+ public string? Justification { get; set; }
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
public sealed class ContractFailedEventArgs : EventArgs
{
private ContractFailureKind _failureKind;
- private string _message;
- private string _condition;
- private Exception _originalException;
+ private string? _message;
+ private string? _condition;
+ private Exception? _originalException;
private bool _handled;
private bool _unwind;
- internal Exception thrownDuringHandler;
+ internal Exception? thrownDuringHandler;
- public ContractFailedEventArgs(ContractFailureKind failureKind, string message, string condition, Exception originalException)
+ public ContractFailedEventArgs(ContractFailureKind failureKind, string? message, string? condition, Exception? originalException)
{
Debug.Assert(originalException == null || failureKind == ContractFailureKind.PostconditionOnException);
_failureKind = failureKind;
_originalException = originalException;
}
- public string Message { get { return _message; } }
- public string Condition { get { return _condition; } }
+ public string? Message { get { return _message; } }
+ public string? Condition { get { return _condition; } }
public ContractFailureKind FailureKind { get { return _failureKind; } }
- public Exception OriginalException { get { return _originalException; } }
+ public Exception? OriginalException { get { return _originalException; } }
// Whether the event handler "handles" this contract failure, or to fail via escalation policy.
public bool Handled