[{"data":1,"prerenderedAt":1267},["ShallowReactive",2],{"blog:2006:implementingweakreferencet":3,"blogMore-Development":1139,"comments-implementingweakreferencet":1152},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":15,"body":48,"_type":1131,"_id":1132,"_source":1133,"_file":1134,"_stem":1135,"_extension":1136,"url":1137,"wordCount":1138,"minutes":164,"commentCount":208},"/blog/2006/implementingweakreferencet","2006",false,"en","Implementing a generic WeakReference\u003CT> in C#","A weak reference lets you hold a reference to an object that will not prevent it from being garbage collected. There are a few scenarios where this might be important, such as listening for events, caching, various MVC patterns.","2006-08-01T16:51:32+00:00","Development",[13,14],".NET","C#",{"type":16,"children":17},"root",[18,35],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23,26,33],{"type":24,"value":25},"text","Check out the replacement ",{"type":19,"tag":27,"props":28,"children":30},"code",{"className":29},[],[31],{"type":24,"value":32},"EquatableWeakReference\u003CT>",{"type":24,"value":34}," class",{"type":19,"tag":20,"props":36,"children":37},{},[38,40,42,44,46],{"type":24,"value":39},"A ",{"type":24,"value":41},"weak reference",{"type":24,"value":43}," lets you hold a reference to an object that will not prevent it from being garbage collected. There are a few scenarios where this might be important, such as listening for events, caching, various ",{"type":24,"value":45},"MVC patterns",{"type":24,"value":47},".",{"type":16,"children":49,"toc":1129},[50,69,88,102,115,127,1086,1114,1123],{"type":19,"tag":51,"props":52,"children":53},"blockquote",{},[54],{"type":19,"tag":20,"props":55,"children":56},{},[57,58],{"type":24,"value":25},{"type":19,"tag":59,"props":60,"children":62},"a",{"href":61},"/blog/2006/equatable_weak_references/",[63,68],{"type":19,"tag":27,"props":64,"children":66},{"className":65},[],[67],{"type":24,"value":32},{"type":24,"value":34},{"type":19,"tag":20,"props":70,"children":71},{},[72,73,80,81,87],{"type":24,"value":39},{"type":19,"tag":59,"props":74,"children":78},{"href":75,"rel":76},"https://en.wikipedia.org/wiki/Weak_reference",[77],"nofollow",[79],{"type":24,"value":41},{"type":24,"value":43},{"type":19,"tag":59,"props":82,"children":85},{"href":83,"rel":84},"https://en.wikipedia.org/wiki/Model-view-controller",[77],[86],{"type":24,"value":45},{"type":24,"value":47},{"type":19,"tag":20,"props":89,"children":90},{},[91,93,100],{"type":24,"value":92},".NET and Java supports weak references with the aptly named ",{"type":19,"tag":59,"props":94,"children":97},{"href":95,"rel":96},"https://windowssdk.msdn.microsoft.com/en-us/library/system.weakreference.aspx",[77],[98],{"type":24,"value":99},"WeakReference",{"type":24,"value":101}," class. In .NET it exposes a .Target property of type object that weakly points to whatever you like.",{"type":19,"tag":20,"props":103,"children":104},{},[105,107,113],{"type":24,"value":106},"Java also sports a generic ",{"type":19,"tag":27,"props":108,"children":110},{"className":109},[],[111],{"type":24,"value":112},"WeakReference\u003CT>",{"type":24,"value":114}," to give you a strongly typed version so it’s somewhat puzzling why .NET doesn’t.",{"type":19,"tag":20,"props":116,"children":117},{},[118,120,125],{"type":24,"value":119},"Here is my attempt at a ",{"type":19,"tag":27,"props":121,"children":123},{"className":122},[],[124],{"type":24,"value":112},{"type":24,"value":126}," for .NET. It works fine in the scenarios I’ve used and performance is close to the non-generic version but a quick glance at the SSCLI code for WeakReference reveals that the standard version is a little more complicated and it’s possible there is a reason for that, most likely due to finalizer issues.",{"type":19,"tag":128,"props":129,"children":134},"pre",{"className":130,"code":131,"language":132,"meta":133,"style":133},"language-csharp shiki shiki-themes everforest-light dracula","using System;\nusing System.Runtime.InteropServices;\n\npublic class WeakReference\u003CT> : IDisposable {\n  private GCHandle handle;\n  private bool trackResurrection;\n\n  public WeakReference(T target)\n    : this(target, false) {\n  }\n\n  public WeakReference(T target, bool trackResurrection) {\n    this.trackResurrection = trackResurrection;\n    this.Target = target;\n  }\n\n  ~WeakReference() {\n    Dispose();\n  }\n\n  public void Dispose() {\n    handle.Free();\n    GC.SuppressFinalize(this);\n  }\n\n  public virtual bool IsAlive {\n    get { return (handle.Target != null); }\n  }\n\n  public virtual bool TrackResurrection {\n    get { return this.trackResurrection; }\n  }\n\n  public virtual T Target {\n    get {\n      object o = handle.Target;\n      if ((o == null) || (!(o is T)))\n        return default(T);\n      else\n        return (T)o;\n    }\n    set {\n      handle = GCHandle.Alloc(value,\n        this.trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak);\n    }\n  }\n}\n","csharp","",[135],{"type":19,"tag":27,"props":136,"children":137},{"__ignoreMap":133},[138,162,196,206,253,272,291,299,333,364,373,381,424,452,478,486,494,512,526,534,542,564,582,609,617,625,647,690,698,706,727,761,769,777,799,811,843,905,931,940,961,970,983,1011,1061,1069,1077],{"type":19,"tag":139,"props":140,"children":143},"span",{"class":141,"line":142},"line",1,[144,150,156],{"type":19,"tag":139,"props":145,"children":147},{"style":146},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[148],{"type":24,"value":149},"using",{"type":19,"tag":139,"props":151,"children":153},{"style":152},"--shiki-default:#DF69BA;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[154],{"type":24,"value":155}," System",{"type":19,"tag":139,"props":157,"children":159},{"style":158},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[160],{"type":24,"value":161},";\n",{"type":19,"tag":139,"props":163,"children":165},{"class":141,"line":164},2,[166,170,174,178,183,187,192],{"type":19,"tag":139,"props":167,"children":168},{"style":146},[169],{"type":24,"value":149},{"type":19,"tag":139,"props":171,"children":172},{"style":152},[173],{"type":24,"value":155},{"type":19,"tag":139,"props":175,"children":176},{"style":158},[177],{"type":24,"value":47},{"type":19,"tag":139,"props":179,"children":180},{"style":152},[181],{"type":24,"value":182},"Runtime",{"type":19,"tag":139,"props":184,"children":185},{"style":158},[186],{"type":24,"value":47},{"type":19,"tag":139,"props":188,"children":189},{"style":152},[190],{"type":24,"value":191},"InteropServices",{"type":19,"tag":139,"props":193,"children":194},{"style":158},[195],{"type":24,"value":161},{"type":19,"tag":139,"props":197,"children":199},{"class":141,"line":198},3,[200],{"type":19,"tag":139,"props":201,"children":203},{"emptyLinePlaceholder":202},true,[204],{"type":24,"value":205},"\n",{"type":19,"tag":139,"props":207,"children":209},{"class":141,"line":208},4,[210,216,220,226,231,237,242,248],{"type":19,"tag":139,"props":211,"children":213},{"style":212},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[214],{"type":24,"value":215},"public",{"type":19,"tag":139,"props":217,"children":218},{"style":146},[219],{"type":24,"value":34},{"type":19,"tag":139,"props":221,"children":223},{"style":222},"--shiki-default:#3A94C5;--shiki-dark:#8BE9FD",[224],{"type":24,"value":225}," WeakReference",{"type":19,"tag":139,"props":227,"children":228},{"style":158},[229],{"type":24,"value":230},"\u003C",{"type":19,"tag":139,"props":232,"children":234},{"style":233},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[235],{"type":24,"value":236},"T",{"type":19,"tag":139,"props":238,"children":239},{"style":158},[240],{"type":24,"value":241},"> : ",{"type":19,"tag":139,"props":243,"children":245},{"style":244},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[246],{"type":24,"value":247},"IDisposable",{"type":19,"tag":139,"props":249,"children":250},{"style":158},[251],{"type":24,"value":252}," {\n",{"type":19,"tag":139,"props":254,"children":256},{"class":141,"line":255},5,[257,262,267],{"type":19,"tag":139,"props":258,"children":259},{"style":212},[260],{"type":24,"value":261},"  private",{"type":19,"tag":139,"props":263,"children":264},{"style":244},[265],{"type":24,"value":266}," GCHandle",{"type":19,"tag":139,"props":268,"children":269},{"style":158},[270],{"type":24,"value":271}," handle;\n",{"type":19,"tag":139,"props":273,"children":275},{"class":141,"line":274},6,[276,280,286],{"type":19,"tag":139,"props":277,"children":278},{"style":212},[279],{"type":24,"value":261},{"type":19,"tag":139,"props":281,"children":283},{"style":282},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[284],{"type":24,"value":285}," bool",{"type":19,"tag":139,"props":287,"children":288},{"style":158},[289],{"type":24,"value":290}," trackResurrection;\n",{"type":19,"tag":139,"props":292,"children":294},{"class":141,"line":293},7,[295],{"type":19,"tag":139,"props":296,"children":297},{"emptyLinePlaceholder":202},[298],{"type":24,"value":205},{"type":19,"tag":139,"props":300,"children":302},{"class":141,"line":301},8,[303,308,313,318,322,328],{"type":19,"tag":139,"props":304,"children":305},{"style":212},[306],{"type":24,"value":307},"  public",{"type":19,"tag":139,"props":309,"children":311},{"style":310},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[312],{"type":24,"value":225},{"type":19,"tag":139,"props":314,"children":315},{"style":158},[316],{"type":24,"value":317},"(",{"type":19,"tag":139,"props":319,"children":320},{"style":244},[321],{"type":24,"value":236},{"type":19,"tag":139,"props":323,"children":325},{"style":324},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[326],{"type":24,"value":327}," target",{"type":19,"tag":139,"props":329,"children":330},{"style":158},[331],{"type":24,"value":332},")\n",{"type":19,"tag":139,"props":334,"children":336},{"class":141,"line":335},9,[337,342,348,353,359],{"type":19,"tag":139,"props":338,"children":339},{"style":158},[340],{"type":24,"value":341},"    : ",{"type":19,"tag":139,"props":343,"children":345},{"style":344},"--shiki-default:#DF69BA;--shiki-default-font-style:inherit;--shiki-dark:#BD93F9;--shiki-dark-font-style:italic",[346],{"type":24,"value":347},"this",{"type":19,"tag":139,"props":349,"children":350},{"style":158},[351],{"type":24,"value":352},"(target, ",{"type":19,"tag":139,"props":354,"children":356},{"style":355},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[357],{"type":24,"value":358},"false",{"type":19,"tag":139,"props":360,"children":361},{"style":158},[362],{"type":24,"value":363},") {\n",{"type":19,"tag":139,"props":365,"children":367},{"class":141,"line":366},10,[368],{"type":19,"tag":139,"props":369,"children":370},{"style":158},[371],{"type":24,"value":372},"  }\n",{"type":19,"tag":139,"props":374,"children":376},{"class":141,"line":375},11,[377],{"type":19,"tag":139,"props":378,"children":379},{"emptyLinePlaceholder":202},[380],{"type":24,"value":205},{"type":19,"tag":139,"props":382,"children":384},{"class":141,"line":383},12,[385,389,393,397,401,405,410,415,420],{"type":19,"tag":139,"props":386,"children":387},{"style":212},[388],{"type":24,"value":307},{"type":19,"tag":139,"props":390,"children":391},{"style":310},[392],{"type":24,"value":225},{"type":19,"tag":139,"props":394,"children":395},{"style":158},[396],{"type":24,"value":317},{"type":19,"tag":139,"props":398,"children":399},{"style":244},[400],{"type":24,"value":236},{"type":19,"tag":139,"props":402,"children":403},{"style":324},[404],{"type":24,"value":327},{"type":19,"tag":139,"props":406,"children":407},{"style":158},[408],{"type":24,"value":409},", ",{"type":19,"tag":139,"props":411,"children":412},{"style":282},[413],{"type":24,"value":414},"bool",{"type":19,"tag":139,"props":416,"children":417},{"style":324},[418],{"type":24,"value":419}," trackResurrection",{"type":19,"tag":139,"props":421,"children":422},{"style":158},[423],{"type":24,"value":363},{"type":19,"tag":139,"props":425,"children":427},{"class":141,"line":426},13,[428,433,437,443,448],{"type":19,"tag":139,"props":429,"children":430},{"style":344},[431],{"type":24,"value":432},"    this",{"type":19,"tag":139,"props":434,"children":435},{"style":158},[436],{"type":24,"value":47},{"type":19,"tag":139,"props":438,"children":440},{"style":439},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[441],{"type":24,"value":442},"trackResurrection",{"type":19,"tag":139,"props":444,"children":445},{"style":212},[446],{"type":24,"value":447}," =",{"type":19,"tag":139,"props":449,"children":450},{"style":158},[451],{"type":24,"value":290},{"type":19,"tag":139,"props":453,"children":455},{"class":141,"line":454},14,[456,460,464,469,473],{"type":19,"tag":139,"props":457,"children":458},{"style":344},[459],{"type":24,"value":432},{"type":19,"tag":139,"props":461,"children":462},{"style":158},[463],{"type":24,"value":47},{"type":19,"tag":139,"props":465,"children":466},{"style":439},[467],{"type":24,"value":468},"Target",{"type":19,"tag":139,"props":470,"children":471},{"style":212},[472],{"type":24,"value":447},{"type":19,"tag":139,"props":474,"children":475},{"style":158},[476],{"type":24,"value":477}," target;\n",{"type":19,"tag":139,"props":479,"children":481},{"class":141,"line":480},15,[482],{"type":19,"tag":139,"props":483,"children":484},{"style":158},[485],{"type":24,"value":372},{"type":19,"tag":139,"props":487,"children":489},{"class":141,"line":488},16,[490],{"type":19,"tag":139,"props":491,"children":492},{"emptyLinePlaceholder":202},[493],{"type":24,"value":205},{"type":19,"tag":139,"props":495,"children":497},{"class":141,"line":496},17,[498,503,507],{"type":19,"tag":139,"props":499,"children":500},{"style":158},[501],{"type":24,"value":502},"  ~",{"type":19,"tag":139,"props":504,"children":505},{"style":310},[506],{"type":24,"value":99},{"type":19,"tag":139,"props":508,"children":509},{"style":158},[510],{"type":24,"value":511},"() {\n",{"type":19,"tag":139,"props":513,"children":515},{"class":141,"line":514},18,[516,521],{"type":19,"tag":139,"props":517,"children":518},{"style":310},[519],{"type":24,"value":520},"    Dispose",{"type":19,"tag":139,"props":522,"children":523},{"style":158},[524],{"type":24,"value":525},"();\n",{"type":19,"tag":139,"props":527,"children":529},{"class":141,"line":528},19,[530],{"type":19,"tag":139,"props":531,"children":532},{"style":158},[533],{"type":24,"value":372},{"type":19,"tag":139,"props":535,"children":537},{"class":141,"line":536},20,[538],{"type":19,"tag":139,"props":539,"children":540},{"emptyLinePlaceholder":202},[541],{"type":24,"value":205},{"type":19,"tag":139,"props":543,"children":545},{"class":141,"line":544},21,[546,550,555,560],{"type":19,"tag":139,"props":547,"children":548},{"style":212},[549],{"type":24,"value":307},{"type":19,"tag":139,"props":551,"children":552},{"style":282},[553],{"type":24,"value":554}," void",{"type":19,"tag":139,"props":556,"children":557},{"style":310},[558],{"type":24,"value":559}," Dispose",{"type":19,"tag":139,"props":561,"children":562},{"style":158},[563],{"type":24,"value":511},{"type":19,"tag":139,"props":565,"children":567},{"class":141,"line":566},22,[568,573,578],{"type":19,"tag":139,"props":569,"children":570},{"style":158},[571],{"type":24,"value":572},"    handle.",{"type":19,"tag":139,"props":574,"children":575},{"style":310},[576],{"type":24,"value":577},"Free",{"type":19,"tag":139,"props":579,"children":580},{"style":158},[581],{"type":24,"value":525},{"type":19,"tag":139,"props":583,"children":585},{"class":141,"line":584},23,[586,591,596,600,604],{"type":19,"tag":139,"props":587,"children":588},{"style":158},[589],{"type":24,"value":590},"    GC.",{"type":19,"tag":139,"props":592,"children":593},{"style":310},[594],{"type":24,"value":595},"SuppressFinalize",{"type":19,"tag":139,"props":597,"children":598},{"style":158},[599],{"type":24,"value":317},{"type":19,"tag":139,"props":601,"children":602},{"style":344},[603],{"type":24,"value":347},{"type":19,"tag":139,"props":605,"children":606},{"style":158},[607],{"type":24,"value":608},");\n",{"type":19,"tag":139,"props":610,"children":612},{"class":141,"line":611},24,[613],{"type":19,"tag":139,"props":614,"children":615},{"style":158},[616],{"type":24,"value":372},{"type":19,"tag":139,"props":618,"children":620},{"class":141,"line":619},25,[621],{"type":19,"tag":139,"props":622,"children":623},{"emptyLinePlaceholder":202},[624],{"type":24,"value":205},{"type":19,"tag":139,"props":626,"children":628},{"class":141,"line":627},26,[629,633,638,642],{"type":19,"tag":139,"props":630,"children":631},{"style":212},[632],{"type":24,"value":307},{"type":19,"tag":139,"props":634,"children":635},{"style":212},[636],{"type":24,"value":637}," virtual",{"type":19,"tag":139,"props":639,"children":640},{"style":282},[641],{"type":24,"value":285},{"type":19,"tag":139,"props":643,"children":644},{"style":158},[645],{"type":24,"value":646}," IsAlive {\n",{"type":19,"tag":139,"props":648,"children":650},{"class":141,"line":649},27,[651,656,661,666,671,675,680,685],{"type":19,"tag":139,"props":652,"children":653},{"style":282},[654],{"type":24,"value":655},"    get",{"type":19,"tag":139,"props":657,"children":658},{"style":158},[659],{"type":24,"value":660}," { ",{"type":19,"tag":139,"props":662,"children":663},{"style":146},[664],{"type":24,"value":665},"return",{"type":19,"tag":139,"props":667,"children":668},{"style":158},[669],{"type":24,"value":670}," (handle.",{"type":19,"tag":139,"props":672,"children":673},{"style":439},[674],{"type":24,"value":468},{"type":19,"tag":139,"props":676,"children":677},{"style":212},[678],{"type":24,"value":679}," !=",{"type":19,"tag":139,"props":681,"children":682},{"style":355},[683],{"type":24,"value":684}," null",{"type":19,"tag":139,"props":686,"children":687},{"style":158},[688],{"type":24,"value":689},"); }\n",{"type":19,"tag":139,"props":691,"children":693},{"class":141,"line":692},28,[694],{"type":19,"tag":139,"props":695,"children":696},{"style":158},[697],{"type":24,"value":372},{"type":19,"tag":139,"props":699,"children":701},{"class":141,"line":700},29,[702],{"type":19,"tag":139,"props":703,"children":704},{"emptyLinePlaceholder":202},[705],{"type":24,"value":205},{"type":19,"tag":139,"props":707,"children":709},{"class":141,"line":708},30,[710,714,718,722],{"type":19,"tag":139,"props":711,"children":712},{"style":212},[713],{"type":24,"value":307},{"type":19,"tag":139,"props":715,"children":716},{"style":212},[717],{"type":24,"value":637},{"type":19,"tag":139,"props":719,"children":720},{"style":282},[721],{"type":24,"value":285},{"type":19,"tag":139,"props":723,"children":724},{"style":158},[725],{"type":24,"value":726}," TrackResurrection {\n",{"type":19,"tag":139,"props":728,"children":730},{"class":141,"line":729},31,[731,735,739,743,748,752,756],{"type":19,"tag":139,"props":732,"children":733},{"style":282},[734],{"type":24,"value":655},{"type":19,"tag":139,"props":736,"children":737},{"style":158},[738],{"type":24,"value":660},{"type":19,"tag":139,"props":740,"children":741},{"style":146},[742],{"type":24,"value":665},{"type":19,"tag":139,"props":744,"children":745},{"style":344},[746],{"type":24,"value":747}," this",{"type":19,"tag":139,"props":749,"children":750},{"style":158},[751],{"type":24,"value":47},{"type":19,"tag":139,"props":753,"children":754},{"style":439},[755],{"type":24,"value":442},{"type":19,"tag":139,"props":757,"children":758},{"style":158},[759],{"type":24,"value":760},"; }\n",{"type":19,"tag":139,"props":762,"children":764},{"class":141,"line":763},32,[765],{"type":19,"tag":139,"props":766,"children":767},{"style":158},[768],{"type":24,"value":372},{"type":19,"tag":139,"props":770,"children":772},{"class":141,"line":771},33,[773],{"type":19,"tag":139,"props":774,"children":775},{"emptyLinePlaceholder":202},[776],{"type":24,"value":205},{"type":19,"tag":139,"props":778,"children":780},{"class":141,"line":779},34,[781,785,789,794],{"type":19,"tag":139,"props":782,"children":783},{"style":212},[784],{"type":24,"value":307},{"type":19,"tag":139,"props":786,"children":787},{"style":212},[788],{"type":24,"value":637},{"type":19,"tag":139,"props":790,"children":791},{"style":244},[792],{"type":24,"value":793}," T",{"type":19,"tag":139,"props":795,"children":796},{"style":158},[797],{"type":24,"value":798}," Target {\n",{"type":19,"tag":139,"props":800,"children":802},{"class":141,"line":801},35,[803,807],{"type":19,"tag":139,"props":804,"children":805},{"style":282},[806],{"type":24,"value":655},{"type":19,"tag":139,"props":808,"children":809},{"style":158},[810],{"type":24,"value":252},{"type":19,"tag":139,"props":812,"children":814},{"class":141,"line":813},36,[815,820,825,830,835,839],{"type":19,"tag":139,"props":816,"children":817},{"style":282},[818],{"type":24,"value":819},"      object",{"type":19,"tag":139,"props":821,"children":822},{"style":158},[823],{"type":24,"value":824}," o ",{"type":19,"tag":139,"props":826,"children":827},{"style":212},[828],{"type":24,"value":829},"=",{"type":19,"tag":139,"props":831,"children":832},{"style":158},[833],{"type":24,"value":834}," handle.",{"type":19,"tag":139,"props":836,"children":837},{"style":439},[838],{"type":24,"value":468},{"type":19,"tag":139,"props":840,"children":841},{"style":158},[842],{"type":24,"value":161},{"type":19,"tag":139,"props":844,"children":846},{"class":141,"line":845},37,[847,852,857,862,866,871,876,881,886,891,896,900],{"type":19,"tag":139,"props":848,"children":849},{"style":146},[850],{"type":24,"value":851},"      if",{"type":19,"tag":139,"props":853,"children":854},{"style":158},[855],{"type":24,"value":856}," ((o ",{"type":19,"tag":139,"props":858,"children":859},{"style":212},[860],{"type":24,"value":861},"==",{"type":19,"tag":139,"props":863,"children":864},{"style":355},[865],{"type":24,"value":684},{"type":19,"tag":139,"props":867,"children":868},{"style":158},[869],{"type":24,"value":870},") ",{"type":19,"tag":139,"props":872,"children":873},{"style":212},[874],{"type":24,"value":875},"||",{"type":19,"tag":139,"props":877,"children":878},{"style":158},[879],{"type":24,"value":880}," (",{"type":19,"tag":139,"props":882,"children":883},{"style":212},[884],{"type":24,"value":885},"!",{"type":19,"tag":139,"props":887,"children":888},{"style":158},[889],{"type":24,"value":890},"(o ",{"type":19,"tag":139,"props":892,"children":893},{"style":146},[894],{"type":24,"value":895},"is",{"type":19,"tag":139,"props":897,"children":898},{"style":244},[899],{"type":24,"value":793},{"type":19,"tag":139,"props":901,"children":902},{"style":158},[903],{"type":24,"value":904},")))\n",{"type":19,"tag":139,"props":906,"children":908},{"class":141,"line":907},38,[909,914,919,923,927],{"type":19,"tag":139,"props":910,"children":911},{"style":146},[912],{"type":24,"value":913},"        return",{"type":19,"tag":139,"props":915,"children":916},{"style":146},[917],{"type":24,"value":918}," default",{"type":19,"tag":139,"props":920,"children":921},{"style":158},[922],{"type":24,"value":317},{"type":19,"tag":139,"props":924,"children":925},{"style":244},[926],{"type":24,"value":236},{"type":19,"tag":139,"props":928,"children":929},{"style":158},[930],{"type":24,"value":608},{"type":19,"tag":139,"props":932,"children":934},{"class":141,"line":933},39,[935],{"type":19,"tag":139,"props":936,"children":937},{"style":146},[938],{"type":24,"value":939},"      else\n",{"type":19,"tag":139,"props":941,"children":943},{"class":141,"line":942},40,[944,948,952,956],{"type":19,"tag":139,"props":945,"children":946},{"style":146},[947],{"type":24,"value":913},{"type":19,"tag":139,"props":949,"children":950},{"style":158},[951],{"type":24,"value":880},{"type":19,"tag":139,"props":953,"children":954},{"style":244},[955],{"type":24,"value":236},{"type":19,"tag":139,"props":957,"children":958},{"style":158},[959],{"type":24,"value":960},")o;\n",{"type":19,"tag":139,"props":962,"children":964},{"class":141,"line":963},41,[965],{"type":19,"tag":139,"props":966,"children":967},{"style":158},[968],{"type":24,"value":969},"    }\n",{"type":19,"tag":139,"props":971,"children":973},{"class":141,"line":972},42,[974,979],{"type":19,"tag":139,"props":975,"children":976},{"style":282},[977],{"type":24,"value":978},"    set",{"type":19,"tag":139,"props":980,"children":981},{"style":158},[982],{"type":24,"value":252},{"type":19,"tag":139,"props":984,"children":986},{"class":141,"line":985},43,[987,992,996,1001,1006],{"type":19,"tag":139,"props":988,"children":989},{"style":158},[990],{"type":24,"value":991},"      handle ",{"type":19,"tag":139,"props":993,"children":994},{"style":212},[995],{"type":24,"value":829},{"type":19,"tag":139,"props":997,"children":998},{"style":158},[999],{"type":24,"value":1000}," GCHandle.",{"type":19,"tag":139,"props":1002,"children":1003},{"style":310},[1004],{"type":24,"value":1005},"Alloc",{"type":19,"tag":139,"props":1007,"children":1008},{"style":158},[1009],{"type":24,"value":1010},"(value,\n",{"type":19,"tag":139,"props":1012,"children":1014},{"class":141,"line":1013},44,[1015,1020,1024,1028,1033,1038,1043,1048,1052,1057],{"type":19,"tag":139,"props":1016,"children":1017},{"style":344},[1018],{"type":24,"value":1019},"        this",{"type":19,"tag":139,"props":1021,"children":1022},{"style":158},[1023],{"type":24,"value":47},{"type":19,"tag":139,"props":1025,"children":1026},{"style":439},[1027],{"type":24,"value":442},{"type":19,"tag":139,"props":1029,"children":1030},{"style":212},[1031],{"type":24,"value":1032}," ?",{"type":19,"tag":139,"props":1034,"children":1035},{"style":158},[1036],{"type":24,"value":1037}," GCHandleType.",{"type":19,"tag":139,"props":1039,"children":1040},{"style":439},[1041],{"type":24,"value":1042},"WeakTrackResurrection",{"type":19,"tag":139,"props":1044,"children":1045},{"style":212},[1046],{"type":24,"value":1047}," :",{"type":19,"tag":139,"props":1049,"children":1050},{"style":158},[1051],{"type":24,"value":1037},{"type":19,"tag":139,"props":1053,"children":1054},{"style":439},[1055],{"type":24,"value":1056},"Weak",{"type":19,"tag":139,"props":1058,"children":1059},{"style":158},[1060],{"type":24,"value":608},{"type":19,"tag":139,"props":1062,"children":1064},{"class":141,"line":1063},45,[1065],{"type":19,"tag":139,"props":1066,"children":1067},{"style":158},[1068],{"type":24,"value":969},{"type":19,"tag":139,"props":1070,"children":1072},{"class":141,"line":1071},46,[1073],{"type":19,"tag":139,"props":1074,"children":1075},{"style":158},[1076],{"type":24,"value":372},{"type":19,"tag":139,"props":1078,"children":1080},{"class":141,"line":1079},47,[1081],{"type":19,"tag":139,"props":1082,"children":1083},{"style":158},[1084],{"type":24,"value":1085},"}\n",{"type":19,"tag":51,"props":1087,"children":1088},{},[1089],{"type":19,"tag":20,"props":1090,"children":1091},{},[1092,1094,1099,1101,1106,1108,1113],{"type":24,"value":1093},"I’ve allowed ",{"type":19,"tag":27,"props":1095,"children":1097},{"className":1096},[],[1098],{"type":24,"value":468},{"type":24,"value":1100}," to be settable against my better judgment to bring it more in like with ",{"type":19,"tag":27,"props":1102,"children":1104},{"className":1103},[],[1105],{"type":24,"value":99},{"type":24,"value":1107},". It’s still not serializable though unlike ",{"type":19,"tag":27,"props":1109,"children":1111},{"className":1110},[],[1112],{"type":24,"value":99},{"type":24,"value":47},{"type":19,"tag":20,"props":1115,"children":1116},{},[1117],{"type":19,"tag":1118,"props":1119,"children":1120},"em",{},[1121],{"type":24,"value":1122},"[)amien",{"type":19,"tag":1124,"props":1125,"children":1126},"style",{},[1127],{"type":24,"value":1128},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":133,"searchDepth":164,"depth":164,"links":1130},[],"markdown","content:blog:2006:implementingweakreferencet.md","content","blog/2006/implementingweakreferencet.md","blog/2006/implementingweakreferencet","md","/blog/2006/implementingweakreferencet/",391,[1140,1144,1148],{"title":1141,"date":1142,"url":1143},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":1145,"date":1146,"url":1147},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":1149,"date":1150,"url":1151},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[1153,1200,1225,1246],{"_path":1154,"_dir":1155,"_draft":6,"_partial":6,"_locale":7,"title":1156,"description":1157,"id":1158,"name":1159,"email":1160,"avatar":1161,"url":1162,"date":1163,"body":1164,"_type":1131,"_id":1197,"_source":1133,"_file":1198,"_stem":1199,"_extension":1136},"/comments/implementingweakreferencet/46263","implementingweakreferencet","46263","This sounds great, however, this duplicates part of the system library...",46263,"Charles","charles.hetier@gmail.com","https://www.gravatar.com/avatar/509761c8ec74c7c7013e1c08abe818bb?r=pg&d=retro","https://www.charly-studio.com","2011-08-01T06:46:58",{"type":16,"children":1165,"toc":1195},[1166,1170,1175,1180,1185,1190],{"type":19,"tag":20,"props":1167,"children":1168},{},[1169],{"type":24,"value":1157},{"type":19,"tag":20,"props":1171,"children":1172},{},[1173],{"type":24,"value":1174},"I think that a simple inheritance could be acceptable in most of the cases -> the new target would hide the one in the base class, this works in c# (cast to original weakreference type allows recovering the base property)",{"type":19,"tag":20,"props":1176,"children":1177},{},[1178],{"type":24,"value":1179},"Target property would look like something like that:\npublic new T Target\n{\nget{...} set{...}\n}",{"type":19,"tag":20,"props":1181,"children":1182},{},[1183],{"type":24,"value":1184},"Otherwise, an adapter pattern could be applied...",{"type":19,"tag":20,"props":1186,"children":1187},{},[1188],{"type":24,"value":1189},"Well, there are at least 3 ways to implement generic weak references. They need to be balanced with the needs I think.",{"type":19,"tag":20,"props":1191,"children":1192},{},[1193],{"type":24,"value":1194},"Anyway, thanks for sharing your solution!",{"title":133,"searchDepth":164,"depth":164,"links":1196},[],"content:comments:implementingweakreferencet:46263.md","comments/implementingweakreferencet/46263.md","comments/implementingweakreferencet/46263",{"_path":1201,"_dir":1155,"_draft":6,"_partial":6,"_locale":7,"title":1202,"description":1203,"id":1204,"name":1205,"email":1206,"avatar":1207,"date":1208,"body":1209,"_type":1131,"_id":1222,"_source":1133,"_file":1223,"_stem":1224,"_extension":1136},"/comments/implementingweakreferencet/10610","10610","I had a similar need and I used inheritance like this:",10610,"Stephen Sabey","stephen.sabey@gov.ab.ca","https://www.gravatar.com/avatar/14ec3629cb83426a18a33fb13464153f?r=pg&d=retro","2008-12-15T12:01:29",{"type":16,"children":1210,"toc":1220},[1211,1215],{"type":19,"tag":20,"props":1212,"children":1213},{},[1214],{"type":24,"value":1203},{"type":19,"tag":20,"props":1216,"children":1217},{},[1218],{"type":24,"value":1219},"Public Class WeakReference(Of T)\nInherits WeakReference\nPublic Shadows Property Target() As T\nGet\nReturn CType(MyBase.Target, T)\nEnd Get\nSet(ByVal value As T)\nMyBase.Target = value\nEnd Set\nEnd Property\nPublic Sub New(ByVal target As T)\nMyBase.New(target)\nEnd Sub\nPublic Sub New(ByVal target As T, ByVal trackResurrection As Boolean)\nMyBase.New(target, trackResurrection)\nEnd Sub\nPublic Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, _\nByVal context As System.Runtime.Serialization.StreamingContext)\nMyBase.New(info, context)\nEnd Sub\nEnd Class",{"title":133,"searchDepth":164,"depth":164,"links":1221},[],"content:comments:implementingweakreferencet:10610.md","comments/implementingweakreferencet/10610.md","comments/implementingweakreferencet/10610",{"_path":1226,"_dir":1155,"_draft":6,"_partial":6,"_locale":7,"title":1227,"description":1228,"id":1229,"name":1230,"email":1231,"avatar":1232,"url":1233,"date":1234,"body":1235,"_type":1131,"_id":1243,"_source":1133,"_file":1244,"_stem":1245,"_extension":1136},"/comments/implementingweakreferencet/1544","1544","You can't change the object type of a property in a subclass.",1544,"Damien Guard","damien@envytech.co.uk","https://www.gravatar.com/avatar/dc72963e7279d34c85ed4c0b731ce5a9?r=pg&d=retro","https://damieng.com/","2006-12-15T05:22:42",{"type":16,"children":1236,"toc":1241},[1237],{"type":19,"tag":20,"props":1238,"children":1239},{},[1240],{"type":24,"value":1228},{"title":133,"searchDepth":164,"depth":164,"links":1242},[],"content:comments:implementingweakreferencet:1544.md","comments/implementingweakreferencet/1544.md","comments/implementingweakreferencet/1544",{"_path":1247,"_dir":1155,"_draft":6,"_partial":6,"_locale":7,"title":1248,"description":1249,"id":1250,"name":1251,"email":1252,"avatar":1253,"url":1254,"date":1255,"body":1256,"_type":1131,"_id":1264,"_source":1133,"_file":1265,"_stem":1266,"_extension":1136},"/comments/implementingweakreferencet/1543","1543","Could you have inherited from WeakReference and simply cast Target as appropriate?",1543,"Marc Brooks","IDisposable@gmail.com","https://www.gravatar.com/avatar/2a6b434b5332e77137537e6de4efc41a?r=pg&d=retro","https://musingmarc.blogspot.com/","2006-12-12T00:16:06",{"type":16,"children":1257,"toc":1262},[1258],{"type":19,"tag":20,"props":1259,"children":1260},{},[1261],{"type":24,"value":1249},{"title":133,"searchDepth":164,"depth":164,"links":1263},[],"content:comments:implementingweakreferencet:1543.md","comments/implementingweakreferencet/1543.md","comments/implementingweakreferencet/1543",1779264596647]