mardi 15 décembre 2020

Suggest best approach/Pattern

I have a list of BaseClauses which returns ==> Clauses Types ClauseA, ClauseB, ClauseC and baseClauseList and its type are from DLL

NOTE : ALL the Classes like BaseClause , ClauseA, ClauseB are from DLL. So Code changes are not possible on that part of code.

List<BaseClause> baseClauseList = DLL.SomeMethod();

String theExpectedResult;

    foreach (var clause in baseClauseList)
    {
        // Need to run some logic depends on clause type
        // each clause will returns some value/values (type of object/s)
        // get those value/values and form a string 
    }

what is the best approach to do this (by avoiding multiple ifs and switch to cast to derived type)

I want to do something like

foreach (var clause in baseClauseList)
{
    clause.GetResultStringFromDerivedClause();
}

so here I will need to convert the BaseClause type to derived but can't able to do changes on that code as its from DLL, so the type conversion is something like BaseClause to ClauseA then ClauseA to MyClauseA (this is to form string) and then reverse it to invoke like clause.GetResultStringFromDerivedClause(); I am not able to implement interfaced approach here as all class like BaseClause and ClauseA, ClauseB etc are from a DLL. Is this possible ? Or is there any other approach for it

Aucun commentaire:

Enregistrer un commentaire