C#.NET: Does object have method or property?

C#.NET: Does object have method or property?

I wanted to see if I could determine if an object had a certain property. I came across this post, that explained how to do this using reflection in .NET.

Here is what the little extension class looks like:

public static class Extensions
    {
        public static bool HasMethod(this object objectToCheck, string methodName)
        {
            var type = objectToCheck.GetType();
            return (type.GetMethod(methodName) != null);
        }

        public static bool HasProperty(this object objectToCheck, string propertyName)
        {
            var type = objectToCheck.GetType();
            return (type.GetProperty(propertyName) != null);
        }
    }

It will add 2 extension methods to the object class.

One thought on “C#.NET: Does object have method or property?

  1. Hi admin, i went through article. As we all know, .net is most popular programming language and it offer huge career prospects for talented professionals. It’s totally awesome, keep on updating your blog with such awesome information.

Leave a Reply

Your email address will not be published. Required fields are marked *