Archive for September, 2009
I have done what I almost never do. I have made an extension method. I don’t like extensions, I believe they increase code complexity and decrease code portability.
Why allow static methods to disguise themselves as instance methods? Well, on the other hand Linq is awesome.
Another move from Hejlsberg & co. that I’m no fan of, is that it is left to the creator of a class to decide if Equals() should mean that two objects are the same (ReferenceEquals()) or simply equal. And when I want the ‘equal’ functionality, I have to implement it.
So I created this:
An extension method that recursively runs through all public fields and properties and check for value equality at node level. It does not support indexed properties, at least not yet and please notice that this uses the MoreLinq extensions.
258 public static bool ValueEquals(this object o, object o2)
260 Type oType = o.GetType();
261 if (!oType.Equals(o2.GetType())) return false;
263 if (o == o2) return true;
265 if (o is ValueType || o is string)
267 return (o.Equals(o2));
269 else if(o is Array)
271 return (o as Array).Cast<object>().ZipLongest((o as Array).Cast<object>(), (i1, i2) => i1.ValueEquals(i2)).All(b => b);
275 return oType.GetFields().All(f =>
277 object v = f.GetValue(o);
278 if (v == null)
280 return f.GetValue(o2) == null;
282 else if (v is ValueType || v is string)
284 return v.Equals(f.GetValue(o2));
288 return v.ValueEquals(f.GetValue(o2));
290 }) &&
291 oType.GetProperties().All(p =>
293 object v = null;
296 v = p.GetValue(o, null);
298 catch (TargetParameterCountException)
300 throw new Exception("Indexed properties are not supported yet");
302 if (v == null)
304 return p.GetValue(o2, null) == null;
306 else if (v is ValueType || v is string)
308 return v.Equals(p.GetValue(o2, null));
312 return v.ValueEquals(p.GetValue(o2, null));
Let me know how it works for you..
I have been running into some BadImageFormatException’s lately and I just discovered the reason today. I found out that I needed to change my targeted platform to the same platform as the assembly was build for, that’s it. Simply right-click the project in Visual Studio -> Properties, Build -> Platform target: [platform of assembly]
It solved the same issue I had with LINQPad, I rebuild my assembly and now it could be loaded. I run Windows 7 x64 so I rebuild my assembly to x64.