可以制作随机类型的方法吗? [英] Possible to make a method with random type?

查看:17
本文介绍了可以制作随机类型的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候

我想知道是否可以创建一个随机类型的方法.

I'm wondering if it is possible to create a single method with a random type.

类似于:

public static T CheckWhatTIs(object source)
{
    MessageBox.Show("T = " + T.GetType());
}

当我将它用作 CheckWhatTIs(true) 时,我会在哪里得到T = bool";并在我将其用作 CheckWhatTIs(1) 时获得T = int";

where I would get "T = bool" when I use it as CheckWhatTIs(true); and get "T = int" when I use it as CheckWhatTIs(1);

有没有可能做到这一点?

Is it possible to accomplish this ?

推荐答案

public static void CheckWhatTIs<T>(T source)
{
    MessageBox.Show("T = " + source.GetType());
}

几点说明:

  1. 该函数没有返回类型,因为您只是显示一个消息框
  2. 如果您想将其用作 CheckWhatTIs(1)CheckWhatTIs(true) 不要将其声明为扩展方法,请删除 this来自参数.
  1. The function has no return type as you are just showing a message box
  2. If you want to use it as CheckWhatTIs(1) and CheckWhatTIs(true) don't declare it as an extension method, remove this from the parameters.

这篇关于可以制作随机类型的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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