vendredi 2 décembre 2016

Create concrete class from type and generic interface

Need some help on this. I am trying to get the concrete implementation of a class that implements a generic interface.

The challenge is the interface uses two types as parameters.

The first thing I do is get all types that implement the interface:

public interface IPermissionAccessDetails<T,TZ> : IPermissionAccessDetailsTypes
{

    List<PermissionAccessDetails<T, TZ>> GetAccessDetails();
}

public interface IPermissionAccessDetailsTypes
{
    Type ModulePermissions { get; }
    Type PermissionDetails { get; }
}

Get all types:

var allPermissionAccessTypeDetails = HelperMethods.GetImplementations(typeof(IPermissionAccessDetailsTypes));

the Helper Method (found this on SO):

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 (type.IsInterface)
            return false;
        if (inter.IsAssignableFrom(type))
            return true;
        if (type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == inter))
            return true;
        return false;
    }

Here is one implementation of IPermissionAccessDetails

public class BillingPermissionAccessDetails : IPermissionAccessDetails<BillingModulePermissions, EBilling>
{

    public Type ModulePermissions => typeof(BillingModulePermissions);
    public Type PermissionDetails => typeof(EBilling);

    public List<PermissionAccessDetails<BillingModulePermissions, EBilling>> GetAccessDetails()
    {
        return Config();
    }
}

What I am trying to do is get all types that implement the interface, then call get the results from GetAccessDetails on each implementation.

Each implementation if IPermissionAccessDetails will use different parameter types and those types will always be some form of Enum.

For example here are the BillingModulePermissions and EBilling enums:

 public enum BillingModulePermissions
{
    [PermissionData(id : "86ba80e1-39c3-42f4-aa4c-9f87ac06fd63",  displayName : "Billing Menu", description : "View Billing Menu Item")]
    BillingMenu,

    [PermissionData(id : "63dabe62-baa4-4883-9a7d-5fe8932cfaae", displayName : "Monthly Billing", description : "Functionality to view billing summary information by month")]
    MonthlyAccess,

    [PermissionData(id: "c7f69a2d-2d1b-4b8b-aa82-a3d272eccdaf", displayName: "Search Billing", description: "Functionality to search for billing information")]
    SearchBilling,

    [PermissionData(id : "7f528767-4424-4a2d-b715-0af723de8dd8", displayName : "Upload Invoices", description : "Functionality to upload billing invoices")]
    UploadInvoice,

    [PermissionData(id : "dde36592-65f6-4c0c-a854-94b0762bd6cd", displayName : "Manage Invoices", description : "Functionality to manage invoices")]
    EBilling
}

    public enum EBilling : long
{
    [PermissionDetailId("ee23174c-9976-4131-80c9-58455f963484")]
    NoAccess = 0 << 0,

    [PermissionDetailId("03c392ac-ab9e-419f-8dce-8ebbef5cf3cb")]
    CanAccess = 1 << 0,

    [PermissionDetailId("c16e7de5-d648-4859-aef0-9fa32adf145e")]
    MayPerformBaseFunctions = 1 << 1,

    [PermissionDetailId("38dd94b5-a3f0-4603-8120-febf25328cf1")]
    CanPerformInvoiceSearch = 1 << 2,

    [PermissionDetailId("3283a34e-3cdc-4001-9c9d-8bedca133e2f")]
    CanViewInvoice = 1 << 3,

    [PermissionDetailId("6fba59a4-9dd5-40fb-9861-5dc2049af231")]
    CanEmailInvoice = 1 << 4,

    [PermissionDetailId("29e4caa1-1df0-4416-9761-8ba983303a48")]
    CanUploadInvoice = 1 << 5,

    [PermissionDetailId("ba836e5a-3e95-46e7-a91c-a95c984ac983")]
    CanDownloadInvoice = 1 << 6,

    [PermissionDetailId("682c3106-c623-432f-85da-d145935ef036")]
    CanPrintInvoice = 1 << 7,

    [PermissionDetailId("d9e2aa0a-0d94-40fa-9689-3bd4377645cb")]
    CanCombineInvoice = 1 << 8,

    [PermissionDetailId("903e185b-f214-47e8-8258-deb621ebc0b2")]
    CanDeleteInvoice = 1 << 9
}





Aucun commentaire:

Enregistrer un commentaire