Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know if you might be looking for something like this. This uses an attribute that you can flag a method with whether or not it is implemented. Next I added an extension method to the interface to allow for checking if ithe method is implemented. Finally, the code will allow you to ask an object if the method is implemented. I don't like this but it might be what you are looking for. </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication1 { public static class Program { static void Main(string[] args) { EmployeeA empA = new EmployeeA(); if (empA.IsImplemented("TestMethod")) empA.TestMethod(); EmployeeB empB = new EmployeeB(); if (empB.IsImplemented("TestMethod")) empB.TestMethod(); Console.ReadLine(); } public static bool IsImplemented(this IEmp emp, string methodName) { ImplementedAttribute impAtt; MethodInfo info = emp.GetType().GetMethod(methodName); impAtt = Attribute.GetCustomAttribute(info, typeof(ImplementedAttribute), false) as ImplementedAttribute; return (impAtt == null) ? true : impAtt.Implemented; } } public class EmployeeA : IEmp { #region IEmp Members [Implemented(false)] public void TestMethod() { Console.WriteLine("Inside of EmployeeA"); } #endregion } public class EmployeeB : IEmp { #region IEmp Members [Implemented(true)] public void TestMethod() { Console.WriteLine("Inside of EmployeeB"); } #endregion } public class ImplementedAttribute : Attribute { public bool Implemented { get; set; } public ImplementedAttribute():this(true) { } public ImplementedAttribute(bool implemented) { Implemented = implemented; } } public interface IEmp { void TestMethod(); } } </code></pre> <p>EDIT: After original author reworded question, you definitely just want to implement the interface guranteeing the method does exist. I will leave above code for curiosity sake.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload