本文介绍了如何将类型转换为类型名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有通用参数的方法,我该如何输入:

Say I have a method with generic parameters, how do I enter:

doSomething<foo.GetType()>();





为什么这不起作用?如何使这项工作?



Why doesn't this work and how can I make this work?

推荐答案


private void DoSomething<T>()
    {
    Console.WriteLine("DoSomething:" + typeof(T));
    }
private void DoSomething<T>(T x)
    {
    Console.WriteLine("DoSomething:" + x.GetType());
    }

然后你可以调用它:

Then you can call it:

DoSomething<int>();
DoSomething<float>();
double d = 12.34;
DoSomething(d);

得到你想要的东西:

And get what you want:

DoSomething:System.Int32
DoSomething:System.Single
DoSomething:System.Double


这篇关于如何将类型转换为类型名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:57
查看更多