-1

Func func = a => a.Id;

I want to get Id from func. Not the value of Id.

func.invoke() give me the value of Id.

As Per suggested comment i tried this. But i got member as null in GetMemberInfo method. I want this approach. ie. i want store func in dictionary. then get that func from dictionary and get the name of Id .

public static MemberInfo GetMemberInfo<T, U>(Expression<Func<T, U>> expression)
    {
        var member = expression.Body as MemberExpression;
        if (member != null)
            return member.Member;

        throw new ArgumentException("Expression is not a member access", "expression");
    }


static void Main(string[] args)
        {
            Dictionary<int, Func<Soure.Employee, int>> testDir = new Dictionary<int, Func<Soure.Employee, int>>();
            testDir.Add(1, p => p.Id);
            var testDirValue = testDir[1];
            Expression<Func<Soure.Employee, int>> expr1 = mc => testDirValue(mc);
            MemberInfo member = Program.GetMemberInfo(expr1);
            Console.WriteLine(member.Name);
}
Hemant Malpote
  • 891
  • 13
  • 28
  • 1
    What is the question? Do you have a specific problem? Asking people to read your code to understand what you are asking usually results in chasing them away, especially if the code is so large it can't fit in a single screen – Panagiotis Kanavos Dec 19 '14 at 15:17
  • @ Panagiotis Kanavos : As i already wrote ' I know its hard describe what i want. Thats why i am giving running code please debug it. You can get what i want.' That why i had given the running code. So that anybody can understand it. – Hemant Malpote Dec 19 '14 at 15:20
  • 1
    @HemantMalpote **Noone** is going to run your code to decipher what you are asking. It seems as simple as 'I'm having trouble getting a value out of a dictionary' but to let me help you please ask a specific question. Don't get me wrong... it's excellent that you provided all the code necessary to duplicate your problem... I really appreciate that... I just can't understand what the problem you are trying to solve is. – Kevin Dec 19 '14 at 15:22
  • @ Kevin basically i am storing dictionary which contain key as Where To Map and Value as Func that will get execute and return value will be set to key. To execute func and assign value is later part. Now i want the property name which will be mapped to destination. And that value will be used in assigning values logic. If i wrote specified function i may need to explain a lot. Thats why i gave entire running code. So that anybody can run it . – Hemant Malpote Dec 19 '14 at 15:26
  • @HemantMalpote Please edit your question and add this information... however I still don't understand what the problem you are having is. – Kevin Dec 19 '14 at 15:31
  • @Kevin please debug the code. You can get what i want. – Hemant Malpote Dec 19 '14 at 15:33
  • Guys i had made question very simple. If i get this one i will manage with previews synervie. – Hemant Malpote Dec 19 '14 at 15:59
  • @HemantMalpote See if this is what you're looking for: http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression – D Stanley Dec 19 '14 at 16:02
  • There is a similar question on SO. Take a look at this [solution.][1] [1]: http://stackoverflow.com/questions/273941/get-property-name-and-type-using-lambda-expression – Oliver Dec 19 '14 at 16:03
  • @D Stanley and @Oliver your answer works in simple example. thanks for it. But my requirement is little different. For that i had added extra information in question please looked into it. – Hemant Malpote Dec 22 '14 at 07:03

1 Answers1

0

You have almost answered your own question. Let's assume MydeptId takes no parameters and returns an int, then:

Func<int> func = (a:SomeType) => a.MydeptId;

Then you invoke the function thus:

int i = func();

David Arno
  • 42,717
  • 16
  • 86
  • 131