我知道有很多关于此错误的问题,但我还没有找到解决此情况的人:
我有一个只读属性,并且在一个方法中我想使用它(而不是分配它),因此错误的解决方案不能是典型的解决方案:
var a = myOjb.MyProp;
DoSomething(a);
myObj.MyProp = a;
因为代码无法编译,例如 MyProp 是只读的,无法赋值。
基本上我想要的是在这样的方法中编写响应流属性:
public ActionResult Guardar()
{
try
{
SomeMethod(ref Response.OutputStream);
Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", name));
Response.ContentType = "Application/pdf";
return null;
}
catch { }
}
public void SomeMethod(ref Stream responseStream)
{
responseStream.Write(new byte[] { 1, 2, 5, 3 }, 0, 4);
}
我该怎么做?
谢谢,迭戈
注意:代码只是一个示例。
最佳答案
您应该将该参数更改为普通参数。
无论如何,您(可能)不需要 ref
参数。
有关通过引用传递的更多信息,请参阅 here .
如果您确实需要一个 ref
参数,您可以将该对象放入本地可写变量中,并将该变量作为 ref
传递。
然而,这将违背该参数的全部目的。
关于c# - 属性、索引器或动态成员访问不能作为具有只读属性的 out 或 ref 参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6492097/