Skip to content

Using UnsafeAccessor to access List<T>._items #110964

Answered by KeterSCP
sfiruch asked this question in Q&A
Discussion options

You must be logged in to vote

The answer is the same as in #110054 (comment)

In your case, T should be type parameter and not method parameter, as in List<T> T - is a type parameter.
Following code works as expected:

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

var list = new List<string>() { "ahoyhoy" };

var internalItemsNew = Accessors<string>.GetListItems(list);
Console.WriteLine(internalItemsNew.Length);

class Accessors<T>
{
    [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_items")]
    public extern static ref T[] GetListItems(List<T> list);
}

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by sfiruch
Comment options

You must be logged in to vote
2 replies
@sfiruch
Comment options

@hamarb123
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants