Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.ComponentModel.Composition / src / ComponentModel / System / ComponentModel / Composition / MetadataServices.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.Collections.Generic;\r
6 using Microsoft.Internal;\r
7 using Microsoft.Internal.Collections;\r
8 \r
9 namespace System.ComponentModel.Composition\r
10 {\r
11     internal static class MetadataServices\r
12     {\r
13         public static readonly IDictionary<string, object> EmptyMetadata = new ReadOnlyDictionary<string, object>(null);\r
14 \r
15         public static IDictionary<string, object> AsReadOnly(this IDictionary<string, object> metadata)\r
16         {\r
17             if (metadata == null)\r
18             {\r
19                 return EmptyMetadata;\r
20             }\r
21 \r
22             if (metadata is ReadOnlyDictionary<string, object>)\r
23             {\r
24                 return metadata;\r
25             }\r
26 \r
27             return new ReadOnlyDictionary<string, object>(metadata);\r
28         }\r
29 \r
30         public static T GetValue<T>(this IDictionary<string, object> metadata, string key)\r
31         {\r
32             Assumes.NotNull(metadata, "metadata");\r
33 \r
34             object untypedValue = true;\r
35             if (!metadata.TryGetValue(key, out untypedValue))\r
36             {\r
37                 return default(T);\r
38             }\r
39 \r
40             if (untypedValue is T)\r
41             {\r
42                 return (T)untypedValue;\r
43             }\r
44             else\r
45             {\r
46                 return default(T);\r
47             }\r
48         }\r
49     }\r
50 }\r