Check any type or object inherits from any class or implemented by any interface.
value.Is<T>()
From Type or Object to check by Generic.value.Is(Type type)
From Type or Object to check by Type.
Any type of object convert to any type.
value.To<T>()
From any type of Object convert to any type by Generic.value.TryTo<T>(out T result)
From any type of Object try to convert to any type by Generic.value.To(Type type)
From any type of Object convert to any type by Type.
IsTo can be installed via the nuget UI (as IsTo), or via the nuget package manager console:
PM> Install-Package IsTo
If you require a strong-named package (because your project is strong-named), then you may wish to use instead:
PM> Install-Package IsTo.StrongName
After installed this package, add using IsTo;
on your source code and enjoy the convenience from it.
value.Is<T>() Check by Generic
// 1. from Type
var value = typeof(Int32);
value.Is<Int32>(); // true
// 2. from Object
var value = 123;
value.Is<Int32>(); // true
value.Is(Type type) Check by Type
// 1. from Type
var value = typeof(Int32);
value.Is(typeof(Int32)); // true
// 2. from Object
var value = 123;
value.Is(typeof(Int32)); // true
There is an enumeration for example:
public enum Animal
{
Bird,
Cat,
Dog
}
value.To<T>() Convert by Generic
var value = new[] { Animal.Cat, Animal.Dog };
// Convert to array
var result1 = value.To<int[]>(); // { 1, 2 }
var result2 = value.To<string[]>(); // { "Cat", "Dog" }
// Convert to List<T>
var result3 = value.To<List<int>>();
var result4 = value.To<List<string>>();
value.TryTo<T>(out T result) Try Convert by Generic
var value = new[] { Animal.Cat, Animal.Dog };
// Try to Convert to array
int[] result;
if(value.TryTo<int[]>(out result)){
// Success
} else {
// Failure
}
value.To(Type type) Convert by Type
var value = new[] { Animal.Cat, Animal.Dog };
var type = typeof(int[]);
// Convert to array
var result1 = value.To(type); // { 1, 2 }