Hi all, I'd like to pin IEnumerable implementations in memory with this generic extension method I'm working on. It seems to work fine with arrays but fails with other sequences (lists and collections). here's the method implementation.
// <summary>
/// Pins an IEnumerable of type T in memory
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sequence"></param>
/// <returns></returns>
public static GCHandle Pin<T>(this IEnumerable<T> @sequence)
{
return GCHandle.Alloc(@sequence, GCHandleType.Pinned);
}
Why does it fail for some types but work for others?Could you explain the concept behind this? Is there a better way to do this than my generic approach? Thank you.