I have a method that should return identifiers from a list. I usually use reflection for this task (I cannot use the general method, since classes are usually POCOS that do not use an interface or base class, and I cannot change them). However, I thought of a new dynamic key and wanted to try it.
However, my problem is that dataSource [index] returns an object. Well at runtime it is ensured that the iself object belongs to my own classes and has the id property. But I suppose, because the method returns an object, I get a RumtineBinderException at runtime when accessing current.id
public List<int> GetItemIds() { var result = new List<int>(); var dataSource = GetDataSource();
Is there a way to achieve what I want, or do I need to go back to thinking in order 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 the class library, maybe this is the reason. 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); }
work.
source share