Sto scrivendo un metodo per generare un DataTable che prende come origine dati un IEnumerable generico. Sto cercando di impostare un valore predefinito sul campo se non c'è nessun valore, con il codice qui sotto:Perché il codice non viene compilato?
private void createTable<T>(IEnumerable<T> MyCollection, DataTable tabela)
{
Type tipo = typeof(T);
foreach (var item in tipo.GetFields())
{
tabela.Columns.Add(new DataColumn(item.Name, item.FieldType));
}
foreach (Pessoa recordOnEnumerable in ListaPessoa.listaPessoas)
{
DataRow linha = tabela.NewRow();
foreach (FieldInfo itemField in tipo.GetFields())
{
Type typeAux = itemField.GetType();
linha[itemField.Name] =
itemField.GetValue(recordOnEnumerable) ?? default(typeAux);
}
}
}
E 'gettando questo errore:
The type or namespace name 'typeAux', could not be found (are you missing a using directive or an assembly reference?)
Perché? La funzione "Predefinito (Tipo)" non dovrebbe restituire un valore predefinito per quel tipo?
'default()' richiede un tipo di nome, non un 'di riferimento Type'. Ad esempio, 'default (int)'. – recursive