Object - As
An object extension method that cast anonymous type to the specified type.
public static void Main() { var intObject = (object) 13; var stringObject = (object) "FizzBuzz"; var arrayObject = (object) new[] {"Fizz", "Buzz"}; // C# Extension Method: Object - As var intValue = intObject.As<int>(); var stringValue = stringObject.As<string>(); int arrayCount = arrayObject.As<string[]>().Length; Console.WriteLine(intValue); Console.WriteLine(stringValue); Console.WriteLine(arrayCount); }
View Source
public static partial class Extensions { /// <summary> /// An object extension method that cast anonymous type to the specified type. /// </summary> /// <typeparam name="T">Generic type parameter. The specified type.</typeparam> /// <param name="this">The @this to act on.</param> /// <returns>The object as the specified type.</returns> public static T As<T>(this object @this) { return (T) @this; } }