Func<TSource, bool>

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following method on a open source library:

public void MapFrom(Func<TSource, object> sourceMember) {
_propertyMap.AssignCustomValueResolver(new
DelegateBasedResolver<TSource>(sourceMember));
}

Which is used as follows:
MapFrom(s => s.File.InputStream)

I would like to add a condition to it as follows:

public void MapFrom(Func<TSource, object> sourceMember, bool
condition) {
if (condition)
_propertyMap.Ignore();
else
_propertyMap.AssignCustomValueResolver(new
DelegateBasedResolver<TSource>(sourceMember));
}

But that I could use as follows:
MapFrom(s => s.File.InputStream, s.FileInputStream.ContenLenght == 0)

So I would like to also use the object s in the condition parameter.

I tried to replace "bool condition" by "Func<TSource, bool> condition"

But then I am not sure how to test its value inside the method.

How can I do this?

Thanks,
Miguel
 
shapper said:
Hello,

I have the following method on a open source library:

public void MapFrom(Func<TSource, object> sourceMember) {
_propertyMap.AssignCustomValueResolver(new
DelegateBasedResolver<TSource>(sourceMember));
}

Which is used as follows:
MapFrom(s => s.File.InputStream)

I would like to add a condition to it as follows:

public void MapFrom(Func<TSource, object> sourceMember, bool
condition) {
if (condition)
_propertyMap.Ignore();
else
_propertyMap.AssignCustomValueResolver(new
DelegateBasedResolver<TSource>(sourceMember));
}

But that I could use as follows:
MapFrom(s => s.File.InputStream, s.FileInputStream.ContenLenght == 0)

So I would like to also use the object s in the condition parameter.

I tried to replace "bool condition" by "Func<TSource, bool> condition"

But then I am not sure how to test its value inside the method.

MapFrom(s => s.File.InputStream, s => (s.FileInputStream.ContentLength==
0));

public void MapFrom(Func<TSource, object> sourceMember, Func<TSource,bool>
predicate)
{
if (predicate(?)) { ... };
}

There's a problem. MapFrom didn't actually use sourceMember for anything,
it just handed it off to DelegateBasedResolver. So you'll have to change
that to accept a predicate as well, and so on, until you find the place
where you have an actual TSource object.
 
There's a problem.  MapFrom didn't actually use sourceMember for anything,
it just handed it off to DelegateBasedResolver.  So you'll have to change
that to accept a predicate as well, and so on, until you find the place
where you have an actual TSource object.

Yes, but I also have the following method:

public void Ignore() {
_propertyMap.Ignore();
}

Can't I add a Func<TSource,bool> predicate so I can use a Lambda
expression that would call Ignore if is true. Something like:

public void MapFrom(Func<TSource, bool> predicate) {
if (predicate ??? )
_propertyMap.Ignore();
}

Basically, what I am trying to do is to have something like:
Ignore(s => s.Type == "image")

So I would run the code _propertyMap.Ignore(); only if this condition
is true.

I am not familiar with creating predicates ...

Thanks,
Miguel
 
Yes, but I also have the following method:

public void Ignore() {
_propertyMap.Ignore();
}

Can't I add a Func<TSource,bool> predicate so I can use a Lambda
expression that would call Ignore if is true. Something like:

public void MapFrom(Func<TSource, bool> predicate) {
if (predicate ??? )
_propertyMap.Ignore();
}

Basically, what I am trying to do is to have something like:
Ignore(s => s.Type == "image")

What does that mean? The example Ignore() method you posted doesn't take
any arguments. Nor does the usage you show in the MapFrom() method. Yet,
you say you want to be able to call it with a delegate that returns a bool?

That's not a meaningful question. GIGO.
So I would run the code _propertyMap.Ignore(); only if this condition
is true.

I am not familiar with creating predicates ...

Predicates are just like any other delegate; they are just a specific
kind, one that takes some input and returns a bool.

The problem Ben is talking about is that for the predicate you passed it
to be of any use, it has to be called with an instance of TSource. But
your MapFrom() method itself doesn't have an instance of TSource it's
using.

You failed to show a concise-but-complete code example that reliably
demonstrates the problem, but we can infer from the little code you did
post that your DelegateBasedResolver() method, or some method called by it
directly or indirectly, must be the one that is somehow enumerating one or
more instances of TSource and then executing the "sourceMember" delegate.

It is nonsensical to ask to be able to execute a delegate that takes as
input an instance of a particular type in code that has no access to an
instance of that particular type.

Most likely, you'll have to do what Ben suggested: you need to pass the
"predicate" delegate instance down along with the "sourceMember" delegate,
as far down in the call chain as you need to go before you _do_ have an
instance of "TSource" with which to call the "predicate" delegate.

It's possible that there's some way for you to get the necessary "TSource"
instance in your MapFrom() method, but there's certainly nothing in the
code example you posted to suggest that would be possible.

Pete
 
Back
Top