none
dynamic keyword bug RRS feed

  • Question

  • Hello.
    I think there is a problem in dynamic keyword.
    I will let code talk for me.

    (new MailController()).OurConfirmation(new { to = <a href="mailto:mailmail@mail.it" rel="nofollow">mailmail@mail.it</a>, Model = new { Cli = User.Identity.Name, DocNum = 50 } }).Deliver();
    

    And here is function definition

         public EmailResult OurConfirmation(dynamic dt)
            {
                string Dest = dt.to;
                From = "vincenzo@free-tech.com";
                To.Add(dt.to);
                Subject = "Prova";
                return Email("OurConfirmation", dt.Model);
            }
    


    It will throw exception, but it will work if i replace dt.to with Dest.
    Have a nice day.


    Il saggio è quello che sa di non sapere
    Monday, October 31, 2011 9:20 AM

Answers

All replies

  • Hi,

    I did a test as below:

     dynamic dt=new { to = "<a href=\"mailto:mailmail@mail.it\" rel=\"nofollow\">mailmail@mail.it</a>", Model = new { Cli = "testname", DocNum = 50 } };

                string dest = dt.to;

                List<string> To = new List<string>();

                To.Add(dest);

                To.Add(dt.to);

                foreach (string x in To)

                {

                    Console.WriteLine(x);

                }

    It works perfectly. Could you please provide some information about the exception you got?


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, November 2, 2011 5:43 AM
  • Hi VincentITA,

     

    According to your code, the underlying type of 'dt.to' is string.

    You should re-write your code as following to pass compilation. Maybe it is a paste error.

    (new MailController()).OurConfirmation(new { to = "<a href='mailto:mailmail@mail.it' rel='nofollow'>mailmail@mail.it</a>", Model = new { Cli = User.Identity.Name, DocNum = 50 } }).Deliver();

    While you debug it, please check if the value of 'dt.to' in OurConfirmation(...) method is correct as your input.

    If returned value is ok, your exception probably is probably caused by other issue such as Email(...) function.

    You can visit http://msdn.microsoft.com/en-us/library/dd264741.aspx and http://msdn.microsoft.com/en-us/library/dd264736.aspx to learn more about dynamic keyword.

     


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, November 2, 2011 7:31 AM
    Moderator
  • Yes it's a paste error, i'm sorry.
    I hope this screenshot can help you.

    It will work perfectly simply replacing dt.to with Dest.
    Thank you!


    Il saggio è quello che sa di non sapere
    Tuesday, November 8, 2011 7:13 AM
  • Hi Vincent,

     

    It seems that the issue is focused on the generics "To". How do you define it? I suppose you define it as List<dynamic>, am I right?

     

    If yes, I'm afraid it is a known limitation.Collection initializers attempt to bind to an available method "Add" on the newly constructed collection object, but there are known limitations, including extension methods and dynamic binding of interface methods. There are two ways to resolve it.

    1. Define "To" as List<string>.
    2. Cast the dt.to to a string object firstly as the sample I posted before.

    https://connect.microsoft.com/VisualStudio/feedback/details/534288/ilist-dynamic-cannot-call-a-method-add-without-casting


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Paul Zhou Tuesday, November 8, 2011 7:57 AM
    • Marked as answer by Paul Zhou Thursday, November 10, 2011 6:26 AM
    Tuesday, November 8, 2011 7:56 AM
  • Here is my Model anonymous object definition:
    (new MailController()).OurConfirmation(new { to = "email@email.it", Model = new { Cli = User.Identity.Name, DocNum = docnumd } }).Deliver();
    

    Il saggio è quello che sa di non sapere
    Thursday, November 10, 2011 7:39 AM