views:

118

answers:

5

I want to be able to do this:

var test = SomeMethod(s => s.SomeMethod);

I can make it work with properties by making the method signature look like this:

SomeMethod<TProperty>(Expression<Func<T, TProperty>> expression)

How can I make it work with methods? I know this is simple, I'm just missing something small.

A: 

Something like this?

    public SimpleCommand( Predicate<object> canExecuteDelegate, Action<object> executeDelegate )
    {
        CanExecuteDelegate = canExecuteDelegate;
        ExecuteDelegate = executeDelegate;
    }

You will need to specify the function signature using Predicate or Action.

David Lynch
Definitely not the first parameter, because Predicate returns true or false... I don't think the second one works unless I"m doing it wrong...In my SomeMethod method I need to be able to access information about the method itself that was passed in.
Max Schmeling
I missed that part then. So you need an expression to get the name of the function (or some other thing)?
David Lynch
Yeah, basically I need the name... and information about the parameters it takes if possible (not necessary though atm)
Max Schmeling
A: 

Take a look here:

How to pass a function as a parameter in C#?

Pass Method as Parameter using C#

Leniel Macaferi
i doubt this is what he is looking for..
Stan R.
A: 
class Program
    {
        public class Test
        {
            public bool SomeMethod(string test)
            {
                return true;
            }
        }

        static void Main(string[] args)
        {

            Test testObj = new Test();


            Func<string, bool> rule1 = AddRule(testObj, x => x.SomeMethod);

            bool rsult = rule1("ttt");

        }

        static Func<string, bool> AddRule<T>( T obj, Func<T,Func<string, bool>> func)
        {
            return func(obj);
        }


}
Stan R.
This way of doing it requires me to know the method signature, correct? That isn't a possibility.
Max Schmeling
yeah, there is no way without knowing the method signature as they could be an infinite possibilities.
Stan R.
@Max, tricky question ;)
Stan R.
There has to be a way... even if it isn't quite as clean... maybe I can pass a delegate type as a generic parameter... hmm
Max Schmeling
I guess that wouldn't be any different from knowing the types
Max Schmeling
yes, you will always need to know the signature...
Stan R.
I find it hard to believe there isn't a way around this... I don't want to resort to magic strings... SecurityFor("Delete")... I guess I could just use T4 to generate constants or something like that... *sigh*
Max Schmeling
+1  A: 

Is something like this what you're looking for?

DoSomethingWithAction<T>(Func<T, Action> actionRetriever) { }
DoSomethingWithFunction<T, TResult>(Func<T, Func<TResult>> functionRetriever) { }

You would call these like:

DoSomethingWithAction<ObjectWithMethod>(obj => obj.Method);
DoSomethingWithFunction<ObjectWithProperty>(obj => obj.Property);
Dan Tao
A: 

I think, there's no way of doing this without specifying the method signature: Just think of overloaded methods:

void SomeMethod() { }
int SomeMethod(int a, int b) { return 0; }

// ...

var test = TestMethod(s => s.SomeMethod);

How would the compiler possibly know, which of the methods you want to test?

MartinStettner