6

I have a method that should return the ids from a List. Usually I would use reflection for this task (I cannot use a generic method since the classes are usually POCOS that don't share an interface or a base class and I can't modify them). However, I thought about the new dynamic keyword and wanted to try this.

However my problem is that dataSource[index] returns an object. Well at runtime it is ensured that the object isself is on of my own classes and has a id property. But I suppose because the method returns an object, I get a RumtineBinderException at runtime while accessing current.id

public List<int> GetItemIds()
{

    var result = new List<int>();
    var dataSource = GetDataSource(); // returns an List<Object>

    for (int i = 0; i <= dataSource.Count - 1; i++)
    {
        dynamic current = dataSource[i];
        int id = current.Id;  // throws RuntimeBinderException: Object has no definition for id
    }

    return result;
}

Is there a way to achive what I want or do I have to go back to reflection to get the id property?

Update:

current.GetType() returns object
current.GetType().GetProperties() returns a TargetInvocationException

My Pocos live in my main project (VB.net) but this method is in a class libary, maybe that is the cause. However:

object current = dataSource[i];
PropertyInfo prop = current.GetType().GetProperty("id", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (prop != null)
{
    int id = (int)prop.GetValue(current, null);
}

works.

4
  • what is current.GetType()? Is it the type you expect? Commented Jul 14, 2011 at 15:32
  • There is something wrong here....using dynamic in the example above should make it work if the runtime type has an Id property... Commented Jul 14, 2011 at 15:34
  • It should work - can you update the post with the information for current.GetType(), and current.GetType().GetProperties()? Commented Jul 14, 2011 at 16:43
  • I updated my question. current.GetType().GetProperties() throws an exception. but in the watch window I see all my properties. Commented Jul 15, 2011 at 6:47

2 Answers 2

1

I believe you may need to define the return type of "GetDataSource()" as "List<dynamic>".

Of course, as stated in the comments, the objects must have the property "id" defined.

Sign up to request clarification or add additional context in comments.

1 Comment

While my guess is that you wouldn't break anything by GetDataSource() as List<dynamic> because it technically compiles into List<object> with type checking, it just looks bad because Lists aren't covariant because you couldn't do same for example if List<SomethingElse> was returned. Either way it's not the solution because you are still telling the compiler to treat the returned object of the index as dynamic so there should be no difference.
1

C# is case sensitive including when you use the dynamic keyword. your call is int id = current.Id; but you talk about the property being lowercase id, and your reflection call looks case insensitive. The dynamic keyword should have no problem calling public instance properties even across assembly boundaries, since it says the method is not found my best guess is that you need to be using int id = current.id;

1 Comment

In my real world application the property is lowercase and I used ´current.id´ to access it to. (but I mixed it up in the example code). However, even if I see the property in the watch window accessing it by code failes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.