Make structs readonly

This commit is contained in:
Pranav K 2018-08-02 15:09:48 -07:00
parent e2b6f07778
commit 0989231ed5
27 changed files with 53 additions and 56 deletions

View File

@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.ActionConstraints
/// <summary> /// <summary>
/// A candidate action for action selection. /// A candidate action for action selection.
/// </summary> /// </summary>
public struct ActionSelectorCandidate public readonly struct ActionSelectorCandidate
{ {
/// <summary> /// <summary>
/// Creates a new <see cref="ActionSelectorCandidate"/>. /// Creates a new <see cref="ActionSelectorCandidate"/>.

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <summary> /// <summary>
/// An abstraction used when grouping enum values for <see cref="ModelMetadata.EnumGroupedDisplayNamesAndValues"/>. /// An abstraction used when grouping enum values for <see cref="ModelMetadata.EnumGroupedDisplayNamesAndValues"/>.
/// </summary> /// </summary>
public struct EnumGroupAndName public readonly struct EnumGroupAndName
{ {
private readonly Func<string> _name; private readonly Func<string> _name;

View File

@ -11,8 +11,20 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
/// <summary> /// <summary>
/// A key type which identifies a <see cref="ModelMetadata"/>. /// A key type which identifies a <see cref="ModelMetadata"/>.
/// </summary> /// </summary>
public struct ModelMetadataIdentity : IEquatable<ModelMetadataIdentity> public readonly struct ModelMetadataIdentity : IEquatable<ModelMetadataIdentity>
{ {
private ModelMetadataIdentity(
Type modelType,
string name = null,
Type containerType = null,
ParameterInfo parameterInfo = null)
{
ModelType = modelType;
Name = name;
ContainerType = containerType;
ParameterInfo = parameterInfo;
}
/// <summary> /// <summary>
/// Creates a <see cref="ModelMetadataIdentity"/> for the provided model <see cref="Type"/>. /// Creates a <see cref="ModelMetadataIdentity"/> for the provided model <see cref="Type"/>.
/// </summary> /// </summary>
@ -25,10 +37,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
throw new ArgumentNullException(nameof(modelType)); throw new ArgumentNullException(nameof(modelType));
} }
return new ModelMetadataIdentity() return new ModelMetadataIdentity(modelType);
{
ModelType = modelType,
};
} }
/// <summary> /// <summary>
@ -58,12 +67,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(name)); throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(name));
} }
return new ModelMetadataIdentity() return new ModelMetadataIdentity(modelType, name, containerType);
{
ModelType = modelType,
Name = name,
ContainerType = containerType,
};
} }
/// <summary> /// <summary>
@ -93,24 +97,19 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
throw new ArgumentNullException(nameof(modelType)); throw new ArgumentNullException(nameof(modelType));
} }
return new ModelMetadataIdentity() return new ModelMetadataIdentity(modelType, parameter.Name, parameterInfo: parameter);
{
Name = parameter.Name,
ModelType = modelType,
ParameterInfo = parameter,
};
} }
/// <summary> /// <summary>
/// Gets the <see cref="Type"/> defining the model property represented by the current /// Gets the <see cref="Type"/> defining the model property represented by the current
/// instance, or <c>null</c> if the current instance does not represent a property. /// instance, or <c>null</c> if the current instance does not represent a property.
/// </summary> /// </summary>
public Type ContainerType { get; private set; } public Type ContainerType { get; }
/// <summary> /// <summary>
/// Gets the <see cref="Type"/> represented by the current instance. /// Gets the <see cref="Type"/> represented by the current instance.
/// </summary> /// </summary>
public Type ModelType { get; private set; } public Type ModelType { get; }
/// <summary> /// <summary>
/// Gets a value indicating the kind of metadata represented by the current instance. /// Gets a value indicating the kind of metadata represented by the current instance.
@ -138,13 +137,13 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
/// Gets the name of the current instance if it represents a parameter or property, or <c>null</c> if /// Gets the name of the current instance if it represents a parameter or property, or <c>null</c> if
/// the current instance represents a type. /// the current instance represents a type.
/// </summary> /// </summary>
public string Name { get; private set; } public string Name { get; }
/// <summary> /// <summary>
/// Gets a descriptor for the parameter, or <c>null</c> if this instance /// Gets a descriptor for the parameter, or <c>null</c> if this instance
/// does not represent a parameter. /// does not represent a parameter.
/// </summary> /// </summary>
public ParameterInfo ParameterInfo { get; private set; } public ParameterInfo ParameterInfo { get; }
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(ModelMetadataIdentity other) public bool Equals(ModelMetadataIdentity other)

