.netcore of dynamic has Bug

波 姜 1 Reputation point
2022-09-29T07:22:13.737+00:00

namespace Command
{
public class ClassTest
{
public static dynamic getusers()
{

        return new { Id = 1, Name = "test" };  


    }  
}  

}

static void Main(string[] args)
{

        dynamic u = ClassTest.getusers();            
        Console.WriteLine(u.Id);  
        Console.ReadKey();           

    }   

------------------------
this bug------------------------------
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“'object' does not contain a definition for 'Id'”
245923-image.png

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,041 Reputation points
    2022-09-29T19:44:06.427+00:00

    dynamic access to anonymous objects is only supported by .net core. the 4.* runtime does not support

    0 comments No comments

  2. 波 姜 1 Reputation point
    2022-10-14T09:18:06.657+00:00

    this is .net core.

    0 comments No comments

  3. Bruce (SqlWork.com) 55,041 Reputation points
    2022-10-14T15:15:56.063+00:00

    Try net 6

    using System;  
      
    dynamic u = ClassTest.getusers();            
    Console.WriteLine(u.Id);  
      
    public class ClassTest  
    {  
        public static dynamic getusers()  
        {  
    		return new { Id = 1, Name = "test" };  
        }  
    }  
    

  4. 波 姜 1 Reputation point
    2022-10-19T02:09:44.777+00:00

    But the dynamic type does have bugs

    0 comments No comments