0

I implement a project and I have a Class for members, but each member has a method of calculating his financial reward.

public class Person : EntityValidator
{
    [DisplayName("Name")]
    [Required(ErrorMessage = "{0} *")]
    [RegularExpression(@"^[\u0600-\u06ff\s]+$", ErrorMessage = " ")]
    public string Name { get; set; }

    [Required(ErrorMessage = "{0} ")]
    [DataType(DataType.CreditCard)]
    [RegularExpression(@"^\d{14}$",
        ErrorMessage = "ا")]
    public string NationalId { get; set; }

    [DataType(DataType.PhoneNumber)]
    [Phone]
    public string Phone { get; set; }
    public ICollection<Salary> Salarys { get; set; }
    public virtual MemberShip MemberShip { get; set; }

So I created an interface to the calculation methods as shown in the code.

interface IArithmetiCoperation
{
    decimal MonetaryAdvantage();
    double DurationBetween();
}

Now first: I need the classes that inherit from this interface appear inside the combo, and when this class is identified, implement its methods and give me the results related to it. Provided that the names of the categories appear with names expressing their character and not their name inside the code.

Second: Is there a better suggestion than the interface method?

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
  • A class does not [inherit](https://learn.microsoft.com/dotnet/csharp/tutorials/inheritance) an interface. A class can inherit from another single class (or extends thsi class), and can [implement](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/interface) some [interfaces](https://stackoverflow.com/questions/67545272/view-interface-list-in-combo-box). An interface is a "never null pointer" to an object instance of a type (a virtual reference over the real reference, that is the same in IL in fact, just compiler twirl). –  May 15 '21 at 10:04
  • 1
    [What is the difference between an interface and a class, and why I should use an interface when I can implement the methods directly in the class?](https://stackoverflow.com/questions/10914802/what-is-the-difference-between-an-interface-and-a-class-and-why-i-should-use-an/58174007) –  May 15 '21 at 10:09

1 Answers1

0

I need the classes that inherit from this interface appear inside the combo, and when this class is identified, implement its methods

For this you may want to consider using Reflection. Reflection allows you to work with your code like you would your data(in a very over generalized sense)

The general way you could find all the items that implement an interface would be to

  1. Get the assembly you want to search in
  2. Get all the types in that assembly
  3. Search those types and get the ones that implement that interface.

You can get an object's assembly with typeof(ObjectThatYouWantToGetAssemblyOf).Assembly. You can use that to search it for types.

You can use the Assembly.GetTypes() to get all the types that assembly defines/implements.

And finally you can just run a LINQ query on the types to search for the ones that implement the interface like: Types.Where(type => type.GetInterface("InterfaceToLookFor") != null )

Put that all together and you can put them in a list and bind that to a list box!

Here's very simple snippet to get you started

namespace StackOverflowQuestions
{
    public class Program
    {
        public static void Main()
        {
            var classesThatImplementIHuman = typeof(Program).Assembly.GetTypes().Where(x => x.GetInterface("IHuman") != null);

            Console.WriteLine(string.Join(", ", classesThatImplementIHuman));

            Console.ReadLine();
        }
    }

    public interface IHuman { }

    public interface IAnimal { }

    public class Dog : IAnimal { }

    public class Cousin : IHuman { }

    public class Neighbor : IHuman { }

    public class Person : IHuman { }

    public class Friend : IHuman { }
}
DekuDesu
  • 2,224
  • 1
  • 5
  • 19