-
Notifications
You must be signed in to change notification settings - Fork 80
Description
I have a scenario when an entity that is sent to Insert or Save methods is instantiated using reflection. In this scenario the type of the entity is not know at compile time, so I can't use generic version of Activator.CreateInstance() and must use Activator.CreateInstance() that returns an object. What happens is if I send this object down to MongoCollection operations, it is treated as "object" because MongoCollection is generic and expects type resolution at compile time. Then the following code in Save method raises NullReferenceException:
var helper = TypeHelper.GetHelperForType(typeof(T));
var idProperty = helper.FindIdProperty();
var id = idProperty.Getter(entity);
In the second line idProperty is null (because "object" type does not have an Id property), and next line raises exception.
My question is: are they reasons to use here and in some other places typeof(T) instead of entity.GetType()? The second form will ensure the type is resolved according its runtime information while the first one relies on static type information and may cause failures like in the scenario above.