为什么这段代码没有编译?

Mar*_*mes 5 .net c# generics types

我正在编写一个生成DataTable的方法,将数据源作为通用的IEnumerable.如果没有值,我试图在字段上设置默认值,代码如下:

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); 

                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

它抛出了这个错误:

找不到类型或命名空间名称'typeAux'(您是否缺少using指令或程序集引用?)

为什么?"Default(Type)"函数不应该返回该类型的默认值吗?

Car*_*ine 1

对于引用类型返回 null ,对于值类型返回Activator.CreateInstance怎么样?

public static object GetDefault(Type type)
{
   if(type.IsValueType)
   {
      return Activator.CreateInstance(type);
   }
   return null;
}
Run Code Online (Sandbox Code Playgroud)

参考:default(Type) 的编程等效项