Working with C# Anonymous Types

前端 未结 8 1541
感动是毒
感动是毒 2020-11-29 12:56

I am calling a method that returns a List variable that contains a c# Anonymous Type objects. For example:

List list = new List()         


        
      
      
      
8条回答
  •  孤街浪徒
    2020-11-29 13:37

    list back

    public static void Main()
    {
        foreach (object item in GetList())
        {
            var x = Cast(item, new { ContactID = 0, FullName = "" });
            Console.WriteLine(x.ContactID + " " + x.FullName);
        }
    
    }
    
    public static IEnumerable GetList()
    {
        yield return new { ContactID = 4, FullName = "Jack Smith" };
        yield return new { ContactID = 5, FullName = "John Doe" };
    }
    
    public static T Cast(object obj, T type)
    {
        return (T)obj;
    }
    
        

    提交回复
    热议问题