// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Mvc.Abstractions;
namespace Microsoft.AspNet.Mvc.ModelBinding.Metadata
{
///
/// A key type which identifies a .
///
public struct ModelMetadataIdentity
{
///
/// Creates a for the provided model .
///
/// The model .
/// A .
public static ModelMetadataIdentity ForType(Type modelType)
{
if (modelType == null)
{
throw new ArgumentNullException(nameof(modelType));
}
return new ModelMetadataIdentity()
{
ModelType = modelType,
};
}
///
/// Creates a for the provided property.
///
/// The model type.
/// The name of the property.
/// The container type of the model property.
/// A .
public static ModelMetadataIdentity ForProperty(
Type modelType,
string name,
Type containerType)
{
if (modelType == null)
{
throw new ArgumentNullException(nameof(modelType));
}
if (containerType == null)
{
throw new ArgumentNullException(nameof(containerType));
}
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(name));
}
return new ModelMetadataIdentity()
{
ModelType = modelType,
Name = name,
ContainerType = containerType,
};
}
///
/// Gets the defining the model property respresented by the current
/// instance, or null if the current instance does not represent a property.
///
public Type ContainerType { get; private set; }
///
/// Gets the represented by the current instance.
///
public Type ModelType { get; private set; }
///
/// Gets a value indicating the kind of metadata represented by the current instance.
///
public ModelMetadataKind MetadataKind
{
get
{
if (ContainerType != null && Name != null)
{
return ModelMetadataKind.Property;
}
else
{
return ModelMetadataKind.Type;
}
}
}
///
/// Gets the name of the current instance if it represents a parameter or property, or null if
/// the current instance represents a type.
///
public string Name { get; private set; }
}
}