跟进来自this的问题。以强制类型转换为匿名类型的简洁方法C#
我想投从Strongly Typed
到Anonymous Type
结果。举例来说,在运行时应将以下类转换为匿名类型对象。我努力实现这一目标。
public sealed class CountryModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public bool IsActive { get; set; }
}
用法:
new CountryModel()
{
CountryCode = "AOE",
CountryId = 2,
CountryName = "Anywhere on Earth",
IsActive = true
};
匿名类型:
上述强类型应转换为匿名和最终的结果是这样的(通过立即窗口捕获):
{ CountryId = 2, CountryName = "Anywhere on Earth", CountryCode = "AOE", IsActive = true }
CountryCode: "AOE"
CountryId: 2
CountryName: "Anywhere on Earth"
IsActive: true
注意:我需要执行此投射操作,以便将对象传递给Dapper.SimpleCRUD和Dapper ORM库。
你为什么要这么做? –
它不是一个匿名对象,它可以被装箱到一个“对象”。但它不是匿名的 –
添加了一个笔记给我的问题。但是,在这里你再次“需要完成这个工作,以便我可以将对象传递给Dapper.SimpleCRUD和Dapper ORM库” –