View File

@ -156,7 +156,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// by caller when child binding context state should be popped off of /// by caller when child binding context state should be popped off of
/// the <see cref="ModelBindingContext"/>. /// the <see cref="ModelBindingContext"/>.
/// </summary> /// </summary>
public struct NestedScope : IDisposable public readonly struct NestedScope : IDisposable
{ {
private readonly ModelBindingContext _context; private readonly ModelBindingContext _context;

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <summary> /// <summary>
/// Contains the result of model binding. /// Contains the result of model binding.
/// </summary> /// </summary>
public struct ModelBindingResult : IEquatable<ModelBindingResult> public readonly struct ModelBindingResult : IEquatable<ModelBindingResult>
{ {
/// <summary> /// <summary>
/// Creates a <see cref="ModelBindingResult"/> representing a failed model binding operation. /// Creates a <see cref="ModelBindingResult"/> representing a failed model binding operation.

View File

@ -1003,7 +1003,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
} }
} }
public struct PrefixEnumerable : IEnumerable<KeyValuePair<string, ModelStateEntry>> public readonly struct PrefixEnumerable : IEnumerable<KeyValuePair<string, ModelStateEntry>>
{ {
private readonly ModelStateDictionary _dictionary; private readonly ModelStateDictionary _dictionary;
private readonly string _prefix; private readonly string _prefix;
@ -1136,7 +1136,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
} }
} }
public struct KeyEnumerable : IEnumerable<string> public readonly struct KeyEnumerable : IEnumerable<string>
{ {
private readonly ModelStateDictionary _dictionary; private readonly ModelStateDictionary _dictionary;
@ -1191,7 +1191,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
} }
} }
public struct ValueEnumerable : IEnumerable<ModelStateEntry> public readonly struct ValueEnumerable : IEnumerable<ModelStateEntry>
{ {
private readonly ModelStateDictionary _dictionary; private readonly ModelStateDictionary _dictionary;

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// regardless of whether a single value or multiple values were submitted. /// regardless of whether a single value or multiple values were submitted.
/// </para> /// </para>
/// </remarks> /// </remarks>
public struct ValueProviderResult : IEquatable<ValueProviderResult>, IEnumerable<string> public readonly struct ValueProviderResult : IEquatable<ValueProviderResult>, IEnumerable<string>
{ {
private static readonly CultureInfo _invariantCulture = CultureInfo.InvariantCulture; private static readonly CultureInfo _invariantCulture = CultureInfo.InvariantCulture;

View File

@ -168,7 +168,7 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers
return false; return false;
} }
private struct RouteAttributeInfo private readonly struct RouteAttributeInfo
{ {
public RouteAttributeInfo(string name, string type, string[] keywords) public RouteAttributeInfo(string name, string type, string[] keywords)
{ {

View File

@ -659,7 +659,7 @@ namespace Microsoft.AspNetCore.Mvc.ApiExplorer
} }
} }
private struct PropertyKey private readonly struct PropertyKey
{ {
public readonly Type ContainerType; public readonly Type ContainerType;

View File

@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
/// <summary> /// <summary>
/// A media type value. /// A media type value.
/// </summary> /// </summary>
public struct MediaType public readonly struct MediaType
{ {
private static readonly StringSegment QualityParameter = new StringSegment("q"); private static readonly StringSegment QualityParameter = new StringSegment("q");
@ -683,7 +683,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
} }
} }
private struct MediaTypeParameter : IEquatable<MediaTypeParameter> private readonly struct MediaTypeParameter : IEquatable<MediaTypeParameter>
{ {
public MediaTypeParameter(StringSegment name, StringSegment value) public MediaTypeParameter(StringSegment name, StringSegment value)
{ {

View File

@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
return validators; return validators;
} }
private struct CacheEntry private readonly struct CacheEntry
{ {
public CacheEntry(IReadOnlyList<IClientModelValidator> validators) public CacheEntry(IReadOnlyList<IClientModelValidator> validators)
{ {

View File

@ -194,7 +194,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
return propertyBindingInfo; return propertyBindingInfo;
} }
private struct BinderItem private readonly struct BinderItem
{ {
public BinderItem(IModelBinder modelBinder, ModelMetadata modelMetadata) public BinderItem(IModelBinder modelBinder, ModelMetadata modelMetadata)
{ {

View File

@ -3,7 +3,7 @@
namespace Microsoft.AspNetCore.Mvc.Internal namespace Microsoft.AspNetCore.Mvc.Internal
{ {
public struct FilterCursorItem<TFilter, TFilterAsync> public readonly struct FilterCursorItem<TFilter, TFilterAsync>
{ {
public FilterCursorItem(TFilter filter, TFilterAsync filterAsync) public FilterCursorItem(TFilter filter, TFilterAsync filterAsync)
{ {

View File

@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc.Filters;
namespace Microsoft.AspNetCore.Mvc.Internal namespace Microsoft.AspNetCore.Mvc.Internal
{ {
public struct FilterFactoryResult public readonly struct FilterFactoryResult
{ {
public FilterFactoryResult( public FilterFactoryResult(
FilterItem[] cacheableFilters, FilterItem[] cacheableFilters,

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters.Internal
/// <summary> /// <summary>
/// A media type with its associated quality. /// A media type with its associated quality.
/// </summary> /// </summary>
public struct MediaTypeSegmentWithQuality public readonly struct MediaTypeSegmentWithQuality
{ {
/// <summary> /// <summary>
/// Initializes an instance of <see cref="MediaTypeSegmentWithQuality"/>. /// Initializes an instance of <see cref="MediaTypeSegmentWithQuality"/>.

View File

@ -122,7 +122,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
return validators; return validators;
} }
private struct CacheEntry private readonly struct CacheEntry
{ {
public CacheEntry(IReadOnlyList<IModelValidator> validators) public CacheEntry(IReadOnlyList<IModelValidator> validators)
{ {

View File

@ -345,7 +345,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
{ {
} }
private struct ModelMetadataCacheEntry private readonly struct ModelMetadataCacheEntry
{ {
public ModelMetadataCacheEntry(ModelMetadata metadata, DefaultMetadataDetails details) public ModelMetadataCacheEntry(ModelMetadata metadata, DefaultMetadataDetails details)
{ {

View File

@ -310,7 +310,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
// the ParameterDescriptor) or in a call to TryUpdateModel (no BindingInfo) or as a collection element. // the ParameterDescriptor) or in a call to TryUpdateModel (no BindingInfo) or as a collection element.
// //
// We need to be able to tell the difference between these things to avoid over-caching. // We need to be able to tell the difference between these things to avoid over-caching.
private struct Key : IEquatable<Key> private readonly struct Key : IEquatable<Key>
{ {
private readonly ModelMetadata _metadata; private readonly ModelMetadata _metadata;
private readonly object _token; // Explicitly using ReferenceEquality for tokens. private readonly object _token; // Explicitly using ReferenceEquality for tokens.

View File

@ -318,7 +318,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Validation
return entry; return entry;
} }
protected struct StateManager : IDisposable protected readonly struct StateManager : IDisposable
{ {
private readonly ValidationVisitor _visitor; private readonly ValidationVisitor _visitor;
private readonly object _container; private readonly object _container;

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
/// <summary> /// <summary>
/// An item in <see cref="ViewLocationCacheResult"/>. /// An item in <see cref="ViewLocationCacheResult"/>.
/// </summary> /// </summary>
public struct ViewLocationCacheItem public readonly struct ViewLocationCacheItem
{ {
/// <summary> /// <summary>
/// Initializes a new instance of <see cref="ViewLocationCacheItem"/>. /// Initializes a new instance of <see cref="ViewLocationCacheItem"/>.

View File

@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
/// <summary> /// <summary>
/// Key for entries in <see cref="RazorViewEngine.ViewLookupCache"/>. /// Key for entries in <see cref="RazorViewEngine.ViewLookupCache"/>.
/// </summary> /// </summary>
public struct ViewLocationCacheKey : IEquatable<ViewLocationCacheKey> public readonly struct ViewLocationCacheKey : IEquatable<ViewLocationCacheKey>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of <see cref="ViewLocationCacheKey"/>. /// Initializes a new instance of <see cref="ViewLocationCacheKey"/>.

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// <summary> /// <summary>
/// Result of <see cref="IRazorPageFactoryProvider.CreateFactory(string)"/>. /// Result of <see cref="IRazorPageFactoryProvider.CreateFactory(string)"/>.
/// </summary> /// </summary>
public struct RazorPageFactoryResult public readonly struct RazorPageFactoryResult
{ {
/// <summary> /// <summary>
/// Initializes a new instance of <see cref="RazorPageFactoryResult"/> with the /// Initializes a new instance of <see cref="RazorPageFactoryResult"/> with the

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// <summary> /// <summary>
/// Result of locating a <see cref="IRazorPage"/>. /// Result of locating a <see cref="IRazorPage"/>.
/// </summary> /// </summary>
public struct RazorPageResult public readonly struct RazorPageResult
{ {
/// <summary> /// <summary>
/// Initializes a new instance of <see cref="RazorPageResult"/> for a successful discovery. /// Initializes a new instance of <see cref="RazorPageResult"/> for a successful discovery.

View File

@ -163,7 +163,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
} }
} }
private struct BinderItem private readonly struct BinderItem
{ {
public BinderItem(IModelBinder modelBinder, ModelMetadata modelMetadata) public BinderItem(IModelBinder modelBinder, ModelMetadata modelMetadata)
{ {

View File

@ -352,7 +352,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers.Internal
return new StringSegment(value.Buffer, offset, trimmedEnd - offset + 1); return new StringSegment(value.Buffer, offset, trimmedEnd - offset + 1);
} }
private struct GlobbingUrlKey : IEquatable<GlobbingUrlKey> private readonly struct GlobbingUrlKey : IEquatable<GlobbingUrlKey>
{ {
public GlobbingUrlKey(string include, string exclude) public GlobbingUrlKey(string include, string exclude)
{ {

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
/// Encapsulates a string or <see cref="IHtmlContent"/> value. /// Encapsulates a string or <see cref="IHtmlContent"/> value.
/// </summary> /// </summary>
[DebuggerDisplay("{DebuggerToString()}")] [DebuggerDisplay("{DebuggerToString()}")]
public struct ViewBufferValue public readonly struct ViewBufferValue
{ {
/// <summary> /// <summary>
/// Initializes a new instance of <see cref="ViewBufferValue"/> with a <c>string</c> value. /// Initializes a new instance of <see cref="ViewBufferValue"/> with a <c>string</c> value.
@ -41,15 +41,13 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
{ {
using (var writer = new StringWriter()) using (var writer = new StringWriter())
{ {
var valueAsString = Value as string; if (Value is string valueAsString)
if (valueAsString != null)
{ {
writer.Write(valueAsString); writer.Write(valueAsString);
return writer.ToString(); return writer.ToString();
} }
var valueAsContent = Value as IHtmlContent; if (Value is IHtmlContent valueAsContent)
if (valueAsContent != null)
{ {
valueAsContent.WriteTo(writer, HtmlEncoder.Default); valueAsContent.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString(); return writer.ToString();

View File

@ -8,7 +8,7 @@ using System.Reflection;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{ {
internal struct MemberExpressionCacheKey internal readonly struct MemberExpressionCacheKey
{ {
public MemberExpressionCacheKey(Type modelType, MemberExpression memberExpression) public MemberExpressionCacheKey(Type modelType, MemberExpression memberExpression)
{ {
@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
public MemberInfo[] Members { get; } public MemberInfo[] Members { get; }
public Enumerator GetEnumerator() => new Enumerator(ref this); public Enumerator GetEnumerator() => new Enumerator(this);
public struct Enumerator public struct Enumerator
{ {
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
private int _index; private int _index;
private MemberExpression _memberExpression; private MemberExpression _memberExpression;
public Enumerator(ref MemberExpressionCacheKey key) public Enumerator(in MemberExpressionCacheKey key)
{ {
Current = null; Current = null;
_members = key.Members; _members = key.Members;