Your question is a little hard to understand. I think you want to write the class name of the instance or method ...
If you need strong typing, I think you need to use reflection. You could, of course, add a string name for each class that can be registered, but this is fragile code, and someone later hates you for it. You see this style in languages ββthat don't easily support reflection, but I would recommend against this in C #.
So, to the solution: Is the registration method called from within the instance? If so, we can use reflection to get the name of the calling method and a lot of other information.
If so, then something like this might work for you:
class MyRequest: IRequest { // other interface implementation details omitted public void SetProfiles(Profile p) { if(HasUglyPicture(p)) { MyLogger.LogError(String.Format( "User {0} update attempted with ugly picture", p.UserName) throw new Exception("Profile update failed due to ugly picture!"); } } class MyLogger : ILogger { // other logger details omitted public void LogError(string errorMsg) { // here where you get the method name // http://www.csharp-examples.net/reflection-calling-method-name/ StackTrace stackTrace = new StackTrace(); MyLogOutputStream.Write(stackTrace.GetFrame(1).GetMethod().Name); MyLogOutputStream.WriteLine(errorMsg); } }
This question may help: How to get calls in C #
There is a code snippet on this website in which two important lines are based on: http://www.csharp-examples.net/reflection-calling-method-name/
Keith hoffman
source share