lundi 6 février 2017

C# How can I do code refactor into interface reflaction

I am doing reflection first time and I already read all provided solution, but actually I didn't found something familiar with my case and as I understand my issue isn't very standard.

At this moment I have a list where I have been adding validation class'es which inherit IValidation interface, but now I would like to create reflection where it will go explicitly trhu all class'es where inherit ISpecialCaseRequestValidation interface .

Here is my Validation class:

private static List<IValidation> _validations = new List<IValidation>();

        static Validations()
        {
        }

        public static bool IsValid(SpecialCaseRequest specialCase, out HttpResponseMessage response)
        {
            var result = _validations.FirstOrDefault(v => !v.IsValid(SpecialCase, out error));
            if (result != null)
            {
                return false;
            }
            return true;
        }

Now I have a error on !v.IsValid , because I allready moved IsValid from IValidation to ISpecialCaseRequestValidation interface. Here is my interfaces:

public interface ISpecialCaseRequestValidation: IValidation
    {
        bool IsValid(SpecialCaseRequest specialCase, out Error error);
    }
public interface IValidation
    {
    }

And here is my class'es which inherit ISpecialCaseRequestValidation interface.

public class NameValidation : ISpecialCaseRequestValidation
    {
        public bool IsValid(SpecialCaseRequest specialCase, out Error error)
        {
            if (condition)
            {
                return false;
            } 
            return true;
        }
    }
public class ExpirationDateTimeValidation : ISpecialCaseRequestValidation{}
public class AgeValidation : ISpecialCaseRequestValidation{}

How can I define with reflection these interfaces and it will go thru all class'es which inherit ISpecialCaseRequestValidation interfaces and lambda expression to pick IsValid method.





Aucun commentaire:

Enregistrer un commentaire