-1

I have an Addin in which I do calls to public method within a DLL which is mine. I have a very weird scenario where I need dynamically (not hardcoded) to get the name of the DLL method I am being calling so I am trying to use the nameof funtion.

The problem is that it is only supported from c# 6.0 so is there any alternative to do the same in C# 5.0 (NET Framework 4.5)?

Example:

ExecuteMethod(() => myCore.SomeMethod1(param1, param2), nameof(myCore.SomeMethod1));
Willy
  • 9,848
  • 22
  • 141
  • 284
  • 1
    will this work for you, did you check it ? https://stackoverflow.com/questions/31261955/nameof-expression-in-net-framework-4/31262225. Reflection is the primary solution for earlier versions. – Anand Sowmithiran Jan 10 '22 at 12:40
  • 6
    `nameof` is only static to begin with. This is equivalent to simply writing `"SomeMethod1"` as a constant (the benefit being that things like refactoring will not break it), so no matter the version of C#, if you need to determine things at runtime reflection is necessary. – Jeroen Mostert Jan 10 '22 at 12:42
  • @JeroenMostert if i simply write "SomeMethod1" I will need to modify again if the method name changes so i want something dynamic in runtime. – Willy Jan 10 '22 at 13:07
  • 1
    If the method name changes *how*? What you've written *right now* contains a static reference to `myCore.SomeMethod1`. Of course there are ways to build the `ExecuteMethod` call dynamically, but what you have written now isn't it. To put it another way, `nameof` isn't your immediate problem, it's figuring out how to identify the method in the first place. It could be a `MethodInfo` parameter or `Func` or a string you have to reflect over, but one way or another information will have to be passed that isn't static. – Jeroen Mostert Jan 10 '22 at 13:19
  • @JeroenMostert I'd say "constant" rather than "static" - i.e. it's determined **at compile-time** – Camilo Terevinto Jan 10 '22 at 13:44
  • 1
    btw; if this `ExecuteMethod` isn't used with massive frequency (i.e. we can afford a little overhead), you could use `Expression` instead of `YourDelegateTypeHere` (i.e. `Expression>`) and inspect the expression tree to get the method name, and compile+call the expression to invoke it. Then you don't even need the second parameter. – Marc Gravell Jan 10 '22 at 14:23

2 Answers2

0

Action (like every other delegate type) has a property Method, which is a MethodInfo, containing the information you can also get via refelection. Hence you could use

ExecuteMethod(() => myCore.SomeMethod1(param1, param2), new Action<string,string>(myCore.SomeMethod1).Method.Name);

(assuming that both parameters are a string. Change the generic type parameters if the parameters are of a different type)

Online demo: https://dotnetfiddle.net/l9nOlX

SomeBody
  • 7,515
  • 2
  • 17
  • 33
  • It seems interesting but in my case someMethod1 has custom types (classes) for parameters and also a custom return type (another class). I cannot figure out how to do it in my concrete case. – Willy Jan 10 '22 at 13:12
  • Use a Func instead? – SomeBody Jan 10 '22 at 13:15
0

The easiest solution here is to simply: use C# 6 (or later). In the csproj, this is the <LangVersion> property. For example:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <LangVersion>6</LangVersion> <!-- or higher -->
        <!-- ... -->
    </PropertyGroup>
    <!-- ... -->
</Project>

As long as your build tools are up to date, you can use later C# versions with earlier .NET Framework versions - the two things are not strictly coupled.

Note that some language features require additional types to exist (init-only members, for example), and some features require specific runtimes (default interface implementations, for example) - however, if the compiler detects that you're trying to use a C# feature that is missing something it needs: it will just tell you. It doesn't need anything to use nameof.

Note that the IDE now deliberately limits your ability to select unusual language versions, and it isn't strictly supported - but: it works just fine in every interesting realistic scenario. I'm not "in the loop" on the reasons for that, but I speculate that this is simply to reduce support overheads, i.e. questions - meaning: it becomes much simpler to describe what features are available.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900