vendredi 2 décembre 2016

Get all concrete implementations of a generic interface

I found this answer to the first part of my question. However it is also returning the interface in my colleciton.

I am trying to get all concrete implementations of an interface

public interface IPermissionAccessDetails<T,TZ>
{
    List<PermissionAccessDetails<T,TZ>> AccessDetails { get; }
}

Here is the only concrete implementation (so far):

    public class BillingPermissionAccessDetails : IPermissionAccessDetails<BillingPermission, EBilling>
    {
        public List<PermissionAccessDetails<BillingPermission, EBilling>> AccessDetails => Config();
    }

Here is the code I am using to find all of the implementations (almost verbatim) from the answer referenced above.

 public static List<Type> GetImplementations(Type desiredType)
    {
        return Assembly.GetExecutingAssembly().GetTypes()
                    .Where(type => DoesTypeSupportInterface(type, desiredType)).ToList();
    }

    static bool DoesTypeSupportInterface(Type type, Type inter)
    {
        if (inter.IsAssignableFrom(type))
            return true;
        if (type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == inter))
            return true;
        return false;
    }


var allPermissionAccessTypeDetails = HelperMethods.GetImplementations(typeof(IPermissionAccessDetails<,>));

my problem is allPermissionAccessTypeDetails should only have 1 item in the collection - but it is also including the IPermissionAccessDetails type. How do I exclude that?





Aucun commentaire:

Enregistrer un commentaire