Tôi đang viết một phương pháp để tạo ra một DataTable lấy làm nguồn dữ liệu chung cho IEnumerable. Tôi cố gắng để thiết lập một giá trị mặc định trên các lĩnh vực nếu theres không có giá trị, với mã dưới đây:Tại sao mã này không biên dịch?
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);
}
}
}
Nó ném lỗi này:
The type or namespace name 'typeAux', could not be found (are you missing a using directive or an assembly reference?)
Tại sao? Không nên chức năng "Default (Type)" trả về một giá trị mặc định cho loại đó?
'mặc định() 'đòi hỏi một kiểu đặt tên, không phải là một' Type' tham khảo. Ví dụ, 'default (int)'. – recursive