C# 将Texture2d对象发送到函数

C# 将Texture2d对象发送到函数,c#,function,xna-4.0,texture2d,C#,Function,Xna 4.0,Texture2d,我试图向函数发送一些对象,但C#似乎不喜欢这样。这是代码 string[] bkgrSource = new string[12]; Texture2D[] bkgrBANK = new Texture2D[12]; 其中bkgrSource[0]是一个文件名数组,bkgrBANK[0]是一个Texture2D数组 此功能不起作用。bkgrBANK[0]将保持为空。有什么帮助吗 commonImageLoader( bkgrSource[0], bkgrBANK[0] ); private

我试图向函数发送一些对象,但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 );
        }
    }
}
我不是C#guru,但我认为关键是您正在按值传递参数(默认方法调用行为), 因此,函数中的源参数和目标参数是原始参数的副本

我想你可以通过参数来解决这个问题。 这可能会起作用:

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 );
        }
    }
}
我不是C#guru,但我认为关键是您正在按值传递参数(默认方法调用行为), 因此,函数中的源参数和目标参数是原始参数的副本

我想你可以通过参数来解决这个问题。 这可能会起作用:

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 );
        }
    }
}