<p>I've got a custom handler applied to a class (using the Policy Injection Application Block in entlib 4) and I would like to know whether the input method is a property when Invoke is called.  Following is what my handler looks like.</p>

<pre><code>[ConfigurationElementType(typeof(MyCustomHandlerData))]
public class MyCustomHandler : ICallHandler
{
    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        if (input.MethodBase.IsPublic &amp;&amp; (input.MethodBase.Name.Contains("get_") || input.MethodBase.Name.Contains("set_")))
        {
            Console.WriteLine("MyCustomHandler Invoke called with input of {0}", input.MethodBase.Name);
        }
        return getNext().Invoke(input, getNext);
    }

    public int Order { get; set; }
}
</code></pre>

<p>As you can see from my code sample, the best way I've thought of so far is by parsing the method name.  Isn't there a better way to do this?</p>

[{'answer_id': 73747, 'author': 'Fredrik Kalseth', 'author_id': 1710, 'author_profile': 'https://Stackoverflow.com/users/1710', 'pm_score': 0, 'selected': False, 'text': '<p>You could check the IsSpecialName property; it will be true for property getters and setters. However, it will also be true for other special methods, like operator overloads.</p>\n'}, {'answer_id': 73779, 'author': 'Nathan Baulch', 'author_id': 8799, 'author_profile': 'https://Stackoverflow.com/users/8799', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m not familiar with that application block, but assuming that MethodBase property is of type System.Reflection.MethodBase, you could take a look at the IsSpecialName property.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.isspecialname.aspx" rel="nofollow noreferrer">System.Reflection.MethodBase.IsSpecialName on MSDN</a></p>\n'}, {'answer_id': 73908, 'author': 'ShuggyCoUk', 'author_id': 12748, 'author_profile': 'https://Stackoverflow.com/users/12748', 'pm_score': 3, 'selected': True, 'text': "<p>You can also check IsSpecialName is true. this will be true in a property (amongst other things)</p>\n\n<p>At the il level the methods are exposed as follows (using Environment.ExitCode as example):</p>\n\n<pre><code>.method public hidebysig specialname static int32 get_ExitCode() cil managed\n.method public hidebysig specialname static void set_ExitCode(int32 'value') cil managed\n</code></pre>\n\n<p>If you wanted to get fancy you could verify after extracting the name that said property exists but to be honest </p>\n\n<pre><code>if (m.IsSpecialName &amp;&amp; (m.Attributes &amp; MethodAttributes.HideBySig) != 0)) \n</code></pre>\n\n<p>as well as starts with get_ or set_ then you should be good even for people using nasty names (faking the hidebysig is easy enough, faking the IsSpecialName would be very tricky)</p>\n\n<p>Nothing is guaranteed though. Someone could emit a class with a set_Foo method that looked just like a real set method but actually wasn't a set on a read only property.\nUnless you check whether the property CanRead/CanWrite as well.</p>\n\n<p>This strikes me as madness for you though you aren't expecting deliberate circumvention. \nA simple utility/extension method on MethodInfo which did this logic wouldn't be too hard and including IsSpecialName would almost certainly cover all your needs.</p>\n"}, {'answer_id': 24102577, 'author': 'Paul Easter', 'author_id': 3583929, 'author_profile': 'https://Stackoverflow.com/users/3583929', 'pm_score': 0, 'selected': False, 'text': '<p>A couple of you mentioned using the "IsSpecialName" property of the MethodBase type. While it is true that the will return true for property "gets" or "sets", it will also return true for operator overloads such as add_EventName or remove_EventName. So you will need to examine other attributes of the MethodBase instance to determine if its a property accessor. Unfortunately, if all you have is a reference to a MethodBase instance (which I believe is the case with intercepting behaviors in the Unity framework) there is not real "clean" way to determine if its a property setter or getter. The best way I\'ve found is as follows:</p>\n\n<p>C#:</p>\n\n<pre><code>bool IsPropertySetter(MethodBase methodBase){\n     return methodBase.IsSpecialName &amp;&amp; methodBase.Name.StartsWith("set_");\n}\n\nbool IsPropertyGetter(MethodBase methodBase){\n     return methodBase.IsSpecialName &amp;&amp; methodBase.Name.StartsWith("get_");\n}\n</code></pre>\n\n<p>VB:</p>\n\n<pre><code> Private Function IsPropertySetter(methodBase As MethodBase) As Boolean\n\n      Return methodBase.IsSpecialName AndAlso methodBase.Name.StartsWith("set_")\n\n End Function\n\n Private Function IsPropertyGetter(methodBase As MethodBase) As Boolean\n\n      Return methodBase.IsSpecialName AndAlso methodBase.Name.StartsWith("get_")\n\n End Function\n</code></pre>\n'}, {'answer_id': 40128143, 'author': 'Tomaz Stih', 'author_id': 6286989, 'author_profile': 'https://Stackoverflow.com/users/6286989', 'pm_score': 0, 'selected': False, 'text': '<p>It is a bit late but other people will read this too. In addition to IsSpecialName and checking for the set_ prefix (operators have op_, event subscr./remov. has add_,remove_) you can check if method is a match to any of properties methods like this:</p>\n\n<pre><code>    bool isProperty = method.ReflectedType.GetProperties().FirstOrDefault(p =&gt; \n        p.GetGetMethod().GetHashCode() == method.GetHashCode() \n        || p.GetSetMethod().GetHashCode() == method.GetHashCode())!=null;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/73467', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6542/']