题
我的问题是,下面decodedProxyExcerpt2的分配覆盖decodedProxyExcerpt1,我不知道为什么。
任何线索?
预先感谢。
DecodedProxyExcerpt decodedProxyExcerpt1 = new DecodedProxyExcerpt(stepSize);
if (audiofactory.MoveNext(stepSize))
{
decodedProxyExcerpt1 = audiofactory.Current(stepSize);
}
// At this point decodedProxyExcerpt1.data contains the correct values.
DecodedProxyExcerpt decodedProxyExcerpt2 = new DecodedProxyExcerpt(stepSize);
if (audiofactory.MoveNext(stepSize))
{
decodedProxyExcerpt2 = audiofactory.Current(stepSize);
}
// At this point decodedProxyExcerpt2.data contains the correct values.
// However, decodedProxyExcerpt1.data is overwritten and now holds the values of decodedProxyExcerpt2.data.
public class DecodedProxyExcerpt
{
public short[] data { get; set; } // PCM data
public DecodedProxyExcerpt(int size)
{
this.data = new short[size];
}
}
从AudioFactory:
public bool MoveNext(int stepSize)
{
if (index == -1)
{
index = 0;
return (true);
}
else
{
index = index + stepSize;
if (index >= buffer.Length - stepSize)
return (false);
else
return (true);
}
}
public DecodedProxyExcerpt Current(int stepSize)
{
Array.Copy(buffer, index, CurrentExcerpt.data, 0, stepSize);
return(CurrentExcerpt);
}}
解决方案
类的实例被存储作为参考。
和decodedProxyExcerpt1是decodedProxyExcerpt2对同一个对象的引用都 - audiofactory.CurrentExcerpt
。其他提示
从它audiofactory.MoveNext(stepSize)
停留在相同的参考的样子。这是造成audiofactory.Current(stepSize)
留在相同的地址。
出于这个原因,但decodedProxyExcerpt1
和decodedProxyExcerpt2
指向相同的参考,并且因此改变为一个传播到其他
因此,问题在于你AudioFactory
类。
我问一个朋友关于谁给我说,我可能已经用C想着提示++当阵列的赋值创建一个副本,而不是C#,其中一个数组分配创建了一个参考。
如果这是正确的和
decodedProxyExcerpt1 = audiofactory.Current(stepSize的);
时,设定基准(不是复制),则重写是完全可以理解。
不隶属于 StackOverflow