Piszę metodę generowania DataTable biorąc jako źródło danych ogólne IEnumerable. Próbuję ustawić wartość domyślną na polu, jeśli tam nie warto, z poniższym kodzie:Dlaczego ten kod nie jest kompilowany?
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);
}
}
}
To rzuca ten błąd:
The type or namespace name 'typeAux', could not be found (are you missing a using directive or an assembly reference?)
Dlaczego? Czy funkcja "Default (Type)" nie powinna zwracać wartości domyślnej dla tego typu?
'default()' wymaga nazwie typu, a nie 'odniesienia typem. Na przykład 'default (int)'. – recursive