Constructing an Object through Reflection and "Object not set to an instance of an object"
When using reflection, you may get an error back that doesn't seem to fit the mold of reflection; I was dynamically executing an object through reflection, and I got the error "object not set to an instance of an object". The class I was trying to access had an internal constructor with some code in it. That code was failing, as there was a null reference, and therefore a null reference came back.
But it would appear there was an issue with the constructor call when executed (line 3) below. It just turned out there was a deeper issue.
ConstructorInfo
ctor = typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, System.Type.EmptyTypes, null);
if (ctor != null)
return (T)ctor.Invoke(new object[] { });
else
return default(T);