本文整理汇总了C#中ModelBinderDictionary.GetBinder方法的典型用法代码示例。如果您正苦于以下问题:C# ModelBinderDictionary.GetBinder方法的具体用法?C# ModelBinderDictionary.GetBinder怎么用?C# ModelBinderDictionary.GetBinder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModelBinderDictionary
的用法示例。
在下文中一共展示了ModelBinderDictionary.GetBinder方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetBinderDoesNotReturnDefaultBinderIfAskedNotTo
public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo() {
// Proper order of precedence:
// 1. Binder registered in the global table
// 2. Binder attribute defined on the type
// 3. <null>
// Arrange
IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
ModelBinderDictionary binders = new ModelBinderDictionary() {
{ typeof(MyFirstConvertibleType), registeredFirstBinder }
};
// Act
IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */);
IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */);
IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */);
// Assert
Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
Assert.IsNull(binder3, "Third binder should have returned null since asked not to use default.");
}
示例2: GetBinderConsultsProviders
public void GetBinderConsultsProviders() {
//Arrange
Type modelType = typeof(string);
IModelBinder expectedBinderFromProvider = new Mock<IModelBinder>().Object;
Mock<IModelBinderProvider> locatedProvider = new Mock<IModelBinderProvider>();
locatedProvider.Setup(p => p.GetBinder(modelType))
.Returns(expectedBinderFromProvider);
Mock<IModelBinderProvider> secondProvider = new Mock<IModelBinderProvider>();
ModelBinderProviderCollection providers = new ModelBinderProviderCollection(new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object });
ModelBinderDictionary binders = new ModelBinderDictionary(providers);
//Act
IModelBinder returnedBinder = binders.GetBinder(modelType);
//Assert
Assert.AreSame(expectedBinderFromProvider, returnedBinder);
}
示例3: GetBinderThrowsIfModelTypeContainsMultipleAttributes
public void GetBinderThrowsIfModelTypeContainsMultipleAttributes() {
// Arrange
ModelBinderDictionary binders = new ModelBinderDictionary();
// Act & Assert
ExceptionHelper.ExpectInvalidOperationException(
delegate {
binders.GetBinder(typeof(ConvertibleTypeWithSeveralBinders), true /* fallbackToDefault */);
},
"The type 'System.Web.Mvc.Test.ModelBinderDictionaryTest+ConvertibleTypeWithSeveralBinders' contains multiple attributes that inherit from CustomModelBinderAttribute.");
}
示例4: GetBinderResolvesBindersWithCorrectPrecedence
public void GetBinderResolvesBindersWithCorrectPrecedence() {
// Proper order of precedence:
// 1. Binder registered in the global table
// 2. Binder attribute defined on the type
// 3. Default binder
// Arrange
IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
ModelBinderDictionary binders = new ModelBinderDictionary() {
{ typeof(MyFirstConvertibleType), registeredFirstBinder }
};
IModelBinder defaultBinder = new Mock<IModelBinder>().Object;
binders.DefaultBinder = defaultBinder;
// Act
IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
IModelBinder binder3 = binders.GetBinder(typeof(object));
// Assert
Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
Assert.AreSame(defaultBinder, binder3, "Third binder should have been the fallback.");
}
示例5: GetBinderThrowsIfModelTypeIsNull
public void GetBinderThrowsIfModelTypeIsNull() {
// Arrange
ModelBinderDictionary binders = new ModelBinderDictionary();
// Act & Assert
ExceptionHelper.ExpectArgumentNullException(
delegate {
binders.GetBinder(null);
}, "modelType");
}
示例6: GetBinderDoesNotReturnDefaultBinderIfAskedNotTo
public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo()
{
// Proper order of precedence:
// 1. Binder registered in the global table
// 2. Binder attribute defined on the type
// 3. <null>
// Arrange
IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
ModelBinderDictionary binders = new ModelBinderDictionary()
{
{ typeof(MyFirstConvertibleType), registeredFirstBinder }
};
// Act
IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */);
IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */);
IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */);
// Assert
Assert.Same(registeredFirstBinder, binder1);
Assert.IsType<MySecondBinder>(binder2);
Assert.Null(binder3);
}
示例7: GetBinderThrowsIfModelTypeIsNull
public void GetBinderThrowsIfModelTypeIsNull()
{
// Arrange
ModelBinderDictionary binders = new ModelBinderDictionary();
// Act & Assert
Assert.ThrowsArgumentNull(
delegate { binders.GetBinder(null); }, "modelType");
}
示例8: GetBinderResolvesBindersWithCorrectPrecedence
public void GetBinderResolvesBindersWithCorrectPrecedence()
{
// Proper order of precedence:
// 1. Binder registered in the global table
// 2. Binder attribute defined on the type
// 3. Default binder
// Arrange
IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
ModelBinderDictionary binders = new ModelBinderDictionary()
{
{ typeof(MyFirstConvertibleType), registeredFirstBinder }
};
IModelBinder defaultBinder = new Mock<IModelBinder>().Object;
binders.DefaultBinder = defaultBinder;
// Act
IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
IModelBinder binder3 = binders.GetBinder(typeof(object));
// Assert
Assert.Same(registeredFirstBinder, binder1);
Assert.IsType<MySecondBinder>(binder2);
Assert.Same(defaultBinder, binder3);
}