我如何获得泛型类型的类型参数? [英] How do I get the type argument of a generic type?

查看:109
本文介绍了我如何获得泛型类型的类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个简单的问题,但我已经搜索过,似乎无法轻松找到答案。



如果您有

  var list = List< int>(); 
...填写清单...

并且您想要获得泛型类型列表,我意识到你可以输入:

  var t = list.FirstOrDefault()。GetType(); 

是否有另一种方法通过列表来完成此操作,而不是参考枚举?



原因是,我有一个 System.Data.Linq.Table< TABLE1> 并且我想要做的是get TABLE1 的类型。



so: pre> var table = new DataContext()。TABLE1s; //这是表< TABLE1>
var tableType = table.GetType()。SomeMethod();
//我希望tableType等于TABLE1.GetType()


解决方案使用它来获取表的实体类型

$ $ p $ $ $ $ $ var tableType = table.GetType()。GetGenericArguments( )[0];


i think this is a simple question but I've searched around and can't seem to find an answer easily.

if you have

var list = List<int>();
... fill list ...

and you want to get the generic type in list, i realise you could just type:

var t = list.FirstOrDefault().GetType();

Is there another way to do this via just the list, rather than referring to the enumeration?

Reason is, i have a System.Data.Linq.Table<TABLE1> and what i want to do is get the type of TABLE1 from it.

so:

var table = new DataContext().TABLE1s; // this is Table<TABLE1>
var tableType = table.GetType().SomeMethod(); 
// i want tableType to equal TABLE1.GetType()

解决方案

try this to get the entity type of the table

var tableType =table.GetType().GetGenericArguments()[0];

这篇关于我如何获得泛型类型的类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