Get the name (s) of interface methods

I have an interface similar to this example:

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

Now, in some protocol components, I do not have access to the object that implements the interface, but I want to use the method names in the interface. I can, of course, print them as a string (copy the method name to a string), but I want to use them strong, so I do not need to keep the method names and string in sync.

In pseudo code, I would do the following:

 string s= IRequest.GetProfiles.ToString() 

Is it possible?

EDIT:

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

+4
source share
4 answers

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; } } 
+6
source share

Sometimes you do not 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 to see if the method is invalid and contains at least one element.

+2
source share

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/

+1
source share

This is the best I have achieved, hh:

 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; } } } 
+1
source share

All Articles