Fixing a bug with the new FixTryGetParameterConditionalChecks - which would always...
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / IgnoreNull.cs
blob5fbd55e522db696f826a99f2c6415163261554a3
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 (name == "_IsIgnoreNullReferencingNotNullObject_")
33 return target != null;
35 if (target == null)
36 return this;
37 object value;
38 if (IsNullOrEmpty(parameters))
39 value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetProperty(target, name);
40 else
41 value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetSlice(target, name, parameters);
42 return new IgnoreNull(value);
45 private static bool IsNullOrEmpty(object[] parameters)
47 return parameters == null || parameters.Length == 0;
50 public object QuackSet(string name, object[] parameters, object obj)
52 if (target == null)
53 return this;
54 if (IsNullOrEmpty(parameters))
55 ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetProperty(target, name, obj);
56 else
57 ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetSlice(target, name, GetParameterArray(parameters, obj));
58 return this;
61 private static object[] GetParameterArray(object[] parameters, object obj)
63 List<object> args = new List<object>(parameters);
64 args.Add(obj);
65 return args.ToArray();
68 public object QuackInvoke(string name, object[] args)
70 if (target == null)
71 return this;
72 object value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.Invoke(target, name, args);
73 return new IgnoreNull(value);
76 public override string ToString()
78 if(target == null)
79 return string.Empty;
80 return target.ToString();
83 public static bool AreEqual(object left, object right)
85 IgnoreNull temp = left as IgnoreNull;
86 if (temp != null)
87 left = temp.target;
88 temp = right as IgnoreNull;
89 if (temp != null)
90 right = temp.target;
91 return Equals(left, right);