✅ Get property from generic type?
I have a method with a generic type. Two objects of different classes may be passed to it. How do I access a property in these objects (it has the same name in both objs)? Been trying with reflection but I'm getting a
Object reference not set to an instance of an object. C# GetProperty()
12 Replies
Do those objects share an interface?
If not, then that's how it should be solved
I can't touch these objects. They do share an interface but that interface does not implement the property I'm interested in.
I guess you could use pattern matching
You want to get the value of that property, right?
yes
it's a bool
let's see
also, why does this not work for me?
typeof(T).GetProperty(parameter).GetValue(currentItem);
I've tried similar stuff.
¯\_(ツ)_/¯
Stack Overflow
Get property of generic class
I have a generic class, and an object value where obj.GetType().GetGenericTypeDefinition() == typeof(Foo<>).
class Foo<T>
{
public List<T> Items { get; set; }
}
How do I get...
thank you, this is useful
you need BindingFlags
I will look into that
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.