Ciel
Ciel

Reputation: 4440

C# - Using a Predicate to select a property

I've got a situation where I would like to pass the name of a property through a method as a parameter so that the method can invoke it.

I'm aware that I can do this with Reflection, but that's a bit overkill - and complicated, for what I want. So I explored a bit more and discovered the wonders of Predicate<T>.

This is basically what I'm angling to do, and keep in mind, this is like - the most rudimentary, quickly thrown together example possible;

public class ContainingParentClassWithProperties {
    public PackagedClassWithProperty OptionOne {
        get; set;
    } = new PackagedClassWithProperty {
        DesiredTargetProperty = "1"
    };

    public PackagedClassWithProperty OptionTwo {
        get; set;
    } = new PackagedClassWithProperty {
        DesiredTargetProperty = "2"
    };
}

Basic wrappers for a type of object that'll contain properties.

Then a method that can take them.

public void TryAcceptPredicateMethod(Predicate<ContainingParentClassWithProperties> p) {
    Console.WriteLine(p.DesiredTargetProperty);
}

And then using it;

public void TryCallAcceptPredicateMethod() {
    TryAcceptPredicateMethod(n => n.OptionOne);
}

I'm encountering two major problems;

1. "cannot convert lambda"

I get the following error when I try to call the method;

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

I honestly have no idea what this is saying. I'm not very versed in lambda expressions though;

2. Calling the intended propety

Trying to write out/call the property I'm aiming for just isn't working; It's telling me the Predicate<ContainingParentClassWithProperties> contains no definition of DesiredTargetProperty.

I'm pretty confused, overall. Can anyone help to clarify some of this, please?

Upvotes: 2

Views: 2287

Answers (2)

Jonathan Twite
Jonathan Twite

Reputation: 952

How about having properties defined as

public class PropertyContainingClass
{
    public int MyProperty
    {
        get
        {
            if(isRunningLocal)
            {
                return 1;
            }
            else
            {
                return 2;
            }
        }
    }
    private bool isRunningLocal{ get; set; }
    public PropertyContainingClass()
    {
        this.isRunningLocal = //code to determine whether running local
    }
}

Class could be static if you like it that way

Upvotes: 0

DRKblade
DRKblade

Reputation: 347

You shouldn't use Predicate for that. As in my comment, please learn more about predicates.

But here I think you can do like this

public void TryAcceptPredicateMethod(Func<PackagedClassWithProperty> p) {
    Console.WriteLine(p.DesiredTargetProperty);
}

public void TryCallAcceptPredicateMethod() {
    // n should be a variable or something in the type of ContainingParentClassWithProperties 
    TryAcceptPredicateMethod(() => n.OptionOne);
}

Upvotes: 1

Related Questions