Marguth
Marguth

Reputation: 191

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

Upvotes: 1

Views: 115

Answers (1)

DoctorMick
DoctorMick

Reputation: 6793

Assuming I've read your question correctly, all you should need is:

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

DateTime dt = GetSomething<DateTime>();

Upvotes: 6

Related Questions