Passing an explicit cast as a ref parameter (C#)

泪湿孤枕 提交于 2019-12-10 12:46:54

问题


I have a class that is mostly a wrapper for a big array and some associated housekeeping. I have a function that takes a ref parameter. When I pass an instance of the class into the function, I want the array to get sent.

I considered explicit casts. Let's say I have some function that has a byte[] ref parameter.

    public void SomeFunction(ref byte[] someBytes);

And that I have some class with an overloaded explicit cast.

    class SomeClass
    {
        byte[] someBytes;
        public static explicit operator byte[](SomeClass someInstance)
        {
            return someInstance.someBytes;
        }
    }

Now I want to call the function with the class as a parameter

    SomeClass someInstance = new SomeClass();
    SomeFunction(ref (byte[]) someInstance);

The compiler complains "a ref or out argument must be an assignable variable". I'm not sure if I'm just failing to massage the compiler properly or if you really just can't do that.

I considered a Property or function return value, but you can't pass those by ref (and after educating myself I see why...)

I'd prefer not to make the array a public field, but that does satisfy the compiler. I suppose I could just create a local variable to reference the array with, but that's an extra line of code before and after each function call...

EDIT: it might be worth noting that SomeFunction was written by a third party and I don't have access to change it. Worse, I don't think their parameter actually needs to be ref...


回答1:


A cast is not an assignable variable; you're passing a return value from your explicit cast operator.

You can create a variable holding the properly cast value before passing it as a ref:

SomeClass someInstance = new SomeClass();
byte[] someBytes = (byte[])someInstance;
SomeFunction(ref someBytes);

Note that it is now the someBytes variable which may be reassigned. You will have to take action to reassign someInstance.someBytes in some way after the call to SomeFunction if you want the internal value of someInstance to be reassigned.




回答2:


You can't do that, you need to materialize the result of casting your object into a variable first.

Consider the following code, if what you ask for was allowed:

public void SomeFunction(ref byte[] someBytes)
{
    someBytes = new byte[] { 1, 2, 3 };
}

SomeClass someInstance = new SomeClass();
SomeFunction(ref (byte[]) someInstance);
// uh-oh, what is "someInstance" now?

Why is the argument marked "ref", what problem is it that you're trying to solve here?




回答3:


The scope of values that can be used as a ref parameter in C# is limited by what the CLR allows. It's specified in CLI spec at sections 12.4.1.5.2 and 12.1.6.1 and includes

  • Argument of the current method
  • Local variable
  • Member Field of an object
  • Static Field
  • Array Element

A cast does not fit any of these and hence cannot be used as ref value. You'll need to assign it to a value like a local before passing it by reference.




回答4:


Do you really have to cast the ref parameter? In my case something like this works fine.

byte[] someInstance = (byte[])new SomeClass(); 
SomeFunction(ref someInstance);


来源:https://stackoverflow.com/questions/2165892/passing-an-explicit-cast-as-a-ref-parameter-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!