First of all, I generally rarely use the Dynamic keyword (class), because after all, is implemented by reflection, so for performance is still a lot of disadvantage ( Note: Because the heart is not the bottom, check some information that dynamic does not seem to be realized by reflection, and its performance is much higher than the direct reflection , as for why, I do not understand, I hope you know the message can tell me a bit! Thank you ^_^) I do not know why the DLR can achieve the same implementation as reflection, but the code, performance than the latter introduction, efficient!!
I'm just here to sit down and record.
- Several ways to pass parameters to anonymous objects (types)
The code is as follows:
class Program
{
static void Main (string [] args)
{
int [] array = new int []{
3,4,5,6,7,8,9
};
var data = from R in Array
where r = = 8
selec T new
{
ID = R
};
GetNumber (data);
GetNumber1 (data. First ()); //Go directly to an anonymous class
}
static void GetNumber (dynamic data)
{
Console. WriteLine (dy. First (). ID); //error because DY does not recognize first () This method of expansion under LINQ
Console. ReadLine ();
}
Static void GetNumber1 (dynamic data)
{
Console. WriteLine (dy.id); //Correct
Console. ReadLine ();
}
static void GetNumber2 (dynamic data)
{
Strong to dynamic object
IEnumerable<dynamic> dy = data;
Console. WriteLine (dy. First (). ID); //correct to correctly identify first () in LINQ
Console. ReadLine ();
}
}
Summary: 1. Dynamic objects do not support extension methods
2. An anonymous function (lambda expression) cannot be used as a parameter to a dynamic method call, so LINQ is not used for dynamic objects
3. Avoid using dynamic to avoid, but in some cases, can be used (such as the ability to significantly reduce the amount of code, improve code clarity, code structure, etc.), sometimes the point of performance is not so important ^_^
OVER!!! Go to bed
Dynamic combines anonymous type anonymous object arguments