简体   繁体   English

类型(x)参数返回类型x的方法

[英]Method with Type (x) parameter returning Type x

im a bit stuck right now and i hope u can quickstart me. 我现在有点卡住了,我希望你能快点开始。

i Want a Method that i can for Example call like this: 我想要一个可以例如这样调用的方法:

string myString = GetSomething(typeof(string));

OR 要么

DateTime dt = GetSomething(typeof(DateTime));

while GetSomething always returns the Type i give in as Argument. GetSomething总是返回我作为参数输入的Type。 Is it possible to make this kind of signature? 可以进行这种签名吗? Or do i blow my head up for nothing right now? 还是我现在一无所获?

Im atm stuck with this approach: 我的atm坚持这种方法:

 public T GetSomething<T>(Type t ) where T : struct

Assuming I've read your question correctly, all you should need is: 假设我已正确阅读您的问题,那么您需要做的只是:

public T GetSomething<T> {
  return default(T);
}

DateTime dt = GetSomething<DateTime>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM