Merge pull request #3389 from lambdageek/bug-43099
[mono.git] / mcs / class / referencesource / System.Web / Cache / SRef.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using System.Linq;
5 using System.Reflection;
6 using System.Security.Permissions;
7 using System.Web;
8
9 namespace System.Web.Caching {
10     internal class SRef {
11         private static Type s_type = Type.GetType("System.SizedReference", true, false);
12         private Object _sizedRef;
13         private long _lastReportedSize; // This helps tremendously when looking at large dumps
14         
15         internal SRef(Object target) {
16             _sizedRef = HttpRuntime.CreateNonPublicInstance(s_type, new object[] {target});
17         }
18         
19         internal long ApproximateSize {
20             [PermissionSet(SecurityAction.Assert, Unrestricted=true)]
21             get {
22                 object o = s_type.InvokeMember("ApproximateSize",
23                                                BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, 
24                                                null, // binder
25                                                _sizedRef, // target
26                                                null, // args
27                                                CultureInfo.InvariantCulture);
28                 return _lastReportedSize = (long) o;
29             }
30         }
31         
32         [PermissionSet(SecurityAction.Assert, Unrestricted=true)]
33         internal void Dispose() {
34             s_type.InvokeMember("Dispose",
35                                 BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, 
36                                 null, // binder
37                                 _sizedRef, // target
38                                 null, // args
39                                 CultureInfo.InvariantCulture);
40         }
41     }
42
43     internal class SRefMultiple {
44         private List<SRef> _srefs = new List<SRef>();
45
46         internal void AddSRefTarget(Object o) {
47             _srefs.Add(new SRef(o));
48         }
49
50         internal long ApproximateSize {
51             [PermissionSet(SecurityAction.Assert, Unrestricted = true)]
52             get {
53                 return _srefs.Sum(s => s.ApproximateSize);
54             }
55         }
56
57         [PermissionSet(SecurityAction.Assert, Unrestricted = true)]
58         internal void Dispose() {
59             foreach (SRef s in _srefs) {
60                 s.Dispose();
61             }
62         }
63     }
64 }