Re: [問題] ref跟指標

看板C_Sharp (C#)作者 (.)時間7年前 (2018/03/20 22:10), 7年前編輯推噓2(203)
留言5則, 3人參與, 7年前最新討論串2/2 (看更多)
※ 引述《justinj (黑旋風)》之銘言: : public struct AAA : { : public string BBB; : public string BBB2; : public string BBB3; : public string BBB4; : } : public struct CCC : { : public AAA C1; : public AAA C2; : public AAA C3; : public AAA C4; : public AAA C5; : } : public static void func(ref CCC data,int a) : { : AAA[] loc_data=new AAA[] : { : data.C1,data.C2,data.C3,data.C4,data.C5 : }; : if(a<5 && a>=0) : { : loc_data[a].BBB="test1"; : } : } : =============================================== : 請問一下C#有沒有法子做到類似指標的動作, : 目前想不到在函數內將資料設定進去的方法 可以用全C指標的寫法 來達成 不過很怪異 按照你的需求 有更快的作法是 set 或是 get field by name 利用 reflection 就可以 google 關鍵字 c# struct set filed reflection 參考 https://tinyurl.com/yas7rmoy 基本上可以改成 if(a<5 && a>=0) { 設定 data 裡頭 一個名為 "C"+a.ToString() 的 filed 內容成 test1 } reflection平常用不到不知道哪裡好用也不知道作啥用的 用到時候才覺得真是好物 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.69.91.220 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1521555014.A.2C8.html

03/20 23:23, 7年前 , 1F
反射其中之一的用途,就是用來做一些感覺手作很蠢的事
03/20 23:23, 1F

03/21 11:23, 7年前 , 2F
謝謝提供
03/21 11:23, 2F

03/21 11:30, 7年前 , 3F
reflection本來就慢了,配struct還要box unbox copy...
03/21 11:30, 3F

03/21 11:31, 7年前 , 4F
如果不是真的field太多,我是會選比較蠢的手作法啦..
03/21 11:31, 4F

03/21 11:39, 7年前 , 5F
當然實際上要看原po的應用場景這邊需不需要考量效能
03/21 11:39, 5F
還是得看實際的狀況來決定 至於效能的話 實測才準 極端情況下全改成unmanaged寫法 當然是很怪異的下下策就是 ※ 編輯: erspicu (60.248.56.185), 03/21/2018 12:24:52
文章代碼(AID): #1QiHP6B8 (C_Sharp)
討論串 (同標題文章)
本文引述了以下文章的的內容:
1
14
完整討論串 (本文為第 2 之 2 篇):
1
14
文章代碼(AID): #1QiHP6B8 (C_Sharp)