Get the name(s) of interface methods strong typed

后端 未结 5 1734
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 02:11

i have an interface like this example:

Interface IRequest{

  List GetProfiles();
  void SetProfile (Profile p);
}

Now, in s

5条回答
  •  清酒与你
    2020-12-21 02:50

    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;
        }
    }
    

提交回复
热议问题