Null and Generic Type

S

Shapper

Hello,

I am trying to validate a few Reply class properties.
When a condition is satisfied an object is returned:

CustomResult r = new ReplyValidator<FindFileByIdReply, CustomResult>()
.Set(x => x.Info == ReplyInfo.AccessDenied, CustomResult.AccessDenied())
.Set(x => x.Info == ReplyInfo.NotFound, CustomResult.NotFound())
.Validate(reply);

or:

String r = new ReplyValidator<FindFileByIdReply, String>()
.Set(x => x.Info == ReplyInfo.AccessDenied, "Access is Denied")
.Set(x => x.Info == ReplyInfo.NotFound, "Not Found")
.Validate(reply);

Consider the following cases:
1) myReply.Info = ReplyInfo.AccessDenied >> GET Acess is Denied
2) myReply.Info = ReplyInfo.NotFound >> GET Not Found
2) myReply.Info = ReplyInfo.OK >> GET null.

However, I am having problems when returning null ...

Could someone help me out or maybe suggesting a different approach?

public class ReplyValidator<T, R> where T : Reply {

private IDictionary<Func<T, Boolean>, R> Rules { get; set; }

public ReplyValidator() {
Rules = new Dictionary<Func<T, Boolean>, R>();
} // ReplyValidator

public ReplyValidator<T, R> Set(Func<T, Boolean> predicate, R result) {
Rules.Add(predicate, result);
return this;
} // Set

public R Validate(T reply) {
foreach (KeyValuePair<Func<T, Boolean>, R> rule in Rules)
if (rule.Key(reply))
return rule.Value;

return null; // NOT ABLE TO RETURN NULL

} // Validate
} // ReplyValidator

Thank You,
Miguel
 
B

bradbury9

Why dont you change

public class ReplyValidator<T, R> where T : Reply {

with

public class ReplyValidator<T, R> where T : Reply, where R : Nullable {
 
B

bradbury9

El viernes, 14 de diciembre de 2012 02:48:26 UTC+1, Peter Duniho escribió:
Probably because that won't work. The System.Nullable type is just a

container for three static methods used for manipulating instances of the

System.Nullable<T> struct. Actual nullable types don't actually inherit

it.

Thanks for the info, should have checked myself before posting but I was ina hurry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top