Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am trying to find a way to examine the expression passed into a function.

assuming I make this call
DoIt( ()=>new Person().FirstName );


to
public static string DoIt<t> (Func<t> callme){
   return "what did callme point to";
}
</t></t>


Is is possible for DoIt to return "FirstName"? and if so can you point me in the right direction please?

Many thanks.
Posted
Updated 6-Nov-16 14:14pm
v3
Comments
wizardzz 5-Apr-11 14:03pm    
Fixed typo.
Sander Rossel 5-Apr-11 14:35pm    
You actually missed the big typo in the subject!
http://en.wikipedia.org/wiki/Lambada :D
wizardzz 5-Apr-11 14:40pm    
Haha, I must have been thinking of this:
http://www.youtube.com/watch?v=PK2HANwsUWg
Sander Rossel 5-Apr-11 14:43pm    
Brings back memories :)
If LaBamba Then
SomeLameJokes
End If
:D
cechode 5-Apr-11 15:00pm    
LOL thx, not sure what i was thinking about ( heh)
actually i'm just a horrible speller :)
thx

Sorry if I don't understand your idea fully, but did you pay attention for System.Delegate.GetInvocationList? It answers exactly the question "what did callme points to". This is nothing like "reverse lambda" though.

Maybe you need to explain your vaguely expressed idea in terms what you want to achieve, exactly, and what would be the ultimate purpose of it…

[EDIT]
Answering the follow-up question/clarification.
This is about Expressions and Expression Trees. Please see http://msdn.microsoft.com/en-us/library/bb397951.aspx[^].

—SA
 
Share this answer
 
v2
Comments
cechode 5-Apr-11 15:03pm    
GetInvocationList does not return the desierd information!
the usage is simple. Do something based on either the name of the last member in the tree or a value of an attribute that might exist on that ;ast member. ( FirstName in the exampe above )
Sergey Alexandrovich Kryukov 5-Apr-11 15:53pm    
Please see my update and our discussion with dasblinkenlight. I think all you can do is to work along these lines.

My I ask you how do you see your ultimate purpose?

To give you an idea: I developed a fully-fledged technology for Computer Algebra System. The latest and most advanced implementation was C#, and it was before Expression trees. I've done the expression trees from scratch, which has serious benefits (except compatibility with v.2.0 :-). I also can work as both symbolic and digital calculator.

Do you aim to develop something like that?

--SA

This is a hard problem - you need a decompiler to do it. There is a hint of the solution[^] that you can use as a template, but it involves a lot of coding.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 15:20pm    
I think this is just about Expression Trees. Please see my update to my Answer (after [EDIT]).
--SA
dasblinkenlight 5-Apr-11 15:30pm    
From his example it looks like he's not dealing with expression trees, because he gets Func[t] as an argument.
Sergey Alexandrovich Kryukov 5-Apr-11 15:33pm    
I see. Nevertheless, the intent is using expression tree, I think.
--SA
dasblinkenlight 5-Apr-11 15:39pm    
Yeah, that's very much the only option there is. The question is, how do you get one if all you've got is a compiled function. The guy at my link suggests decompiling into an expression tree. It would be a lot simpler if the OP could use an expression tree instead, because the code could both examine the tree and produce an executable expression by calling Compile().
Sergey Alexandrovich Kryukov 5-Apr-11 15:43pm    
I think you're right.
--SA
C#
var actual0 = value.Aggregate(new StringBuilder(), (x, y) => x.Insert(0, y)).ToString();
 
Share this answer
 
Comments
CHill60 12-Nov-16 21:26pm    
In what way does this answer the 5 year old question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900