public...() where T statement confusion??
-
I'm trying to understand what this piece of code does from Prajeesh's Blog I've never seen a statement with any 'where' statements in the method before
using FluentValidation;
....public static ValidationResult Validate(K entity)
where T : IValidator, new()
where K : class
{
IValidator __Validator = new T();
return __Validator.Validate(entity);
}Can you help...???
-
I'm trying to understand what this piece of code does from Prajeesh's Blog I've never seen a statement with any 'where' statements in the method before
using FluentValidation;
....public static ValidationResult Validate(K entity)
where T : IValidator, new()
where K : class
{
IValidator __Validator = new T();
return __Validator.Validate(entity);
}Can you help...???
-
I'm trying to understand what this piece of code does from Prajeesh's Blog I've never seen a statement with any 'where' statements in the method before
using FluentValidation;
....public static ValidationResult Validate(K entity)
where T : IValidator, new()
where K : class
{
IValidator __Validator = new T();
return __Validator.Validate(entity);
}Can you help...???
The where statements are used to constrain which Types the generic can be used for. So:
Sevententh wrote:
where T : IValidator, new()
Means that the type T must inherit from IValidator and must have an accessible constructor that takes no arguments.
Sevententh wrote:
where K : class
This states that the type must be a Reference Type, not a Value Type.
-
yes, no reply as yet, so I thought I'd ask here, seeing as I use CP loads :-D
-
The where statements are used to constrain which Types the generic can be used for. So:
Sevententh wrote:
where T : IValidator, new()
Means that the type T must inherit from IValidator and must have an accessible constructor that takes no arguments.
Sevententh wrote:
where K : class
This states that the type must be a Reference Type, not a Value Type.
So is the above code wrong? It should be
public static ValidationResult Validate<T>(K entity)
where T : IValidator, new()
where K : class
{
IValidator __Validator = new T();
return __Validator.Validate(entity);
}As I get the Error: Constraints are not allowed on non-generic declarations, if I use the code from the blog...? I think I need to wait until the author gets back to me! I run into even more problems if i use this change!! :~ Thanks for your help tho
-
So is the above code wrong? It should be
public static ValidationResult Validate<T>(K entity)
where T : IValidator, new()
where K : class
{
IValidator __Validator = new T();
return __Validator.Validate(entity);
}As I get the Error: Constraints are not allowed on non-generic declarations, if I use the code from the blog...? I think I need to wait until the author gets back to me! I run into even more problems if i use this change!! :~ Thanks for your help tho
You could try:
public static ValidationResult Validate<T, K>(K entity)
where T : IValidator, new()
where K : class
{
IValidator __Validator = new T();
return __Validator.Validate(entity);
}But, without more context this is nothing more than an educated guess, so the blog writer will definitly know best.
-
You could try:
public static ValidationResult Validate<T, K>(K entity)
where T : IValidator, new()
where K : class
{
IValidator __Validator = new T();
return __Validator.Validate(entity);
}But, without more context this is nothing more than an educated guess, so the blog writer will definitly know best.
Fantastic! that resolved all the other problems I was having with this code :-D Thanks