Perché array semplice e Linq generano VerificationException: l'operazione potrebbe destabilizzare il runtime
-
20-08-2019 - |
Domanda
Un operatore molto semplice? runtime quot. &; quando eseguito su un sito Web ASP.NET in esecuzione con " High " trust (nota che " Full " è l'impostazione predefinita)
Ecco il codice, oltre alla semplice soluzione che ho escogitato:
protected void Page_Load(object sender, EventArgs e) {
Repro();
//Workaround();
}
private IEnumerable<string> Repro() {
bool test = true;
string[] widgets = new string[0];
return test ? widgets : widgets.Reverse();
}
private IEnumerable<string> Workaround() {
bool test = true;
string[] widgets = new string[0];
if (test) {
return widgets;
} else {
return widgets.Reverse();
}
}
Per impostare il livello di affidabilità su " Alto " è necessario aggiungere quanto segue al file web.config:
<trust level="High" originUrl=""/>
La soluzione alternativa è una funzionalità equivalente alla problematica?: operatore che ripresenta il problema. La mia ipotesi dalla lettura di post correlati è che si tratta di un bug del compilatore C #. Qualcuno sa cosa sta succedendo qui?
Soluzione
Questa riga non presenta il problema:
return test ? widgets.AsEnumerable() : widgets.Reverse();
Altri suggerimenti
Ho scoperto che cambiare l'ordine dell'espressione funziona anche attorno al problema:
private IEnumerable<string> Workaround() {
bool test = true;
string[] widgets = new string[0];
return !test ? widgets.Reverse() : widgets;
}
Vai a capire!