Fixing equality tests for nullable parameters.
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / IgnoreNull.cs
blob4f0194637b9f0f3cc60e99e141c21010d145c4bc
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Views.Brail
17 using System;
18 using System.Collections.Generic;
19 using Boo.Lang;
21 public class IgnoreNull : IQuackFu
23 private readonly object target;
25 public IgnoreNull(object target)
27 this.target = target;
30 public object QuackGet(string name, object[] parameters)
32 if (target == null)
33 return this;
34 object value;
35 if (IsNullOrEmpty(parameters))
36 value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetProperty(target, name);
37 else
38 value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetSlice(target, name, parameters);
39 return new IgnoreNull(value);
42 private static bool IsNullOrEmpty(object[] parameters)
44 return parameters == null || parameters.Length == 0;
47 public object QuackSet(string name, object[] parameters, object obj)
49 if (target == null)
50 return this;
51 if (IsNullOrEmpty(parameters))
52 ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetProperty(target, name, obj);
53 else
54 ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetSlice(target, name, GetParameterArray(parameters, obj));
55 return this;
58 private static object[] GetParameterArray(object[] parameters, object obj)
60 List<object> args = new List<object>(parameters);
61 args.Add(obj);
62 return args.ToArray();
65 public object QuackInvoke(string name, object[] args)
67 if (target == null)
68 return this;
69 object value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.Invoke(target, name, args);
70 return new IgnoreNull(value);
73 public override string ToString()
75 if(target == null)
76 return string.Empty;
77 return target.ToString();
80 public static bool AreEqual(object left, object right)
82 IgnoreNull temp = left as IgnoreNull;
83 if (temp != null)
84 left = temp.target;
85 temp = right as IgnoreNull;
86 if (temp != null)
87 right = temp.target;
88 return Equals(left, right);