简体   繁体   中英

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. 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:

 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>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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