第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 C# 中,刪除對(duì)象的 Action 會(huì)發(fā)生什么?

在 C# 中,刪除對(duì)象的 Action 會(huì)發(fā)生什么?

PHP
一只萌萌小番薯 2024-01-21 10:05:50
我想為我的 TreeNode 類進(jìn)行深度復(fù)制。這是我的代碼:    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);}然而,騎士給了我警告:看來(lái)騎士是在說(shuō)我的“新”毫無(wú)意義。如果我遵循 Rider 的指示,this.RemoveClonedObj = node.RemoveClonedObj;在刪除原始 TreeNode 后,使用我復(fù)制的 TreeNode 的操作會(huì)發(fā)生什么?他們也會(huì)被移除嗎?如果是這樣,為什么 Rider 會(huì)給我這樣的警告?
查看完整描述

2 回答

?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

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

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

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

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

  • 如果 E 是匿名函數(shù),則委托創(chuàng)建表達(dá)式的處理方式與從 E 到 D 的匿名函數(shù)轉(zhuǎn)換(匿名函數(shù)轉(zhuǎn)換)相同。

  • 如果 E 是一個(gè)值,則 E 必須與 D 兼容(委托聲明),并且結(jié)果是對(duì)新創(chuàng)建的 D 類型委托的引用,該委托引用與 E 相同的調(diào)用列表。如果 E 與 D 不兼容,則會(huì)發(fā)生編譯時(shí)錯(cuò)誤。

所以關(guān)于你的問(wèn)題

刪除原始 TreeNode 后,我復(fù)制的 TreeNode 的操作會(huì)發(fā)生什么?他們也會(huì)被移除嗎?

他們不會(huì)有什么事。它們不會(huì)被刪除。


順便說(shuō)一句,由于您正在嘗試對(duì)樹節(jié)點(diǎn)進(jìn)行深層復(fù)制,因此我懷疑這是否是正確的方法。盡管您創(chuàng)建了委托的新實(shí)例,但與其關(guān)聯(lián)的類實(shí)例(將調(diào)用成員方法的實(shí)例)保持不變。


查看完整回答
反對(duì) 回復(fù) 2024-01-21
?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

不要將實(shí)例方法相互鏈接。這會(huì)導(dǎo)致內(nèi)存泄漏。


即使在原始節(jié)點(diǎn)被刪除并且代碼不再需要之后,由于副本的引用,原始實(shí)例將存在于內(nèi)存中并且不會(huì)被垃圾收集。


我懷疑這不是你想要的,測(cè)試代碼


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;

    }

}

節(jié)點(diǎn)上的方法應(yīng)該是 Node 對(duì)象的一部分,這樣您就不必將它們分配給新節(jié)點(diǎn),或者它們應(yīng)該位于單獨(dú)的類中,最好是靜態(tài)的,以便創(chuàng)建新節(jié)點(diǎn)不會(huì)導(dǎo)致內(nèi)存問(wèn)題。


查看完整回答
反對(duì) 回復(fù) 2024-01-21
  • 2 回答
  • 0 關(guān)注
  • 258 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)