2011-10-03 3 views
16

Sto usando il ReSharper per ridimensionare il mio codice. Quando provo a spostare un blocco di codice al metodo, ottengo il seguente avvertimento:Perché viene visualizzato l'errore ReSharper "Il codice estratto ha più punti di accesso"?

The extracted code has multiple entry points

Ecco la firma del metodo sto progettando di utilizzare:

private void GetRatePlanComponents(ProductPlan productPlan, 
    ProductRatePlan productRatePlan)  

ho cercato nel web per capire cosa significa. Ma non ha avuto molta fortuna. Qualcuno lo spiegherebbe?

Per riferimento, ecco il frammento di codice che sto cercando di passare ad un metodo separato:

QueryResult productRatePlanChargeQueryResult = 
    _zuoraService.query(string.Format(@"select Id, Name, IncludedUnits from 
     ProductRatePlanCharge where ProductRatePlanId = '{0}' and 
     ChargeModel = 'Overage Pricing'", productRatePlan.Id)); 

if (productRatePlanChargeQueryResult.size > 0) 
{ 
    foreach (ProductRatePlanCharge productRatePlanCharge 
     in productRatePlanChargeQueryResult.records) 
    { 
     string numberOfUnits = productRatePlanCharge.IncludedUnits.ToString(); 

     if (productRatePlanCharge.Name.Equals("Users")) 
     { 
      productPlan.NumberofUsers = numberOfUnits; 
     } 
     else if (productRatePlanCharge.Name.Equals("Projects")) 
     { 
      productPlan.NumberofProjects = numberOfUnits; 
     } 
     else if (productRatePlanCharge.Name.Equals("Storage")) 
     { 
      decimal volumeOfStorage; 
      if (decimal.TryParse(productRatePlanCharge.IncludedUnits.ToString(), 
       out volumeOfStorage)) 
      { 
       if (volumeOfStorage < 1) volumeOfStorage *= 1000; 
        productPlan.VolumeofStorage = volumeOfStorage.ToString(); 
       } 
       else 
       { 
        productPlan.VolumeofStorage = numberOfUnits; 
       } 
      } 
     } 
    } 
} 
+3

Sei sicuro che indichi più punti * entry * anziché * exit * punti? Indica una linea particolare? È questa la totalità del metodo? Puoi includere la firma del metodo? –

+0

@Jon Skeet: Sì. dice "entry point". Dai un'occhiata alla domanda aggiornata. – Moon

risposta

7

Sembra che si può avere incontrato a known issue:

public static IEnumerable<ITagPrefixHolder> GetRelevantHolders(IPsiSourceFile sourceFile) 
{ 
    var targetPath = FileSystemPath.Empty; 
    var projectFile = sourceFile.ToProjectFile(); 
    if (projectFile != null) 
    targetPath = projectFile.Location; 

    foreach(var holder in GetRelevantHoldersBeforeFile(sourceFile, targetPath)) 
    yield return holder; 

    foreach(var holder in GetHoldersInFile(sourceFile, targetPath)) 
    yield return holder; 
} 

selezionare sia foreach-loops e metodo di estratto. Dà strano avvertimento che il frammento ha più punti di ingresso e genera il seguente codice di (??!):

public static IEnumerable<ITagPrefixHolder> GetRelevantHolders(IPsiSourceFile sourceFile) 
{ 
    var targetPath = FileSystemPath.Empty; 
    var projectFile = sourceFile.ToProjectFile(); 
    if (projectFile != null) 
    targetPath = projectFile.Location; 

    foreach(var tagPrefixHolder in Foo(sourceFile, targetPath)) 
     yield return tagPrefixHolder; 
} 

private static IEnumerable<ITagPrefixHolder> Foo(IPsiSourceFile sourceFile, FileSystemPath targetPath) 
{ 
    foreach(var holder in GetRelevantHoldersBeforeFile(sourceFile, targetPath)) 
    yield return holder; 
    foreach(var holder in GetHoldersInFile(sourceFile, targetPath)) 
    yield return holder; 
} 

Sarebbe meglio sostituire foreach generata con semplice return Foo(sourceFile, targetPath);.

1

ho visto ReSharper fare la stessa cosa quando il codice stavo cercando di estrarre avuto un paio di rilascia dichiarazioni.

Si può fare ciò che ho fatto in quel caso - commentare sistematicamente una riga alla volta fino a trovare quella su cui ReSharper scatta. Quindi è possibile estrarre il metodo e decommentare la riga in seguito.

Oppure puoi semplicemente effettuare il refactoring a mano.