- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 FastMember 库将对象列表转换为数据表,但它返回空对象,所以任何人都可以帮我解决这个问题
List<object> list = new List<object>() { new { Number = 500 } };
DataTable table = new DataTable();
using (var reader = ObjectReader.Create(list))
{
table.Load(reader);
}
最佳答案
显然,Fastmember 无法枚举匿名对象的属性。因此,创建的数据读取器没有列,DataTable.Load 方法拒绝为该读取器创建空行。
如果可以,请尝试使用具体类:
class Thingy
{
public int Number { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Thingy> list = new List<Thingy>() { new Thingy { Number = 500 } };
DataTable table = new DataTable();
using (var reader = ObjectReader.Create(list))
{
table.Load(reader);
}
}
}
编辑:实际上,Fastmember 完全能够访问这些属性,但是通用列表(对象)的类型阻止它看到它们。如果您可以提供具有实际运行时类型的 IEnumerable,它也应该可以工作:
//This creates a "strongly" typed list, instead of List<object>:
var list = new[] { (new { Number = 500 }) }.ToList();
DataTable table = new DataTable();
using (var reader = ObjectReader.Create(list))
{
table.Load(reader);
}
编辑 2: 还有另一种使用构造函数将类型信息传递给 Fastmember 的方法:
List<object> list = new List<object> { new { Number = 500 } };
DataTable table = new DataTable();
// Note that type information is derived directly from the first object in the list,
// so try not to pass an empty one :)
using (var reader = new ObjectReader(list[0].GetType(), list, null))
{
table.Load(reader);
}
另请注意,这比其他方法风险更大,因为可以创建包含混合项目类型的列表。 Fastmember 需要列表中的每个项目都是完全相同的类型,像下面这样的东西会导致异常:
//These two items are not of the same type. Look carefully at the "Extra" property:
List<object> list = new List<object> { new { Number = 500, Extra = true }, new { Number = 500, Extra = "Boom" } };
关于c# - FastMember ObjectReader 返回 0 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40020589/
当使用 TypeAccessor.Create FastMember 时,似乎总是返回按字母排序的列列表。是否可以告诉它保留类中列的顺序? 例如: var testClass = new { B =
我需要提取所有不可枚举的公共(public)读写属性,除非它们是字符串。目前这是通过反射完成的,想知道这是否可以用 FastMember 完成. 我尝试了类似下面的代码,但它没有达到我想要的效果。我可
(这是通过 Twitter 上的一个问题,经许可在此处重新提问) 我正在尝试快速验证某些对象(以测试空值),我认为 FastMember 可能会有所帮助 - 但是,通过下面显示的测试,我发现性能要差得
当我尝试使用 FastMember 设置嵌套成员属性时出现异常。例如当有这些类(class)时 public class A { public B First { get; set; } }
我有以下对象: dynamic person = new {Id = 1, Name = "SpiderMan"}; 我需要能够遍历属性名称,例如“Id”,“Name”。 我还需要能够以最有效的方式实
我想用 FastMember ( https://www.nuget.org/packages/FastMember/ ) 替换我的反射索引访问器,但偶然发现了以下问题。 我有以下设置: class
我从一个我无法控制的库返回一个接口(interface): public interface IA : IB { String A { get;} } public interface IB { St
我不确定它是否受支持,但我试图让 fastmember 为我提供动态 ExpandoObject 的类型信息(使用下面的代码),但我收到错误: System.NotSupportedException
我有一个 C# .NET Core 2.0 Web API 项目(作为微服务实现)。 我在我的存储库层中使用 NuGet 包“Dapper”和“FastMember”(最新版本)来尝试转换 IEnum
我正在使用 FastMember转换 List到 Datatable .一些类包含枚举,这在将数据表作为 TVP 传递给存储过程时会导致问题。 public class MyObject {
我编写了一个方法,使用 FastMember 将通用列表转换为数据表来自 NuGet。 这是我的代码: public DataTable ConvertGenericListToDataTable(
我正在使用 FastMember 库将对象列表转换为数据表,但它返回空对象,所以任何人都可以帮我解决这个问题 List list = new List() { new { Number = 500 }
我已经使用这个函数成功地为属性和嵌套属性赋值 private static void AssignValueToProperty(ObjectAccessor accessor, object val
我从 http://code.google.com/p/fast-member/source/checkout 加载了 .net 3.5 FastMember在 VS 2010 中。 编译器生成此错误
我一直遇到这个错误 指定的参数超出了有效值的范围。参数名称:名称 当我几乎只是在这里复制示例时 https://code.google.com/p/fast-member/ 错误发生在 bcp.Wri
我是一名优秀的程序员,十分优秀!