Constructor invocation returned null: what to do?
- by strager
I have code which looks like:
private static DirectiveNode CreateInstance(Type nodeType, DirectiveInfo info) {
var ctor = nodeType.GetConstructor(new[] { typeof(DirectiveInfo) });
if(ctor == null) {
throw new MissingMethodException(nodeType.FullName, "ctor");
}
var node = ctor.Invoke(new[] { info }) as DirectiveNode;
if(node == null) {
// ???;
}
return node;
}
I am looking for what to do (e.g. what type of exception to throw) when the Invoke method returns something which isn't a DirectiveNode or when it returns null (indicated by // ??? above).
(By the method's contract, nodeType will always describe a subclass of DirectiveNode.)
I am not sure when calling a constructor would return null, so I am not sure if I should handle anything at all, but I still want to be on the safe side and throw an exception if something goes wrong.