Click here to Skip to main content
15,881,763 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
let suppose
C#
var res = result.Select(s=> new {id=s.id,name=s.name}).ToList();
now i want pass this res in another method like func(res).
Please tell me that how can i get the type of this res or how i can get data from res.
Posted
Updated 2-Dec-11 9:34am
v2

You won't be able to pas the anonymous class as a variable since it is only used and valid with the context of this scope. You will need to create a concrete class or struct.
 
Share this answer
 
Comments
zeeShan anSari 2-Dec-11 15:39pm    
Can i use reflection?
Wonde Tadesse 2-Dec-11 15:48pm    
You won't able to get the type. The result return list of objects which have ID and Name fields.
Well, there is a "back-door" way to do this, using the new 'dynamic' keyword in .NET 4: but whether you should do this, or not, you need to carefully evaluate.

Consider:
List<int> newList = new List<int> {1, 2, 3};

// you could also use .ToList<int>() here
var result = newList.Select(itm => itm).ToList();

UseListMadeUsingVar(result);
And the function 'UseListMadeUsingVar is:
private void UseListMadeByVar(dynamic aList)
{
    Type type = aList.GetType();

    bool isListInt = aList is List<int>;
}

By declaring the input parameter as 'dynamic' you essentially avoid compile-time type checking.

Suggest you run the above code, put a break-point after the boolean variable 'isListInt is set, and examine the values of both 'type and 'isListInt to verify that you are getting a List<int> passed in.

And then, you need to think about what you might need to do with a late-bound result passed in this way: do you need to type-check before using ?
 
Share this answer
 
v5
Comments
Wonde Tadesse 3-Dec-11 17:51pm    
Good point. But what if the selection is made base on certain fields as if it is stated by the OP. What is going to be the anonymous type ? I believe it will end up to anonymous.
BillWoodruff 3-Dec-11 19:14pm    
Good question, Wonde, and possible necessity of complex parsing of the Type passed into the parameter 'aList is exactly why I expressed caution about using this technique.

If there is only a certain range of Types possible to be passed into 'aList, then, as my code example in 'UseListMadeByVar demonstrates, testing for specific type equivalence can be done easily: from there naturally comes the idea of some kind of if/then/else or switch structure that would do different things based on Type evaluation.

edit ... In this case the OP doesn't give us a clue as to what range of types might be created, but, for example, if we knew that in every case an IEnumerable<?> was created, that might allow a non-late-bound solution. Or if we knew that every Type composed and passed out was some "flavor" of perhaps related (by inheritance) Interface, and was passed as an instance of that Interface ... other possibilities ? ... end edit ...
Wonde Tadesse 3-Dec-11 19:26pm    
Thanks
Addy Tas 4-Dec-11 17:27pm    
Tricky stuff, i guess that if you are thinking about using the magic dynamic keyword; you should reconsidder your design. If you still end up at the same conclusion, talk to some one else... If you still think you need it, well ok as long as you keep it all in the same assembly. There is no way to track compatibility if you cross the assembly boundry.

Cheers, AT
BillWoodruff 4-Dec-11 17:37pm    
Yes, I think I emphasized in my answer exactly what the "tricky" we are using here is :) In actual real-world practice I think it's going to be rare to have this scenario occur; and I think the OP problably knows more about what possible types are being passed than we know from reading the question. And, in my edit in my response to Wonde, I tried to think of the probable ways you could use to get around this.

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