1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
23 public class IgnoreNull
: IQuackFu
25 private readonly object target
;
27 public IgnoreNull(object target
)
32 #region IQuackFu Members
34 public object QuackGet(string name
, object[] parameters
)
36 if (name
== "_IsIgnoreNullReferencingNotNullObject_")
38 return target
!= null;
46 if (IsNullOrEmpty(parameters
))
48 value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods
.GetProperty(target
, name
);
52 value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods
.GetSlice(target
, name
, parameters
);
54 return new IgnoreNull(value);
57 public object QuackSet(string name
, object[] parameters
, object obj
)
63 if (IsNullOrEmpty(parameters
))
65 ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods
.SetProperty(target
, name
, obj
);
69 ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods
.SetSlice(target
, name
,
70 GetParameterArray(parameters
, obj
));
75 public object QuackInvoke(string name
, object[] args
)
81 object value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods
.Invoke(target
, name
, args
);
82 return new IgnoreNull(value);
87 private static bool IsNullOrEmpty(object[] parameters
)
89 return parameters
== null || parameters
.Length
== 0;
92 private static object[] GetParameterArray(object[] parameters
, object obj
)
94 List
<object> args
= new List
<object>(parameters
);
96 return args
.ToArray();
99 public override string ToString()
105 return target
.ToString();
108 public static bool AreEqual(object left
, object right
)
110 IgnoreNull temp
= left
as IgnoreNull
;
115 temp
= right
as IgnoreNull
;
120 return Equals(left
, right
);
124 /// Gets the underlying target object of the IgnorNull
126 /// <param name="ignored"></param>
127 /// <returns>target</returns>
128 public static Object
ExtractTarget(IgnoreNull ignored
)
130 return ignored
.target
;
134 /// Returns a new array copied from args that has replaced IgnoreNull instances
135 /// with the underlying target object.
137 /// <param name="args">source array</param>
138 /// <returns>new array</returns>
139 public static Object
[] ReplaceIgnoreNullsWithTargets(Object
[] args
)
141 Object
[] replaced
= new object[args
.Length
];
142 for (int i
= 0; i
< args
.Length
; i
++)
144 if (args
[i
] is IgnoreNull
)
146 replaced
[i
] = ExtractTarget((IgnoreNull
) args
[i
]);
150 replaced
[i
] = args
[i
];
157 /// Returns a new Hashtable that has copied the entries from dict
158 /// and replaced values of IgnoreNull with the underlying target object.
160 /// <param name="dict"></param>
161 /// <returns>Hashtable</returns>
162 public static IDictionary
ReplaceIgnoreNullsWithTargets(IDictionary dict
)
164 Hashtable hash
= new Hashtable(dict
.Count
);
166 foreach (DictionaryEntry de
in dict
)
168 if (de
.Value
is IgnoreNull
)
170 hash
[de
.Key
] = ExtractTarget((IgnoreNull
) de
.Value
);
174 hash
[de
.Key
] = de
.Value
;