https://referencesource.microsoft.com/# ... neric/list. cs,2765070d40f47b98
Mein Code hier:
Code: Select all
private UIVertex[] GetArray(List verts)
{
var aryF = typeof(List).GetField("_items", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
return aryF.GetValue(verts) as UIVertex[];
}
private void SetSize(List list, int size)
{
sizeF = typeof(List).GetField("_size",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
}
static List verts
static UIVertex[] ary;
//...
ary = GetArray(verts);
Die Kosten für die Reflexion sind zu hoch. Kann ich die Referenz einfach behalten? Wie mache ich das?
PS:
Wenn ich List.Add oder List.Get verwende, ist es ein Auslöser String.memcpy(), es ist zu langsam, deshalb möchte ich das Array direkt verwenden.
Und weil ich List wiederverwende, ändert sich die Array-Länge also nicht regelmäßig. Ich kann es einfach beibehalten, um schnell zu sein.
PS 2.
Hier ist vorher zu ändern:
Code: Select all
static List Orgin = new List(128);
void DO() // *it call a lot of times.*
{
GetListByAPI(Orgin);// here is API , So it must insert List.
int Size = Orgin.Count() * 5; // size is unchanged in here.
if(Orgin.Capacity < Size)
{
Orgin.Capacity = Size;
}
for(int i = Orgin.Count() ; i < Size ;i++)
{
T t
Orgin.Add(t);// I need "Oring.Count() * 5 " Size Array for my logic.
//...
}
}
Code: Select all
static List Orgin = new List(128);
static T[] StaticAry;
void DO() // *it call a lot of times.*
{
bool LastOringCapacity = Orgin.Capacity;
GetListByAPI(Orgin);// here is API , So it must insert List.
bool arrayRefChanged = Orgin.Capacity > LastOringCapacity ; // I Can know it. and get array again.
int Size = Orgin.Count() * 5; // size is unchanged in here.
SetSize(Oring, Size);//here set array legth of list by refection
StaticAry = GetArray(Orgin);//here I get array of list by reflection
for(int i = 0 ; i < size; i++)
{
T t = StaticAry[i];//just use it , avoid to use List.Add api.
//....
}
}
Aber jetzt. Ich muss Reflection verwenden, um in jedem einzelnen DO() ein Array zu erhalten.
Die Komplexität liegt darin
Code: Select all
List.Capacity