亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 C# 中,刪除對象的 Action 會發生什么?

在 C# 中,刪除對象的 Action 會發生什么?

PHP
一只萌萌小番薯 2024-01-21 10:05:50
我想為我的 TreeNode 類進行深度復制。這是我的代碼:    public TreeNode(TreeNode node, GUIStyle inPointStyle, GUIStyle outPointStyle, Action<ConnectionPoint> OnClickInPoint, Action<ConnectionPoint> OnClickOutPoint){    this.rect = new Rect(node.rect);    this.style = new GUIStyle(node.style);    this.inPoint = new ConnectionPoint(this, ConnectionPointType.In, inPointStyle, OnClickInPoint);    this.outPoint = new ConnectionPoint(this, ConnectionPointType.Out, outPointStyle, OnClickOutPoint);    this.defaultNodeStyle = new GUIStyle(node.defaultNodeStyle);    this.selectedNodeStyle = new GUIStyle(node.selectedNodeStyle);    this.allDecorations = new List<GameObject>(node.allDecorations);    this.objs = new Dictionary<GameObject, IndividualSettings>(node.objs);    this.name = String.Copy(node.name);    this.RemoveClonedObj = new Action(node.RemoveClonedObj);    this.OnChangeView = new Action<TreeNode>(node.OnChangeView);    this.OnRemoveNode =  new Action<TreeNode>(node.OnRemoveNode);    this.OnCopyNode = new Action<TreeNode>(node.OnCopyNode);    this.PreviewTree = new Action<TreeNode, bool> (node.PreviewTree);}然而,騎士給了我警告:看來騎士是在說我的“新”毫無意義。如果我遵循 Rider 的指示,this.RemoveClonedObj = node.RemoveClonedObj;在刪除原始 TreeNode 后,使用我復制的 TreeNode 的操作會發生什么?他們也會被移除嗎?如果是這樣,為什么 Rider 會給我這樣的警告?
查看完整描述

2 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

在 C# 2.0 或更高版本中,以下代碼是等效的(DelegateType是委托類型,顧名思義):

newDelegate?=?new?DelegateType(oldDelegate);
newDelegate?=?oldDelegate;

此類操作將始終創建 的新實例DelegateType,該實例與oldDelegate.?它們并不引用同一個對象(不要被=賦值所混淆):

new D(E) 形式的 delegate_creation_expression(其中 D 是 delegate_type,E 是表達式)的綁定時處理由以下步驟組成:

  • 如果 E 是方法組,則委托創建表達式的處理方式與從 E 到 D 的方法組轉換(Method group conversions)相同。

  • 如果 E 是匿名函數,則委托創建表達式的處理方式與從 E 到 D 的匿名函數轉換(匿名函數轉換)相同。

  • 如果 E 是一個值,則 E 必須與 D 兼容(委托聲明),并且結果是對新創建的 D 類型委托的引用,該委托引用與 E 相同的調用列表。如果 E 與 D 不兼容,則會發生編譯時錯誤。

所以關于你的問題

刪除原始 TreeNode 后,我復制的 TreeNode 的操作會發生什么?他們也會被移除嗎?

他們不會有什么事。它們不會被刪除。


順便說一句,由于您正在嘗試對樹節點進行深層復制,因此我懷疑這是否是正確的方法。盡管您創建了委托的新實例,但與其關聯的類實例(將調用成員方法的實例)保持不變。


查看完整回答
反對 回復 2024-01-21
?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

不要將實例方法相互鏈接。這會導致內存泄漏。


即使在原始節點被刪除并且代碼不再需要之后,由于副本的引用,原始實例將存在于內存中并且不會被垃圾收集。


我懷疑這不是你想要的,測試代碼


class Program

{

    static void Main(string[] args)

    {

        First t = new First();

        Second s = new Second();

        t.Print = s.TestMethod;

        s.test = "change";

        s = null;

        t.Print("Hell"); // can debug and see that the function call goes through and string test is = "change"

    }

}


public class First

{

    public string s;

    public Action<string> Print;

}

public class Second

{

    public string test = "created";

    public void TestMethod (string test)

    {

        var res = "hello" + test + test;

    }

}

節點上的方法應該是 Node 對象的一部分,這樣您就不必將它們分配給新節點,或者它們應該位于單獨的類中,最好是靜態的,以便創建新節點不會導致內存問題。


查看完整回答
反對 回復 2024-01-21
  • 2 回答
  • 0 關注
  • 273 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號