//怎么取得匿名类型的Type放到
//泛型T当中?? var 匿名 = new {
A = 0,
B = 1 };
Type t = 匿名.GetType(); //然后下面
var xx = dbContext.Database.SqlQuery<t>("sql");
//就悲剧了 var xx2 = dbContext.Database.SqlQuery<dynamic>("sql");
//xx2有列表,但是都是Object..~~~无法显示真实项,用Profile跟..SQL确实提交了.
求解释

msdn大神的解决办法

看到了所以记录下来

出现那个问题的原因是动态类型上没有 Entity Framework 需要的属性定义,Entity Framework 是通过反射类型上的属性来做映射的。为了解决这个问题,我用到了 Emit 技术,动态产生一个类型,并且动态写入 IL 代码产生属性。

代码如下,测试在 C# 4.0 上通过。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Collections;
using System.Reflection.Emit;
using System.Reflection; namespace Demo
{
public class Program
{
public static void Main(string[] args)
{
string connectionString = "Server=(local); Integrated Security=true; Database=master";
using (DbContext context = new DbContext(connectionString))
{
TypeBuilder builder = Program.CreateTypeBuilder("MyDynamicAssembly", "MyModule", "MyType");
Program.CreateAutoImplementedProperty(builder, "name", typeof(string));
Program.CreateAutoImplementedProperty(builder, "type", typeof(string));
Program.CreateAutoImplementedProperty(builder, "id", typeof(int)); Type resultType = builder.CreateType(); dynamic queryResult = context.Database.SqlQuery(resultType, "SELECT * FROM sys.sysobjects"); Console.WriteLine("{0,20} {1,4} {2,10}", "Name", "Type", "ID");
foreach (dynamic item in queryResult)
{
Console.WriteLine("{0,10} {1,4} {2,10}", item.name, item.type, item.id);
}
} Console.ReadKey();
} public static TypeBuilder CreateTypeBuilder(string assemblyName, string moduleName, string typeName)
{
TypeBuilder typeBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Run).DefineDynamicModule(moduleName).DefineType(typeName, TypeAttributes.Public);
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
return typeBuilder;
} public static void CreateAutoImplementedProperty(TypeBuilder builder, string propertyName, Type propertyType)
{
const string PrivateFieldPrefix = "m_";
const string GetterPrefix = "get_";
const string SetterPrefix = "set_"; // Generate the field.
FieldBuilder fieldBuilder = builder.DefineField(string.Concat(PrivateFieldPrefix, propertyName), propertyType, FieldAttributes.Private); // Generate the property
PropertyBuilder propertyBuilder = builder.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null); // Property getter and setter attributes.
MethodAttributes propertyMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; // Define the getter method.
MethodBuilder getterMethod = builder.DefineMethod(string.Concat(GetterPrefix, propertyName), propertyMethodAttributes, propertyType, Type.EmptyTypes); // Emit the IL code.
// ldarg.0
// ldfld,_field
// ret
ILGenerator getterILCode = getterMethod.GetILGenerator();
getterILCode.Emit(OpCodes.Ldarg_0);
getterILCode.Emit(OpCodes.Ldfld, fieldBuilder);
getterILCode.Emit(OpCodes.Ret); // Define the setter method.
MethodBuilder setterMethod = builder.DefineMethod(string.Concat(SetterPrefix, propertyName), propertyMethodAttributes, null, new Type[] { propertyType }); // Emit the IL code.
// ldarg.0
// ldarg.1
// stfld,_field
// ret
ILGenerator setterILCode = setterMethod.GetILGenerator();
setterILCode.Emit(OpCodes.Ldarg_0);
setterILCode.Emit(OpCodes.Ldarg_1);
setterILCode.Emit(OpCodes.Stfld, fieldBuilder);
setterILCode.Emit(OpCodes.Ret); propertyBuilder.SetGetMethod(getterMethod);
propertyBuilder.SetSetMethod(setterMethod);
}
}
}

转自:http://www.cnblogs.com/0banana0/archive/2012/05/05/2484698.html

注:这样应该可以轻松实现 DataTable To Class

