Browsed by
Tag: Extension methods

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); }…

Read More Read More