0

Is there a way to do this:

var methodToDoStuffTo = typeof (FancyClass).GetMethod("MethodName").Name;

Without relaying on the string "MethodName"?

What I want is something like this:

var methodToDoStuffTo = typeof (FancyClass).GetMethod(FancyClass.MethodName).Name;

So I can be sure that there is no unexpected error when I rename my method MethodName.

For reasons I can't simple update my enviroment to c# 6, so no nameof().

I try and give a reason why I'am doing this:

I (have to) use one Authorization Attribute on several different Methods. Depending on from which method the Attribute was 'called', the code has to do slitly different stuff.

That's why I can't / don't want to use differnet Attributes for each Method.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Jan
  • 13
  • 4
  • 2
    If you're using Visual Studio 2015, check out the "nameof" method: GetMethod(nameof(FancyClass.MethodName)) – RB. Mar 12 '16 at 11:36
  • 1
    It might help to explain a bit more why you are trying to do this; for example I cannot understand *why* you might want to do something like this at all :) – Stephen Byrne Mar 12 '16 at 11:38
  • I agree with @StephenByrne, for example you may be better off passing round an expression rather than using reflection, that would give you your compile time checking. – DavidG Mar 12 '16 at 11:40
  • But if it's only slightly different stuff, then shouldn't this be an implementation detail; i.e. your code that is executing in the Attribute (or whatever) deals with the current method by interface and calls a common method. If the parameters to the methods vary by use case then it hints that you do need to specialise several Attributes, even if you don't want to :) – Stephen Byrne Mar 12 '16 at 20:55

1 Answers1

2

Depending on from which method the Attribute was 'called', the code has to do slitly different stuff

You should never do that. Letting magic stuff happen based on naming conventions is in most application code a bad idea that will lead to unexpected side-effects.

Just add a constructor parameter to the attribute:

public class YourAttribute : Attribute
{
    private bool doSomethingDifferent;

    public YourAttribute(bool doSomethingDifferent = false)
    {
        _doSomethingDifferent = doSomethingDifferent;
    }
}

And apply it as such:

public class AttributeApplication
{
    [YourAttribute]
    public void NormalMethod()
    {
    }

    [YourAttribute(doSomethingDifferent: true)]
    public void MethodSomewhatDifferent()
    {
    }
}

But if you really want a type-safe GetMethod(), you could create an extension method with a MethodCallExpression, as explained in Get the name of a method using an expression.

The syntax will then become something like this:

var methodToDoStuffTo = typeof(FancyClass).GetMethod(c => c.MethodName()).Name;

But again, that approach should not be chosen lightly.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    Of course it is a stupid idea to do something like this! Honestly I just didn't think of using the constructor. Thanks alot! :) – Jan Mar 12 '16 at 12:08