How can I get a value of a property from an anonymous type?

后端 未结 12 1196
南旧
南旧 2020-12-24 08:34

I have a datagrid populated by a Linq query. When the focused row in the datagrid changes I need to set a variable equal to one of the properties in that object.

I t

相关标签:
12条回答
  • 2020-12-24 09:11

    If you know what you are doing and are not afraid of getting runtime errors when your code changes, you could cast your row data as dynamic:

    var data = view.GetRow(rowHandle) as dynamic;  
    
    int clientId      = data.ClientID;
    string clientName = data.ClientName;
    int jobs          = data.Jobs
    

    No Compile-time verification. But it should work nicely.

    0 讨论(0)
  • 2020-12-24 09:13

    One of the problems with anonymous types is that it's difficult to use them between functions. There is no way to "name" an anonymous type and hence it's very difficult to cast between them. This prevents them from being used as the type expression for anything that appears in metadata and is user defined.

    I can't tell exactly which API you are using above. However it's not possible for the API to return a strongly typed anonymous type so my guess in that selectedObject is typed to object. C# 3.0 and below does not support dynamic access so you will be unable to access the property Id even if it is available at runtime.

    You will need to one of the following to get around this

    • Use reflection to grab the property
    • Create a full type and use that to populate the datagrid
    • Use one of the many hacky anonymous type casts

    EDIT

    Here's a sample on how to do a hack anonymous type cast

    public T AnonymousTypeCast<T>(object anonymous, T typeExpression) { 
      return (T)anonymous;
    }
    
    ...
    object obj = GetSomeAnonymousType();
    var at = AnonymousTypeCast(obj, new { Name = String.Empty, Id = 0 });
    

    The reason it's hacky is that it's very easy to break this. For example in the method where the anonymous type is originally created. If I add another property to the type there the code above will compile but fail at runtime.

    0 讨论(0)
  • 2020-12-24 09:17

    You could use the dynamic type to access properties of anonymous types at runtime without using reflection.

    var aType = new { id = 1, name = "Hello World!" };
    //...
    //...
    dynamic x = aType;
    Console.WriteLine(x.name); // Produces: Hello World!
    

    Read more about dynamic type here: http://msdn.microsoft.com/en-us/library/dd264736.aspx

    0 讨论(0)
  • 2020-12-24 09:20

    This may be wrong (you may not have enough code there) but don't you need to index into the row so you choose which column you want? Or if "Id" is the column you want, you should be doing something like this:

    var selectedObject = view.GetRow(rowHandle);
    _selectedId = selectedObject["Id"];
    

    This is how I'd grab the contents of a column in a datagrid. Now if the column itself is an anonymous type, then I dunno, but if you're just getting a named column with a primitive type, then this should work.

    0 讨论(0)
  • 2020-12-24 09:20

    You can loop through the properties of an anonymous type like this:

    var obj = new {someValue = "hello", otherValue = "world"};
    foreach (var propertyInfo in obj.GetType().GetProperties() {
        var name = propertyInfo.Name;
        var value = propertyInfo.GetValue(obj, index: null);
        ...
    }
    
    0 讨论(0)
  • 2020-12-24 09:21

    When I was working with passing around anonymous types and trying to recast them I ultimately found it easier to write a wrapper that handled working with the object. Here is a link to a blog post about it.

    http://somewebguy.wordpress.com/2009/05/29/anonymous-types-round-two/

    Ultimately, your code would look something like this.

    //create an anonymous type
    var something = new {  
      name = "Mark",  
      age = 50  
    };  
    AnonymousType type = new AnonymousType(something);
    
    //then access values by their property name and type
    type.With((string name, int age) => {  
      Console.Write("{0} :: {1}", name, age);  
    }); 
    
    //or just single values
    int value = type.Get<int>("age");   
    
    0 讨论(0)
提交回复
热议问题