Dynamic combines anonymous type anonymous object arguments

Source: Internet
Author: User

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.

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.