Reputation: 51221
public static T Get<T>() where T : class
{
string implName = Program.Settings[typeof(T).Name].ToString();
object concrete = Activator.CreateInstance(Type.GetType(implName));
return (T)concrete;
}
Please explain what does Get() where T means?
Welcome to put some reading URLs.
Upvotes: 2
Views: 2661
Reputation: 17498
This will constrain T to be a reference type in this particular case.
Upvotes: 1
Reputation: 6916
The where T : class
puts a constrain on what types are allowed for T
. This will
So for your method this will produce an error if you call it like this Get<int>()
since int
is not a class.
public static T Get<T>() where T : class
{
string implName = Program.Settings[typeof(T).Name].ToString();
var implType = Type.GetType(implName);
return (T)Activator.CreateInstance(implType);
}
Upvotes: 3
Reputation: 48402
This is an example of a generic. 'T' represents a type.
For example:
string result = Get<string>();
Do a Google search on Generics. This will get you started: http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx
Upvotes: 2