简体   繁体   中英

Get the name(s) of interface methods strong typed

i have an interface like this example:

Interface IRequest{

  List<profile> GetProfiles();
  void SetProfile (Profile p);
}

Now, in some logging component, i don't have access to the object implementing the interface, but i want to use the names of the methods in the interface. i can of course type them as a string (copy the method name in a string), but i want to use them strong typed so i don't have to keep the method names and string in sync.

In pseudo code, i would do this:

string s= IRequest.GetProfiles.ToString()

Is this in a way possible?

EDIT:

Maybe i should call it: use the interface like it's an Enum string s= IRequest.GetProfiles.ToString()

You can achieve this in two ways:

//If you CAN access the instance
var instance = new YourClass(); //instance of class implementing the interface
var interfaces = instance.GetType().GetInterfaces();

//Otherwise get the type of the class
var classType = typeof(YourClass); //Get Type of the class implementing the interface
var interfaces = classType.GetInterfaces()

And then:

foreach(Type iface in interfaces)
{
    var methods = iface.GetMethods();

    foreach(MethodInfo method in methods)
    {        
        var methodName = method.Name;
    }
}

Sometimes you don't know the class name in advance, then you can use:

var interfaceType = typeof(InterfaceName);
var methods = interfaceType.GetMethods();

and then:

List<String> methodNames = new List<String>();
foreach(var method in methods)
{
    methodNames.Add(method.Name);
}

Be sure to check if the method is not null and contains at least one element.

This is the best I achieved, HTH:

using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DotNetSandbox
{
    // Subject interface to get names from
    interface IRequest
    {
        List<object> GetProfiles();
        void SetProfile(object p);
    }

    // Use case / Example:
    [TestClass]
    public class InterfaceNamesTests
    {
        [TestMethod]
        public void InterfaceNamesTest()
        {
            // Option 1 - Not strongly typed
            string name = typeof(IRequest).GetMethod("GetProfiles").Name;
            Console.WriteLine(name);    // OUTPUT: GetProfiles

            // Option 2 - Strongly typed!!
            var @interface = InterfaceNames<IRequest>.Create();

            Func<List<object>> func = @interface.GetProfiles;
            var name1 = func.Method.Name;
            Console.WriteLine(name1);   // OUTPUT: GetProfiles

            Action<object> action = @interface.SetProfile;
            var name2 = action.Method.Name;
            Console.WriteLine(name2);   // OUTPUT: SetProfile

            // Other options results with complex/unclear code.
        }
    }

    // Helper class
    public class InterfaceNames<T> : RealProxy
    {
        private InterfaceNames() : base(typeof(T)) { }

        public static T Create()
        {
            return (T)new InterfaceNames<T>().GetTransparentProxy();
        }

        public override IMessage Invoke(IMessage msg)
        {
            return null;
        }
    }
}

Your question is a bit hard to grok. I think you want to log the name of the instance class or method...

If you want strong typing, I think you need to use reflection. You could of course add a string name to each class that can be logged but that is brittle code and someone later on will hate you for it. You see this style in languages that don't easily support reflection but I'd recommend against that in a language like C#.

So on to a solution: Is your logging method called from inside the instance? If so, we can use reflection to get the name of the calling method and lots of other information.

If yes, 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's 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 stack overflow question might help: How I can get the calling methods in C#

This website has the code snippet that the two important lines are based on: http://www.csharp-examples.net/reflection-calling-method-name/

you can use this

nameof(Interface.Method)

it's simple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM