简体   繁体   中英

Working with C# Anonymous Types

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

List<object> list = new List<object>();
foreach ( Contact c in allContacts ) {
    list.Add( new {
        ContactID = c.ContactID,
        FullName = c.FullName
    });
}
return list;

How do I reference this type properties in the code I am working on like for example

foreach ( object o in list ) {
    Console.WriteLine( o.ContactID );
}

I know that my sample is not possible, I have only wrote that way to say that I need to identify each property of the anonymous type exactly.

Thanks!

Solution :

Not just one of the answer is correct and/or suggest a working solution. I have ended up to using Option 3 of Greg answer. And I learned something very interesting regarding the dynamic in .NET 4.0!

You can't return a list of an anonymous type, it will have to be a list of object . Thus you will lose the type information.

Option 1
Don't use an anonymous type. If you are trying to use an anonymous type in more than one method, then create a real class.

Option 2
Don't downcast your anonymous type to object . (must be in one method)

var list = allContacts
             .Select(c => new { c.ContactID, c.FullName })
             .ToList();

foreach (var o in list) {
    Console.WriteLine(o.ContactID);
}

Option 3
Use the dynamic keyword. (.NET 4 required)

foreach (dynamic o in list) {
    Console.WriteLine(o.ContactID);
}

Option 4
Use some dirty reflection.

foreach ( var o in list ) {
    Console.WriteLine( o.ContactID );
}

this will work only if list is IEnumerable<anonymous type> , like this:

var list = allContacts.Select(c => new {
        ContactID = c.ContactID,
        FullName = c.FullName
    });
}

but you can't return anonymous types, because you must define return type (you can't return var ) and anonymous types can't have names. you should create non-anonymous type if you with to pass it. Actually anonymous types should not be used too much, except for inside of queries.

If you have a method like this:

  List<object> GetContactInfo() {
    List<object> list = new List<object>();
    foreach ( Contact c in allContacts ) { 
        list.Add( new { 
            ContactID = c.ContactID, 
            FullName = c.FullName 
        }); 
    } 
    return list;
  }

You shouldn't really do this, but there's a very ugly and not future-proof technique that you can use:

  static T CastByExample<T>(object target, T example) {
    return (T)target;
  } 

  // .....

  var example = new { ContactID = 0, FullName = "" };
  foreach (var o in GetContactInfo()) {
    var c = CastByExample(o, example);
    Console.WriteLine(c.ContactID);
  }

It relies on the fact (which can change!) that the compiler reuses anonymous types for types that have the same "shape" (properties names and types). Since your "example" matches the "shape" of the type in the method, the same type is reused.

Dynamic variables in C# 4 are the best way to solve this though.

You cannot do this with anonymous types. Just create a Contact class/struct and use that.

List<object> list = new List<object>();
foreach ( Contact c in allContacts ) {
    list.Add( c );
}

Then you can do this:

foreach ( var o in list ) {
    Console.WriteLine( o.ContactID );
}

...or this:

foreach ( object o in list ) {
    Console.WriteLine( ((Contact)o).ContactID ); //Gives intellisense
}

Of course you should in that case just do create a Contact list instead of an object list:

List<Contact> list = new List<Contact>();
foreach ( Contact c in allContacts ) {
    list.Add( c );
}

EDIT: Missed essential part of the question. Now fixed.
EDIT: Changed answer yet again. See above.

I know I'm late to the party but I researching somthing else and found this article which answers your question.

It is possible to cast the list of objects back into the anonymous type.

    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<object> GetList()
    {
        yield return new { ContactID = 4, FullName = "Jack Smith" };
        yield return new { ContactID = 5, FullName = "John Doe" };
    }

    public static T Cast<T>(object obj, T type)
    {
        return (T)obj;
    }

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<object> GetList()
{
    yield return new { ContactID = 4, FullName = "Jack Smith" };
    yield return new { ContactID = 5, FullName = "John Doe" };
}

public static T Cast<T>(object obj, T type)
{
    return (T)obj;
}

用每个构造的var in替换对象可能有效

i would use

allContacts
 .Select(c => new { c.ContactID, c.FullName })
 .ToList()
 .ForEach(c => {
   ...do stuff;
 });

then you dont need to declare at all. i believe that less declaration, less semi-colon leads to less bug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM