Giter VIP home page Giter VIP logo

h-extensions's Introduction

h-extensions

An extension library for .Net classes and types.

Requirements

h-extensions works with .NET Framework 4.5 and higher

Usage

Build a string from any IEnumerable of char's.

  var chars = new []{'a', 'b', 'f', 'o', 'o'};
  var foo = chars.Where(chr => chr > 0x65).BuildString();
  // foo == "foo"

Build a list string from any IEnumerable of char's.

  var chars = new []{'a', 'b', 'f', 'o', 'o'};
  var foo = chars.Where(chr => chr > 0x65).ToRangeString();
  // foo == "f, o, o"

Parse an enumeration from a string.

public enum TestEnum
{
  Unknown = 0x0,
  FirstValue = 0x1,
  SecondValue = 0x2
}

public void Foo()
{
  var first = "FirstValue".ToEnum<TestEnum>();
  // first == TestEnum.FirstValue

  var invalid = "NotARealValue".ToEnum<TestEnum>();
  // invalid == TestEnum.Unknown
}

Retrieve the description from an enumeration.

public enum TestEnum
{
  [Description("Uknown Value")]
  Unknown = 0x0,

  [Description("The First Value")]
  FirstValue = 0x1,

  [Description("The Second Value")]
  SecondValue = 0x2
}

public void Foo()
{
  var description = "FirstValue".ToEnum<TestEnum>().GetDescription();
  // description == "The First Value"
}

Convert from to and from an integer and an arbitrary enumeration.

public enum TestEnum
{
  Unknown = 0x0,
  FirstValue = 0x1,
  SecondValue = 0x2
}

public void Foo()
{
  Assert.AreEqual(TestEnum.FirstValue, 1.ToEnum<TestEnum>());
  Assert.AreEqual(1, TestEnum.FirstValue.ToInt());

  Assert.AreEqual(TestEnum.Unknown, 55.ToEnum<TestEnum>());
}

Create a delimited list string, from any IEnumerable.

public void Foo()
{
  const string fooDir = @"C:\foo";

  Directory.CreateDirectory(fooDir);
  File.Create(Path.Combine(fooDir, "bar.txt"));
  File.Create(Path.Combine(fooDir, "baz.txt"));

  var fooFiles = Directory.GetFiles(fooDir).ToListString("; ");
  // fooFiles = "bar.txt; baz.txt"
}

Retrieve the inner most exception.

  var foo = new NotImplementedException("Foo");
  var bar = new NotImplementedException("Bar", foo);

  var inner = bar.InnerMost();
  // inner == foo

Build a string of object properties.

	var testObject = new
	{
		Foo = "Baz",
		Identity = 42,
		NestedValues = new[]
		{
			"First",
			"Second",
			"Thrid"
		},
		NestedObject = new
		{
			Inner = "InnerValue",
			InnerIdentity = 47
		}
	};

	var details = testObject.ToDetailedString("testObject");
	/* details =
		(<>f__AnonymousType1`4) [<>f__AnonymousType1`4] : testObject
			(String) [Foo] : Baz
			(Int32) [Identity] : 42
			(String[]) [String[]] : NestedValues
				(String) : First
				(String) : Second
				(String) : Thrid
			(<>f__AnonymousType0`2) [<>f__AnonymousType0`2] : NestedObject
				(String) [Inner] : InnerValue
				(Int32) [InnerIdentity] : 47
	*/

Convert enumerations to collections.

  var foo = new [] { 1, 2, 3, 4 };
  var bar = foo
    .Where(val => val > 2)
    .ToCollection();

  // bar == Collection<int> { 3, 4 };

Perform an action on an enumeration.

  var foo = new [] { 1, 2, 3, 4 };

  foo.Select(val => val.ToString())
    .ForEach(Console.Write);

 // Console: 1234

Retrieve the default value of a given type.

  var foo = typeof(string).DefaultValue();
  var bar = typeof(int).DefaultValue();

  // foo == null
  // bar == 0

  foo = DefaultValue<string>();
  bar = DefaultValue<int>();

  // foo == null
  // bar == 0

Build

You can build the project using Visual Studio or by running the grunt tasks for msbuild

Contribute

This project uses hylasoft/cs-boilerplate to define tasks and stle guides. Please read the readme of the project to learn more about how to contribute.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.