C#typeof()和Generics [英] C# typeof() and Generics

查看:56
本文介绍了C#typeof()和Generics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说你有:


A级

{

T DoSomething< T>()其中T:class,new()

{

T ThisObj = new T();

//做一些有用的事情...

返回ThisObj;

}


void StartHere(类型类型)

{

//这就是问题

DoSomething< type>();

}

}


当调用函数将类型作为参数而不是类型参数时,有关如何调用泛型函数(如'DoSomething')的任何想法吗?


Jorge Varas

Let''s say that you have:

class A
{
T DoSomething<T>() where T : class, new()
{
T ThisObj = new T();
// Do something usefull...
return ThisObj;
}

void StartHere(Type type)
{
// And here is the problem
DoSomething<type>();
}
}

Any Ideas on how to call a generic function like ''DoSomething'' when the calling function got the type as a parameter, not as a type parameter?

Jorge Varas

推荐答案

让我们说你有:
Let''s say that you have:

>

A级

{

T DoSomething< T>()其中T:class,new()

{

T ThisObj = new T();

//做一些有用的事情......

返回ThisObj;

}

void StartHer e(类型类型)

{

//这就是问题

DoSomething< type>();

}

}

当调用函数得到类型时,有关如何调用像''DoSomething'这样的泛型函数的任何想法作为参数,而不是类型

参数?
>
class A
{
T DoSomething<T>() where T : class, new()
{
T ThisObj = new T();
// Do something usefull...
return ThisObj;
}
void StartHere(Type type)
{
// And here is the problem
DoSomething<type>();
}
}
Any Ideas on how to call a generic function like ''DoSomething'' when
the calling function got the type as a parameter, not as a type
parameter?



不幸的是,这不可能完成(我不知道这里是如何使用typeof)。

你可以得到的最接近的是使StartHere方法通用如

这个:


A级

{

T DoSomething< T>()其中T:class,new()

{

T ThisObj = new T();

//做一些有用的事情......

返回ThisObj;

}


void StartHere< T>( )

{

//这就是问题

T myValue = DoSomething< T>();

}

}


最诚挚的问候,

Dustin Campbell

Developer Express Inc.

Unfortunately, this can''t be done (and I don''t see how "typeof" is used here).
The closest that you could get is to make the StartHere method generic like
this:

class A
{
T DoSomething<T>() where T : class, new()
{
T ThisObj = new T();
// Do something usefull...
return ThisObj;
}

void StartHere<T>()
{
// And here is the problem
T myValue = DoSomething<T>();
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.





" Dustin Campbell" < du ***** @ no-spam-pleasedevexpress.com在留言新闻中写道:c1 ************************** @ news。 microsoft.c om ...


"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message news:c1**************************@news.microsoft.c om...


不幸的是,这不可能完成(而且我也看不出typeof是怎样的在这里使用)。

Unfortunately, this can''t be done (and I don''t see how "typeof" is used here).



关于typeof的用法,这里有一个我的意思(这个有效)的例子:


class Program

{

static void Main(string [] args)

{

Test test = new Test();

test.CallMe();

}

}


类测试

{

public void CallMe()

{

this.DoSomething< Test>();

}

public void DoSomething< T>()其中T:class,new()

{

Console.WriteLine(typeof( T).FullName);

}

}


这里我可以使用typeof来获取Type参数的类型。我希望有一种方法(可能使用typeof)来解决第一个问题。

About the usage of typeof, here is an example of what I mean (this works):

class Program
{
static void Main(string[] args)
{
Test test = new Test();
test.CallMe();
}
}

class Test
{
public void CallMe()
{
this.DoSomething<Test>();
}
public void DoSomething<T>() where T : class, new()
{
Console.WriteLine(typeof(T).FullName);
}
}

Here I can use typeof to get the type of a Type Parameter. I was hoping that there was a way (maybe using typeof) to solve the first problem.


你能得到的最接近的是使StartHere方法像

这个:


A级

{

T DoSomething< T>()其中T: class,new()

{

T ThisObj = new T();

//做一些有用的事情...

返回ThisObj;

}


无效StartHere< T>()

{

//这就是问题

T myValue = DoSomething< T>();

}

}
The closest that you could get is to make the StartHere method generic like
this:

class A
{
T DoSomething<T>() where T : class, new()
{
T ThisObj = new T();
// Do something usefull...
return ThisObj;
}

void StartHere<T>()
{
// And here is the problem
T myValue = DoSomething<T>();
}
}



我知道,但在我的情况下,我正在使用反射检索属性的类型,这是我需要传递给通用DoSomething的类型,所以没有必要使初始调用通用。另一个选择是制作一个非通用版本的DoSomething,只吃掉拳击的性能影响。


Jorge Varas

I know, but in my case I am retrieving the type of a property using reflection, and that is the type that I need to pass to the generic DoSomething, so no point to make the initial call generic. The other option is to make a non-generic version of DoSomething and just eat the performance impact of the boxing.

Jorge Varas


你可以,但你需要使用反射,这是(相对)慢;在

特别是,您将获得方法的MethodInfo(GetMethod),

,然后调用MakeGenericMethod(),它接受Type实例。


然后你会。提供退回的方法信息。


Marc
You can, but you need to use reflection, which is (relatively) slow; in
particular, you would obtain the MethodInfo for the method ("GetMethod"),
and then call MakeGenericMethod(), which accepts the Type instances.

You would then .Invoke the returned method info.

Marc


这篇关于C#typeof()和Generics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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