1

我尝试将一些对象发送到函数,但似乎 C# 不喜欢那样。这是代码。

string[] bkgrSource = new string[12];
Texture2D[] bkgrBANK = new Texture2D[12];

bkgrSource[0]是一个文件名数组,bkgrBANK[0]是一个Texture2D.

此功能不起作用。bkgrBANK[0]遗嘱仍然是空的。有什么帮助吗?

commonImageLoader( bkgrSource[0], bkgrBANK[0] ); 

private void commonImageLoader(string source, Texture2D destination ) {
    if ( !string.IsNullOrEmpty( source ) ) {
        fileName = source;
        using ( fileStream = new FileStream( @fileName, FileMode.Open ) ) {
        destination = Texture2D.FromStream( GraphicsDevice, fileStream );
        }
    }
}
4

1 回答 1

1

我不是 C# 专家,但我认为关键是您按值传递参数(默认方法调用行为),因此函数中的源参数和目标参数是原始参数的副本。

我认为您可以通过引用传递参数来解决此问题。可能这应该工作:

commonImageLoader( ref bkgrSource[0], ref bkgrBANK[0] ); 

private void commonImageLoader(ref string source, ref Texture2D destination ) {
    if ( !string.IsNullOrEmpty( source ) ) {
        fileName = source;
        using ( fileStream = new FileStream( @fileName, FileMode.Open ) ) {
        destination = Texture2D.FromStream( GraphicsDevice, fileStream );
        }
    }
}
于 2012-08-05T18:00:24.683 回答