Check for Nulls with Reflection?

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

I have severaloverloaded methods that take a multiple combinations of
parameters, some which can't be null. I don't want to use
try...finally logic all over the place to catch the nulls. I want to
create some "catch all" functionality that I can add to every method
that will sweep through all parameters and tell me if any are null.

I think that is a good use for Reflection, but I am not sure how to
proceed. Any examples?

Thanks.
 
If a parameter can't be null, then enforce it.

public void SomeMethod(object a, object b) // b cannot be null
{
if (b == null)
throw new ArgumentNullException();

// do something with a and b
}

public void SomeMethod(object b) // b cannot be null
{
if (b == null)
throw new ArgumentNullException();
else
this.SomeMethod(null, b);
}


HTH;
Eric Cadwell
http://www.origincontrols.com
 
Hi localhost,

Thank you for posting in the community!

I think Eric's solution is the correct way to do parameter validation. You
may follow that to see if it resolves your problem.

If you have any questions or concerns, please feel free to post it in the
group. I am standing by to be of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Well, I wanted to get away from having to do a lot of if... statements
as well, because with 8+ parameters on lots of methods it gets
tedious. I was hoping there was some kind of catch-all trick I could
do to see if any passed parameters were null as soon as the method
signature was touched.
 
Hi localhost,

Thanks very much for your feedback.

I see your concern, as a substitution, you can use try...catch approach.
You can only apply try...catch in most-parameter overloading function, then
in other less-parameter overloading function, just invoke the
most-parameter overloading function, do like this:

public void func(object p1, object p2, object p3, object p4)
{
try
{
}
catch(Exception ex)
{
}
}

public void func(object p1, object p2, object p3)
{
object p4="default value";
func(p1,p2,p3,p4);
}

public void func(object p1, object p2)
{
object p4="default value";
object p3="default value";
func(p1,p2,p3,p4);
}

I think this may meet your need.

=============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi localhost ,

Does my workaround of using try...catch meet your need?

If you still have concern, please feel free to feedback.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top