Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / DescriptorUtil.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Linq;\r
16     using System.Threading;\r
17 \r
18     internal static class DescriptorUtil {\r
19 \r
20         public static TDescriptor[] LazilyFetchOrCreateDescriptors<TReflection, TDescriptor>(ref TDescriptor[] cacheLocation, Func<TReflection[]> initializer, Func<TReflection, TDescriptor> converter) {\r
21             // did we already calculate this once?\r
22             TDescriptor[] existingCache = Interlocked.CompareExchange(ref cacheLocation, null, null);\r
23             if (existingCache != null) {\r
24                 return existingCache;\r
25             }\r
26 \r
27             TReflection[] memberInfos = initializer();\r
28             TDescriptor[] descriptors = memberInfos.Select(converter).Where(descriptor => descriptor != null).ToArray();\r
29             TDescriptor[] updatedCache = Interlocked.CompareExchange(ref cacheLocation, descriptors, null);\r
30             return updatedCache ?? descriptors;\r
31         }\r
32 \r
33     }\r
34 }\r