Sto lavorando con un dispositivo che restituisce un'immagine, e quando richiedo un'immagine, ci sono alcune informazioni non documentate che vengono prima dei dati dell'immagine. Sono stato in grado di realizzare questo solo guardando attraverso i dati binari e identificando le informazioni di intestazione dell'immagine all'interno.Problemi con i metodi di estensione per i matrici di byte
Originariamente avevo un metodo normale e l'ho convertito in un metodo di estensione. La domanda originale era relativa al compilatore che si lamentava di non avere Array come primo parametro (avevo Byte []), ma risulta che avevo commesso un errore e ho dimenticato di cancellare il primo argomento nel codice chiamante. In altre parole, ho usato per avere:
Byte[] new_buffer = RemoveUpToByteArray(buffer, new byte[] { 0x42, 0x4D });
e dopo aver cambiato ad un metodo di estensione, avevo erroneamente utilizzato:
buffer.RemoveUpToByteArray(buffer, new byte[] { 0x42, 0x4D });
In ogni caso, questo è tutto fisso ora perché ho realizzato il mio errore come ero inserendo l'esempio di codice in SO. Tuttavia,, ho un nuovo problema che è semplicemente la mancanza di comprensione dei metodi di estensione e di riferimento rispetto ai tipi di valore. Ecco il codice:
public static void RemoveFromByteArrayUntil(this Byte[] array, Byte[] until)
{
Debug.Assert(until.Count() > 0);
int num_header_bytes = until.Count();
int header_start_pos = 0; // the position of the header bytes, defined by [until]
byte first_header_byte = until[0];
while(header_start_pos != -1) {
header_start_pos = Array.IndexOf(array, first_header_byte, header_start_pos);
if(header_start_pos == -1)
break;
// if we get here, then we've found the first header byte, and we need to look
// for the next ones sequentially
for(int header_ctr=1; header_ctr<num_header_bytes; header_ctr++) {
// we're going to loop over each of the header bytes, but will
// bail out of this loop if there isn't a match
if(array[header_start_pos + header_ctr] != until[header_ctr]) {
// no match, so bail out. but before doing that, advance
// header_start_pos so the outer loop won't find the same
// occurrence of the first header byte over and over again
header_start_pos++;
break;
}
}
// if we get here, we've found the header!
// create a new byte array of the new size
int new_size = array.Count() - header_start_pos;
byte[] output_array = new byte[new_size];
Array.Copy(array, header_start_pos, output_array, 0, new_size);
// here is my problem -- I want to change what array points to, but
// when this code returns, array goes back to its original value, which
// leads me to believe that the first argument is passed by value.
array = output_array;
return;
}
// if we get here, we didn't find a header, so throw an exception
throw new HeaderNotInByteArrayException();
}
Il mio problema ora è che sembra che il primo questo argomento per il metodo di estensione è passato per valore. Voglio riassegnare a quale array punta, ma in questo caso sembra che dovrò semplicemente manipolare i dati dell'array.
Non una risposta, solo un'osservazione: sembra che il tuo RemoveUpToByteArray debba creare un nuovo array di byte ogni volta che viene chiamato. Sarebbe più efficiente usare un intero come indice nell'array per tenere traccia di quanto lontano hai letto. –
@Dave: non si è troppo specifici su dove si sta verificando l'errore del compilatore. Suppongo che sia al * call site * e non al metodo di estensione. Anche se lo è, non stai mostrando nessuno dei codici per nessuno dei due. È necessario approfondire questo aspetto per ottenere una risposta, poiché * è possibile * creare metodi di estensione su array di byte ed eseguirli facilmente. – casperOne
ok, nessun problema! Vado avanti e postare il * codice * reale. :) Grazie per il feedback. – Dave