jeudi 10 novembre 2016

Attach Generic method event handler with unknow type

I need to attach this handler to a RadListView Column creation, by adding a DataSource to the control.

 public void GenericColumnCreatingHandler<T>(object sender, ListViewColumnCreatingEventArgs e)
    {
        e.Column.Visible = BaseEntity<int>.MemberVisibility<T>
           (e.Column.FieldName, TelerikPropertyVisibilityAttribute.VisibilityTypeEnum.BaseDetails);

        e.Column.HeaderText = CaricaTestoLocale(e.Column.HeaderText, "Col_" + e.Column.HeaderText);                        

        e.Column.BestFit();
        e.Column.AutoSizeMode = ListViewBestFitColumnMode.AllCells;
    } 

My problem is that I need to perform the handler attach from this other generic method:

  private void PopulateRecord(TipoTabellaBase tipo)
    {
        Type generic = typeof(CommonTableService<>);
        Type[] typeArgs = { tipo.Tipo };
        var constructed = generic.MakeGenericType(typeArgs);

        var instance = Activator.CreateInstance(constructed);
        if (instance == null)
            return;

        MethodInfo getEntities = constructed.GetMethod("GetEntitiesWithNoParameters");
        //getEntities = getEntities.MakeGenericMethod(typeArgs);            

        var result = (IEnumerable<BaseEntity<int>>)getEntities.Invoke(instance, null);                                                                                   
        lvRecords.ColumnCreating += base.GenericColumnCreatingHandler<BaseEntity<int>>;
        lvRecords.DataSource = result;
        BestFit(lvRecords);

        generic = null;
        typeArgs = null;
        constructed = null;
        getEntities = null;
        instance = null;          
    }

The problematic row is this one:

 lvRecords.ColumnCreating += base.GenericColumnCreatingHandler<BaseEntity<int>>

because BaseEntity is EF base type for all Entities, but this is not enought for the BaseEntity.MemberVisibility method; this method need to know the exact entity type to set the visible properties (and, of course, grid column) based on specific custom attribute on that.

Question is: how I can call base.GenericColumnCreatingHandler where T is TipoTabellaBase tipo.Tipo (type) without knowing type at design time?

Any help would be very appreciated! Thanks is advance.

Daniel





Aucun commentaire:

Enregistrer un commentaire