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>
/// A candidate action for action selection.
/// </summary>
public struct ActionSelectorCandidate
public readonly struct ActionSelectorCandidate
{
/// <summary>
/// Creates a new <see cref="ActionSelectorCandidate"/>.

View File

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

View File

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

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <summary>
/// Contains the result of model binding.
/// </summary>
public struct ModelBindingResult : IEquatable<ModelBindingResult>
public readonly struct ModelBindingResult : IEquatable<ModelBindingResult>
{
/// <summary>
/// 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 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;
@ -1191,7 +1191,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
}
}
public struct ValueEnumerable : IEnumerable<ModelStateEntry>
public readonly struct ValueEnumerable : IEnumerable<ModelStateEntry>
{
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.
/// </para>
/// </remarks>
public struct ValueProviderResult : IEquatable<ValueProviderResult>, IEnumerable<string>
public readonly struct ValueProviderResult : IEquatable<ValueProviderResult>, IEnumerable<string>
{
private static readonly CultureInfo _invariantCulture = CultureInfo.InvariantCulture;

View File

@ -168,7 +168,7 @@ namespace Microsoft.AspNetCore.Mvc.Analyzers
return false;
}
private struct RouteAttributeInfo
private readonly struct RouteAttributeInfo
{
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;

View File

@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
/// <summary>
/// A media type value.
/// </summary>
public struct MediaType
public readonly struct MediaType
{
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)
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -122,7 +122,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
return validators;
}
private struct CacheEntry
private readonly struct CacheEntry
{
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)
{

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.
//
// 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 object _token; // Explicitly using ReferenceEquality for tokens.

View File

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

View File

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

View File

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

View File

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

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// <summary>
/// Result of locating a <see cref="IRazorPage"/>.
/// </summary>
public struct RazorPageResult
public readonly struct RazorPageResult
{
/// <summary>
/// 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)
{

View File

@ -352,7 +352,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers.Internal
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)
{

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
/// Encapsulates a string or <see cref="IHtmlContent"/> value.
/// </summary>
[DebuggerDisplay("{DebuggerToString()}")]
public struct ViewBufferValue
public readonly struct ViewBufferValue
{
/// <summary>
/// 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())
{
var valueAsString = Value as string;
if (valueAsString != null)
if (Value is string valueAsString)
{
writer.Write(valueAsString);
return writer.ToString();
}
var valueAsContent = Value as IHtmlContent;
if (valueAsContent != null)
if (Value is IHtmlContent valueAsContent)
{
valueAsContent.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();

View File

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