Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PtrAsArrayWrapper<T> : IEnumerable<T> where T : unmanaged
- {
- private LargeIntPtr Ptr { get; set; }
- long _count;
- private unsafe T* data;
- public long Count
- {
- get => _count;
- }
- public PtrAsArrayWrapper(LargeIntPtr ptr)
- {
- this.Ptr = ptr;
- _count = ptr.Size / System.Runtime.InteropServices.Marshal.SizeOf(typeof(T));
- unsafe
- {
- data = (T*) Ptr.Ptr.ToPointer();
- }
- }
- public T this[long index]
- {
- get
- {
- unsafe
- {
- return *(data + index);
- }
- }
- set
- {
- unsafe
- {
- *(data + index) = value;
- }
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- for (long i = 0; i < Count; i++)
- {
- yield return this[i];
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement