/IsTo

Any type or object checking and conversion extensions.

Primary LanguageC#Apache License 2.0Apache-2.0

IsTo

Any type or object checking and conversion extension.

Features

Is

Check any type or object inherits from any class or implemented by any interface.

  1. value.Is<T>()
    From Type or Object to check by Generic.
  2. value.Is(Type type)
    From Type or Object to check by Type.

To

Any type of object convert to any type.

  1. value.To<T>()
    From any type of Object convert to any type by Generic.
  2. value.TryTo<T>(out T result)
    From any type of Object try to convert to any type by Generic.
  3. value.To(Type type)
    From any type of Object convert to any type by Type.

Installation

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

How to use

After installed this package, add using IsTo; on your source code and enjoy the convenience from it.

Is

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

To

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 }

License