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”
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;
};
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.
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;
};
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.
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!
Are we worried about the warning that pops up when saving the function?
“These warnings will become errors in the future…”
warning ECF1002: The ‘System.Reflection.PropertyInfo.GetGetMethod(bool)’ method cannot be called.
@klincecum did you ever raise an Epicor Idea for the FunctionID to be exposed? I reached out directly to @Epic_Santiago and he agreed it was a nice easy thing to do just needed an Idea raising.
I can raise one now if not - cheers.
Can’t remember. Pop it.
Only two votes