New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ViewDataDictionary`1.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 \r
16     public class ViewDataDictionary<TModel> : ViewDataDictionary {\r
17         public ViewDataDictionary() :\r
18             base(default(TModel)) {\r
19         }\r
20 \r
21         public ViewDataDictionary(TModel model) :\r
22             base(model) {\r
23         }\r
24 \r
25         public ViewDataDictionary(ViewDataDictionary viewDataDictionary) :\r
26             base(viewDataDictionary) {\r
27         }\r
28 \r
29         public new TModel Model {\r
30             get {\r
31                 return (TModel)base.Model;\r
32             }\r
33             set {\r
34                 SetModel(value);\r
35             }\r
36         }\r
37 \r
38         public override ModelMetadata ModelMetadata {\r
39             get {\r
40                 ModelMetadata result = base.ModelMetadata;\r
41                 if (result == null) {\r
42                     result = base.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel));\r
43                 }\r
44                 return result;\r
45             }\r
46             set {\r
47                 base.ModelMetadata = value;\r
48             }\r
49         }\r
50 \r
51         protected override void SetModel(object value) {\r
52             bool castWillSucceed = TypeHelpers.IsCompatibleObject<TModel>(value);\r
53 \r
54             if (castWillSucceed) {\r
55                 base.SetModel((TModel)value);\r
56             }\r
57             else {\r
58                 InvalidOperationException exception = (value != null)\r
59                     ? Error.ViewDataDictionary_WrongTModelType(value.GetType(), typeof(TModel))\r
60                     : Error.ViewDataDictionary_ModelCannotBeNull(typeof(TModel));\r
61                 throw exception;\r
62             }\r
63         }\r
64 \r
65     }\r
66 }\r