开发者

How do I get the type argument of a generic type?

开发者 https://www.devze.com 2022-12-24 23:56 出处:网络
i think this is a simple question but I\'ve searched around and can\'t seem to find an answer easily.

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


Type  GetType<T>(Table<T> table)
{
    return typeof(T);
}

var table = new DataContext().TABLE1s; // this is Table<TABLE1>  
var tableType = GetType(table);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号