如何把匿名类型.GetType()返回的对象传进泛型里面[转]的更多相关文章

  1. 匿名类型 使用泛型T linq返回dynamic类型的匿名实体 如何把匿名类型.GetType()返回的对象传进泛型里面 EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题

    [100分]紧急求助:LinQ下使用IQueryable<T>如何将返回类型<T>使用匿名类型 问题描述如下:我有一个方法如下:public IQueryable Dissen ...

  2. 匿名类型(C# 编程指南)

    匿名类型提供了一种方便的方法,可用来将一组只读属性封装到单个对象中,而无需首先显式定义一个类型. 类型名由编译器生成,并且不能在源代码级使用.  每个属性的类型由编译器推断. 可通过使用 new 运算 ...

  3. C#3.0新增功能02 匿名类型

    连载目录    [已更新最新开发文章,点击查看详细] 匿名类型提供了一种方便的方法,可用来将一组只读属性封装到单个对象中,而无需首先显式定义一个类型. 类型名由编译器生成,并且不能在源代码级使用. 每 ...

  4. Netcore webapi action swagger response返回参数使用匿名类型

    问题:action中返回匿名对象时,swagger只能按强类型生成返回值描述 解决办法:使用roslyn在内存中动态执行代码,使用json.net反序列化匿名对象,向swagger返回动态匿名对象 效 ...

  5. C# 匿名对象(匿名类型)、var、动态类型 dynamic

    本文是要写的下篇<C#反射及优化用法>的前奏,不能算是下一篇文章的基础的基础吧,有兴趣的朋友可以关注一下. 随着C#的发展,该语音内容不断丰富,开发变得更加方便快捷,C# 的锋利尽显无疑. ...

  6. C# 匿名对象(匿名类型)、var、动态类型 dynamic——实用之:过滤类属性、字段实用dynamic

    例子 返回一个LIst<oject>类型 而oject含有 30个字段 而我只需要两个字段.这里实用dynamic 和 linq. 上代码: 注意select new {} 为匿名类型,这 ...

  7. C#3.0新特性:隐式类型、扩展方法、自动实现属性,对象/集合初始值设定、匿名类型、Lambda,Linq,表达式树、可选参数与命名参数

    一.隐式类型var 从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型var.隐式类型可以替代任何类型,编译器自动推断类型. 1.var类型的局部变量必须赋予初始值,包括匿名 ...

  8. c# 把一个匿名对象赋值给一个Object类型的变量后,怎么取这个变量? c# dynamic动态类型和匿名类 详解C# 匿名对象(匿名类型)、var、动态类型 dynamic 深入浅析C#中的var和dynamic

    比如有一个匿名对象,var  result =......Select( a=>new {  id=a.id, name=a.name});然后Object  obj =  result ;我怎 ...

  9. [LINQ2Dapper]最完整Dapper To Linq框架(六)---多表联合与匿名类型返回

    目录 [LINQ2Dapper]最完整Dapper To Linq框架(一)---基础查询 [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询 [LINQ2Dapp ...

随机推荐

  1. STL,ATL,WTL之间的联系和区别

    STL即 Standard Template Library (标准模板库) STL是惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Lee和David R M ...

  2. 解析php中die(),exit(),return的区别

    die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值die是遇到错误才停止exit是直接停止,并且不运行后续代码,exit()可以显示内容.return就是纯粹的返回 ...

  3. EntityFreamwork 读写分离

    http://www.cnblogs.com/cjw0511/p/4391092.html

  4. 查看使用的Eclipse版本

    第一种方法 1. 找到Eclipse的解压目录就是你的Eclipse.exe 所在的目录 2. 找到 .eclipseproduct 文件双击打开

  5. 174. Dungeon Game

    题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...

  6. R语言学习笔记:查看函数的R源代码

    getAnywhere 该函数可以返回一些函数的R源代码,如: getAnywhere(kmeans) 该函数具体用法,请参看官方说明. Retrieve an R Object, Including ...

  7. zip文件解压或压缩

    <span style="font-size:18px;">/** * lsz */ public final class ZipUtil { /** * 解压zip文 ...

  8. VMware下ubuntu与win8共享文件时/mnt/hgfs目录为空的解决办法

    VMware下ubuntu(guest)与win8共享文件时/mnt/hgfs目录为空的解决办法 环境:VMware-player-5.0.2-1031769 + ubuntu13.04 1.安装vm ...

  9. 最受 Web 开发者欢迎的 NoSQL 和关系数据库

    Web应用离不开数据库,目前市场上有种类繁多数据库可供开发者选择,例如SQL.NoSQL.键值.图谱数据库等等.关于不同数据库在开发者中的受欢迎程度也是仁者见仁智者见智,但是通过统计亚马逊这样的公共云 ...

  10. I.MX6 Android frameworks services 文件架构

    /******************************************************************************* * I.MX6 Android fra ...