ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Say

Speaks any object using the speech synthesis API

Source

public static T Say<T>(this T thing) {
	const int enumerableLimit = 100;
	
	object obj = thing;
	if(obj == null) {
		obj = "null reference";
	}
	
	var enumerable = obj as IEnumerable;
	if(enumerable != null && obj.GetType() != typeof(string)) {
		var items = enumerable.OfType<object>().Take(enumerableLimit + 1).ToList();
		if(items.Count == enumerableLimit + 1) {
			items[enumerableLimit] = "and so on";
		}
		foreach(var x in items) {
			Say(x);
		}
	} else {
		using(var synth = new System.Speech.Synthesis.SpeechSynthesizer()) {
			synth.Speak(obj.ToString());
		}
	}
	return thing;
}

Example

var files = Say(Directory.GetFiles("c:\\"));
Say("and the total size is");
var size = files.Select(x => new FileInfo(x).Length).Sum();
Say(size);
Say("bytes");

Author: totally professional rock star coder

Submitted on: 11 mrt. 2015

Language: C#

Type: T

Views: 4640