无法从'ref byte []'转换为'ref object' [英] cannot convert from 'ref byte[]' to 'ref object'
问题描述
我尝试读取图像中某一行的像素值:
I try to read the pixels value on a line in an image:
byte[] myArray=new byte[20];
for(int i=0;i<20;i++)
myArray[i]=0;
myImage.GetLine(ref myArray, 10,10,20,10);
Getline是myImage提供的函数,它有5个参数:
ref对象,int startX,int startY,int endX, int EndY;
运行上面的代码时,它会显示错误:无法将'ref byte []'转换为'ref object'。
但是当我用这种方式调用它时:
Getline is the function provided by myImage, which has 5 parameters:
ref object, int startX,int startY, int endX,int EndY;
When run the above code, it will show error as: cannot convert 'ref byte[]' to 'ref object'.
But when I call it in this way:
object points=null;
myImage.GetLine(ref points,10,10,20,10);
The error will show as: invalid userarray.
不知道为什么?
Don't know why?
推荐答案
你不能用ref参数调用方法到 以外的任何类型 指定的参数类型。推理非常简单 - 假设你可以:
You cannot call the method with a ref parameter to any type other than exactly the parameter type specified. The reasoning is pretty simple - suppose you could:
private void MyMethod(ref object o)
{
o = (object) new Point(10, 10);
}
...
byte[] data = new byte[1024];
MyMethod(ref data);
Console.WriteLine(data[100]);
你会得到什么样的输出?除了随机和垃圾?
为了使用ref参数调用方法,它必须是 可分配 值,所以请尝试
What kind of output would you get? Apart from random and rubbish?
In order to call a method with a ref parameter, it must be an assignable value, so try
byte[] myArray=new byte[20];
for(int i=0;i<20;i++)
myArray[i]=0;
object o = myArray;
myImage.GetLine(ref o, 10,10,20,10);
该方法可以然后更改o的值而不会以任何方式影响数组。
The method can then change the value of o without affecting the array in any way.
似乎需要从myArray到Object的强制转换。
我想得到的是鼠标在图像上移动时获取像素值。
谢谢。我稍后会试一试。
Seems it will need a cast from myArray to Object.
What I want to get is to get a pixel's value when mouse moved on an image.
Thank you. I will try this later.
这篇关于无法从'ref byte []'转换为'ref object'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!