本文整理汇总了C#中DataAnnotationsModelMetadataProvider.GetMetadataForType方法的典型用法代码示例。如果您正苦于以下问题:C# DataAnnotationsModelMetadataProvider.GetMetadataForType方法的具体用法?C# DataAnnotationsModelMetadataProvider.GetMetadataForType怎么用?C# DataAnnotationsModelMetadataProvider.GetMetadataForType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataAnnotationsModelMetadataProvider
的用法示例。
在下文中一共展示了DataAnnotationsModelMetadataProvider.GetMetadataForType方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetMetadataForTypeSetsTypeWithNullPropertyName
public void GetMetadataForTypeSetsTypeWithNullPropertyName() {
// Arrange
DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();
// Act
ModelMetadata result = provider.GetMetadataForType(null, typeof(string));
// Assert
Assert.AreEqual(typeof(string), result.ModelType);
Assert.IsNull(result.PropertyName);
}
示例2: GetMetadataForType
private static ModelMetadata GetMetadataForType(Type t)
{
DataAnnotationsModelMetadataProvider metadataProvider = new DataAnnotationsModelMetadataProvider();
return metadataProvider.GetMetadataForType(null, t);
}
示例3: GetMetadataForObject
private static ModelMetadata GetMetadataForObject(object o)
{
DataAnnotationsModelMetadataProvider metadataProvider = new DataAnnotationsModelMetadataProvider();
return metadataProvider.GetMetadataForType(() => o, o.GetType());
}
示例4: SimpleDisplayTextForNonNullClassWithNullDisplayColumnValueRevertsToDefaultBehavior
public void SimpleDisplayTextForNonNullClassWithNullDisplayColumnValueRevertsToDefaultBehavior() {
// Arrange
DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();
SimpleDisplayTextAttributeModel model = new SimpleDisplayTextAttributeModel();
ModelMetadata metadata = provider.GetMetadataForType(() => model, typeof(SimpleDisplayTextAttributeModel));
// Act
string result = metadata.SimpleDisplayText;
// Assert
Assert.AreEqual("42", result); // Falls back to the default logic of first property value
}
示例5: SimpleDisplayTextForNullClassRevertsToDefaultBehavior
public void SimpleDisplayTextForNullClassRevertsToDefaultBehavior() {
// Arrange
string expected = "Null Display Text";
DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();
ModelMetadata metadata = provider.GetMetadataForType(null, typeof(SimpleDisplayTextAttributeModel));
metadata.NullDisplayText = expected;
// Act
string result = metadata.SimpleDisplayText;
// Assert
Assert.AreEqual(expected, result);
}
示例6: SimpleDisplayTextForNonNullClassWithNonNullDisplayColumnValue
public void SimpleDisplayTextForNonNullClassWithNonNullDisplayColumnValue() {
// Arrange
string expected = "Custom property display value";
DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();
SimpleDisplayTextAttributeModel model = new SimpleDisplayTextAttributeModel { DisplayColumnProperty = expected };
ModelMetadata metadata = provider.GetMetadataForType(() => model, typeof(SimpleDisplayTextAttributeModel));
// Act
string result = metadata.SimpleDisplayText;
// Assert
Assert.AreEqual(expected, result);
}
示例7: SimpleDisplayTextForTypeWithWriteOnlyDisplayColumnThrows
public void SimpleDisplayTextForTypeWithWriteOnlyDisplayColumnThrows() {
// Arrange
DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();
// Act & Assert
ExceptionHelper.ExpectInvalidOperationException(
() => provider.GetMetadataForType(() => new WriteOnlyDisplayColumnModel(), typeof(WriteOnlyDisplayColumnModel)).SimpleDisplayText,
"System.Web.Mvc.Test.DataAnnotationsModelMetadataProviderTest+WriteOnlyDisplayColumnModel has a DisplayColumn attribute for WriteOnlyProperty, but property WriteOnlyProperty does not have a public getter.");
ExceptionHelper.ExpectInvalidOperationException(
() => provider.GetMetadataForType(() => new PrivateReadPublicWriteDisplayColumnModel(), typeof(PrivateReadPublicWriteDisplayColumnModel)).SimpleDisplayText,
"System.Web.Mvc.Test.DataAnnotationsModelMetadataProviderTest+PrivateReadPublicWriteDisplayColumnModel has a DisplayColumn attribute for PrivateReadPublicWriteProperty, but property PrivateReadPublicWriteProperty does not have a public getter.");
}
示例8: SimpleDisplayNameWithUnknownDisplayColumnThrows
public void SimpleDisplayNameWithUnknownDisplayColumnThrows() {
// Arrange
DataAnnotationsModelMetadataProvider provider = new DataAnnotationsModelMetadataProvider();
// Act & Assert
ExceptionHelper.ExpectInvalidOperationException(
() => provider.GetMetadataForType(() => new UnknownDisplayColumnModel(), typeof(UnknownDisplayColumnModel)).SimpleDisplayText,
"System.Web.Mvc.Test.DataAnnotationsModelMetadataProviderTest+UnknownDisplayColumnModel has a DisplayColumn attribute for NoPropertyWithThisName, but property NoPropertyWithThisName does not exist.");
}
示例9: CreateBindingContext
private static ModelBindingContext CreateBindingContext(IModelBinder binder,
IValueProvider valueProvider,
Type type,
IEnumerable<IModelValidatorProvider> validatorProviders = null)
{
validatorProviders = validatorProviders ?? Enumerable.Empty<IModelValidatorProvider>();
var metadataProvider = new DataAnnotationsModelMetadataProvider();
var bindingContext = new ModelBindingContext
{
ModelBinder = binder,
FallbackToEmptyPrefix = true,
MetadataProvider = metadataProvider,
ModelMetadata = metadataProvider.GetMetadataForType(null, type),
ModelState = new ModelStateDictionary(),
ValueProvider = valueProvider,
ValidatorProviders = validatorProviders
};
return bindingContext;
}