Click here to Skip to main content
15,905,028 members
Home / Discussions / C#
   

C#

 
GeneralRe: Verify Email problem with AdminControler.cs Pin
Afzaal Ahmad Zeeshan21-Feb-17 6:06
professionalAfzaal Ahmad Zeeshan21-Feb-17 6:06 
GeneralRe: Verify Email problem with AdminControler.cs Pin
John Nederveen21-Feb-17 8:20
John Nederveen21-Feb-17 8:20 
QuestionHow To Use Windows Runtime Component in WinForm App Pin
Django_Untaken20-Feb-17 7:14
Django_Untaken20-Feb-17 7:14 
AnswerRe: How To Use Windows Runtime Component in WinForm App Pin
Afzaal Ahmad Zeeshan20-Feb-17 11:23
professionalAfzaal Ahmad Zeeshan20-Feb-17 11:23 
GeneralRe: How To Use Windows Runtime Component in WinForm App Pin
Django_Untaken20-Feb-17 20:20
Django_Untaken20-Feb-17 20:20 
GeneralRe: How To Use Windows Runtime Component in WinForm App Pin
Afzaal Ahmad Zeeshan20-Feb-17 22:46
professionalAfzaal Ahmad Zeeshan20-Feb-17 22:46 
QuestionError Could not open form interface design in C# ? Pin
Member 245846719-Feb-17 22:59
Member 245846719-Feb-17 22:59 
AnswerRe: Error Could not open form interface design in C# ? Pin
Eddy Vluggen19-Feb-17 23:05
professionalEddy Vluggen19-Feb-17 23:05 
AnswerRe: Error Could not open form interface design in C# ? Pin
OriginalGriff19-Feb-17 23:23
mveOriginalGriff19-Feb-17 23:23 
GeneralRe: Error Could not open form interface design in C# ? Pin
Rob Philpott20-Feb-17 5:02
Rob Philpott20-Feb-17 5:02 
GeneralRe: Error Could not open form interface design in C# ? Pin
OriginalGriff20-Feb-17 5:07
mveOriginalGriff20-Feb-17 5:07 
QuestionAccessing the parameters of a method assigned to a delegate Pin
Jörgen Andersson19-Feb-17 10:47
professionalJörgen Andersson19-Feb-17 10:47 
AnswerRe: Accessing the parameters of a method assigned to a delegate Pin
Dave Kreskowiak19-Feb-17 11:33
mveDave Kreskowiak19-Feb-17 11:33 
GeneralRe: Accessing the parameters of a method assigned to a delegate Pin
Jörgen Andersson19-Feb-17 21:09
professionalJörgen Andersson19-Feb-17 21:09 
AnswerRe: Accessing the parameters of a method assigned to a delegate Pin
Gerry Schmitz19-Feb-17 15:06
mveGerry Schmitz19-Feb-17 15:06 
GeneralRe: Accessing the parameters of a method assigned to a delegate Pin
Jörgen Andersson19-Feb-17 21:04
professionalJörgen Andersson19-Feb-17 21:04 
GeneralRe: Accessing the parameters of a method assigned to a delegate Pin
Gerry Schmitz20-Feb-17 8:25
mveGerry Schmitz20-Feb-17 8:25 
AnswerRe: Accessing the parameters of a method assigned to a delegate Pin
Richard Deeming20-Feb-17 2:44
mveRichard Deeming20-Feb-17 2:44 
With a compiled delegate, it's going to be almost impossible.

In your specific example, the Function.Target property will return an instance of an anonymous type, with a field for each captured variable, and one for the delegate:
C#
Function.Target == new
{
    Val1 = 3,
    Val2 = 4,
    Function = Func<int>
}

However, without reading the IL of Function.Method, you have no way of knowing which field corresponds to which argument.

Things get even more interesting if the anonymous closure type is shared between multiple closures, or you're not using a closure at all.

You might be able to extract the information if you use an Expression<Func<T>>[^] instead of a Func<T>. In simple cases, the following seems to work:
C#
Expression<Func<int>> expr = () => Class1.GetValue(Val1, Val2);
var body = (MethodCallExpression)expr.Body;
var parameters = body.Method.GetParameters();
for (int index = 0; index < body.Arguments.Count; index++)
{
    var arg = (MemberExpression)body.Arguments[index];
    var instance = arg.Expression == null ? null : ((ConstantExpression)arg.Expression).Value;
    var field = (FieldInfo)arg.Member;
    object value = field.GetValue(instance);
    Console.WriteLine("{0} = {1}", parameters[index].Name, value);
}

/*
Output:
First = 3
Second = 4
*/

However, this code is rather fragile, and likely to break if you pass in any "interesting" expressions.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Accessing the parameters of a method assigned to a delegate Pin
Jörgen Andersson28-Feb-17 20:34
professionalJörgen Andersson28-Feb-17 20:34 
QuestionWindows form application connectivity Pin
Member 1301073119-Feb-17 8:54
Member 1301073119-Feb-17 8:54 
AnswerRe: Windows form application connectivity Pin
NotPolitcallyCorrect19-Feb-17 9:47
NotPolitcallyCorrect19-Feb-17 9:47 
AnswerRe: Windows form application connectivity Pin
Richard Deeming19-Feb-17 9:48
mveRichard Deeming19-Feb-17 9:48 
GeneralRe: Windows form application connectivity Pin
Member 1301073120-Feb-17 1:48
Member 1301073120-Feb-17 1:48 
GeneralRe: Windows form application connectivity Pin
Richard Deeming20-Feb-17 1:53
mveRichard Deeming20-Feb-17 1:53 
GeneralRe: Windows form application connectivity Pin
Member 1301073120-Feb-17 4:23
Member 1301073120-Feb-17 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.