-
Notifications
You must be signed in to change notification settings - Fork 2
Type extensions
halcharger edited this page Apr 7, 2014
·
5 revisions
#####TypeExtensions.CreateInstance
Given a Type
variable:
var userType = typeof(User);
Instead of:
var user = (User)Activator.CreateInstance(userType);
We can write:
var user = (User)userType.CreateInstance();
//or
var user = userType.CreateInstance<User>();
#####TypeExtenstions.IsIEnumerable
Given:
var enumerableType = typeof(IEnumerable<string>);
Instead of:
if (typeof(IEnumerable).IsAssignableFrom(enumerableType))
{
//do something
}
We can write:
if (enumerableType.IsIEnumerable())
{
//do something
}
#####TypeExtenstions.IsIList
Given:
var listType = typeof(IList);
Instead of:
if (typeof(IList).IsAssignableFrom(listType))
{
//do something
}
We can write:
if (listType.IsIList())
{
//do something
}
#####TypeExtenstions.IsString
Given:
var type = typeof(string);
Instead of:
if (typeof(string) == type)
{
//do something
}
We can write:
if (type.IsString())
{
//do something
}
#####TypeExtenstions.IsType
Given:
var type = typeof(Type);
Instead of:
if (typeof(Type) == type)
{
//do something
}
We can write:
if (type.IsType())
{
//do something
}