Get the name of a Function

This seems like a ridiculous thing to be having an issue with but here it is. I need to get the name of the Function and Library to pass as a parameter to another function. I can get the Library with this.LibraryID but I can’t figure out how to get the name of the Function itself.

Try the below:

string functionName = this.GetType().Name;

If my function is named “Testing” then functionName will be “TestingImpl”

2 Likes

Don’t give me the solution, but here is a complete implementation:

Mistake
  //Helper Function (Func<T>) to get THIS Function ID
  Func<string> GetFunctionID = () =>
  {
      string suffix = "Impl";
      return this.GetType().Name.TrimEnd( suffix.ToCharArray() );
  };
  //Helper Function (Func<T>) to get THIS Function ID
  Func<string> GetFunctionID = () =>
  {
      string functionName = this.GetType().Name; 
      string suffix = "Impl";
      
      return functionName.EndsWith(suffix) ? functionName.Remove(functionName.LastIndexOf(suffix)) : functionName;
  };
3 Likes

That will work until the Function name ends in I, m, p, or l … or a combination of those. (TrimEnd(trimChars) doesn’t care about the order of the characters in the array)
Maybe better to use substring and chop off the last four characters.

1 Like

Ahh, yeah, I forgot. I actually ran into that the other day.
Forgot and just cut and pasted.

  //Helper Function (Func<T>) to get THIS Function ID
  Func<string> GetFunctionID = () =>
  {
      string functionName = this.GetType().Name; 
      string suffix = "Impl";
      
      return functionName.EndsWith(suffix) ? functionName.Remove(functionName.LastIndexOf(suffix)) : functionName;
  };
2 Likes

Looky Looky whats built in but not exposed:

string FunctionID = (string)this.GetType().GetProperty("FunctionID", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetGetMethod(nonPublic: true).Invoke(this, null);

Edit, gonna have to make an Epicor Idea for them to expose that.
Pain in the ass for logging.

3 Likes

Nice one Kevin - was annoying me hard coding the FunctionID into a code block that I re-use for writing into the Sys Monitor log - nice that it can by dynamic now!

1 Like