Constructor invocation returned null: what to do?
Posted
by strager
on Stack Overflow
See other posts from Stack Overflow
or by strager
Published on 2010-03-19T01:39:38Z
Indexed on
2010/03/19
1:51 UTC
Read the original article
Hit count: 280
c#
|reflection
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.
© Stack Overflow or respective owner