有没有办法通过引用传递属性或字段以在函数中更新它?

WDUK

我有一小段代码将节点分配给名为 NodeA 和 NodeB 的字段。但是代码相当重复,所以感觉我可以将它简化为一个函数。但我想知道是否可以引用类中的实际字段以分配给特定字段?

我的字段/属性设置如下:

public class Test {

    private Node _nodeA;
    private Node _nodeB;

    public void Set(Node nodeA, Node nodeB){

        if (_nodeA != null)
            _nodeA.OnChange -= Update;
        _nodeA = nodeA;
        _nodeA.OnChange += Update;

        if (_nodeB != null)
            _nodeB.OnChange -= Update;
        _nodeB = nodeB;
        _nodeB.OnChange += Update;  

    }

    void Update(){}//todo

}

我想把它放到一个简单的函数中,这样我就可以传入节点,但也可以传递一个指向我想要分配给它的字段的引用......沿着这个想法(伪代码)

Set(Node node, /* pointer to _nodeA or _nodeB */ ptr) 
{
   if(ptr != null) ptr.OnChange -= Update;
   ptr = node;
   ptr.OnChange += Update;
}
Set(nodeA, /* pointer to field _nodeA */ );
Set(nodeB, /* pointer to field _nodeB */ );

Set是否可以像在 C# 中一样创建函数?如果是这样,我如何将这样的引用传递给这样的字段?

枪2171

使用ref关键字。这样您就可以更改基础分配。

这是一个简化版本:

using System;
                    
public class Program
{
    public static void Main()
    {
        // print the original node values
        Console.WriteLine(Test._nodeA.Message);
        Console.WriteLine(Test._nodeB.Message);
        
        // modify the two nodes using a brand new instance
        Test.Set(ref Test._nodeA, new Node { Message = "modified a" });
        Test.Set(ref Test._nodeB, new Node { Message = "modified b" });
        
        // print out the new node data
        Console.WriteLine(Test._nodeA.Message);
        Console.WriteLine(Test._nodeB.Message);
    }
}

public static class Test
{
    // these need to be public so that the code CALLING "Set" can use these fields
    public static Node _nodeA = new Node { Message = "start a" };
    public static Node _nodeB = new Node { Message = "start b" };
    
    public static void Set(ref Node target, Node newVal)
    {
        // all this does is simple reassignment, but it's to the
        // same reference location in memory that _nodeA/_nodeB holds.
        // you can add more complex logic as well.
        target = newVal;

        // because target and newVal have the same memory ref
        // you can modify them interchangeably and they effect each other
        target.Message += "!";
        newVal.Message += "?";
    }
}

public class Node
{
    public string Message {get;set;}
}

Set方法的第一个参数使用ref,它允许您在其原始位置重新分配传递的实例。

这打印

开始a
开始b
修改a!?
修改了b!?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法通过引用传递属性或字段以在函数中更新它?

有没有办法在C ++中通过引用传递右值?

有没有办法将对象的字段隐式传递给C ++中的函数

有没有办法在活动之间传递函数引用?

使用.net core和mongodb,有没有办法只更新我在json中传递的字段?

有没有办法通过两个函数传递可选参数?

有没有办法通过pthread将2D数组传递给函数

有没有办法通过dplyr的`do`函数传递附加参数的向量?

有没有办法在通过构造函数传递值后保留数据?

有没有办法解构传递给 onClick 函数的数据属性开玩笑?

有没有办法更新次要属性?

有没有办法遍历传递给Elixir中的函数的所有参数?

有没有办法在vue中引用图像?

有没有办法在c#中通过名称访问变量的引用

有没有办法在函数调用中更改对对象的原始引用

有没有办法在打字稿定义中引用函数自变量?

有没有办法在TypeScript函数文档中引用参数?

有没有办法通过 JMeter 中的 csv 文件传递用户定义的变量?

有没有办法通过外壳中的参数传递语句?

有没有办法在 F# 中通过字符串获取记录字段?

有没有办法将 v-for 中的对象属性传递给 Vue 数据?

有没有办法在 React 中传递/迭代数组作为样式属性?

有没有办法将哈希数组传递给 Chef 中的提供程序函数

有没有办法使用python通过elementtree中的id属性过滤xml

有没有办法通过 C# 中的类的属性来限制 foreach 循环?

有没有办法重新分配R访问器函数并使用它来更新它访问的变量属性?

有没有办法通过破折号中的滑块更新间隔?

有没有办法指定通过 mysql 中的触发器更新哪些列?

有没有办法在C ++中复合函数?