由于各种大的性能优势(在我的情况下),我发现自己处于一个我必须推出自己的动态数组实现的位置.但是,在为我的版本创建一个枚举器,并将效率与List使用的比较后,我有点困惑; List one比我的版本大约快30-40%,尽管它要复杂得多.
这是List枚举器实现的重要部分:
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{
private List<T> list;
private int index;
private int version;
private T current;
internal Enumerator(List<T> list)
{
this.list = list;
this.index = 0;
this.version = list._version;
this.current = default(T);
return;
}
public bool MoveNext()
{
List<T> list;
list = this.list;
if (this.version != list._version)
{
goto Label_004A;
}
if (this.index >= list._size)
{
goto Label_004A;
}
this.current = list._items[this.index];
this.index += 1;
return 1;
Label_004A:
return this.MoveNextRare();
}
public …Run Code Online (Sandbox Code Playgroud) 所以,我在摆弄和学习反思时遇到了一个奇怪的问题.我正在尝试更改一个私有的readonly字段,如下所示:
public class A
{
private static readonly int x;
public int X
{
get { return x; }
}
}
static void Main(string[] args)
{
A obj = new A();
Type objType = typeof(A);
Console.WriteLine(obj.X);
FieldInfo objField = objType.GetField("x", BindingFlags.Static | BindingFlags.NonPublic);
objField.SetValue(null, 100);
Console.WriteLine(obj.X);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
如果我按原样运行程序,那么每次都会打印0到控制台.但是,如果我注释掉第一个印刷品,那么第二个印刷品会写出预期的100个印刷品.
谁可以了解这里发生的事情?谢谢!
编辑:扼杀它似乎在Visual Studio 2012中工作,但不是在2010年.据我所知,两者的设置是相同的.
编辑2:使用平台目标x64构建时工作,而不是使用x86.猜猜新问题是:为什么?
编辑3:在反汇编中比较x64和x86版本; 在x86版本中似乎有一些内联.
编辑4:哦,我想我已经弄清楚发生了什么,有点像.我不认为A类中的属性是内联的问题.我相信当在main方法中第二次读取属性时,属性调用被优化掉了(后备字段应该是readonly,值应该相同)并且旧值被重用.这至少是我的"理论".
可以说我有这样的界面:
public interface MyInterface
{
int Property1
{
get;
}
void Method1();
void Method2();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法强制接口的实现者明确地实现它的一部分?就像是:
public interface MyInterface
{
int Property1
{
get;
}
explicit void Method1();
explicit void Method2();
}
Run Code Online (Sandbox Code Playgroud)
编辑:至于为什么我关心界面是否明确实现; 就功能而言,它并不重要,但是使用代码隐藏一些不必要的细节可能会有所帮助.
我正试图在我的系统中模仿多重继承,使用这种模式:
public interface IMovable
{
MovableComponent MovableComponent
{
get;
}
}
public struct MovableComponent
{
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
public int Method1()
{
// Implementation
}
public int Method2()
{
// Implementation
}
}
public static IMovableExtensions
{
public static void …Run Code Online (Sandbox Code Playgroud)