I have this:
var Virtual = Property.GetValue(Entity, null);
Entity is being pulled from a repository using Entity Framework.
How can I determine if it is a single object, or a collection?
I tried checking the type, but wasn’t sure what to compare it to.
var VirtualType = Virtual.GetType();
Upon inspection with a debugger, Virtual is either a DynamicProxy of an instantiated single class, or a HashSet of an instantiated ICollection. If .GetType() is used on the DynamicProxy this is thrown:
Object reference not set to an instance of an object.
I also tried this:
var test = (IEnumerable<object>)Virtual;
if (test.Count() > 0)
which works on the collection, but fails on the single object.
I also tried this:
if (Virtual is Enumerable)
but it was false, even for the collection.
How can I discern the two through reflection?
just a quick guess from looking at your code.
this is the same as