Skip to main content

Posts

Generic overloads and ref parameter hell (the return of the Stack overflow)

In a framework that I have written for my dev team, I have a class that has a method like so: public class ObjectBase {      public void Clone( ref ObjectBase target, ObjectBase parent); } This method (virtual in my library, in fact) has two functions:  When the target ref is null, then a new object should be created, but when it's not, then the contents of the object on which it's being called should be cloned into the passed object.  The parent of the new object is required because in the framework our objects have keys that can come from multiple sources - including an expression on the parent. Anyway, whilst this method is particularly useful, it's a little unfriendly to use from your code when you don't have a direct ObjectBase reference to pass, but instead a reference to an object derived from it: public class MyObject : ObjectBase { }     public static void main() {      MyObject obj = new MyObject ();      MyObject ...

Exception when using WCF Client to Web Service Reference behind a proxy

Are you getting the following exception when calling a simple ASMX service via a WCF service reference from behind a firewall (in this case ISA Server):  "Server encountered an internal error. For more information, turn off customErrors in the server's .config file." You'd expect there to be an inner exception, but there isn't.  You can get to the actual exception by enabling all exceptions to cause a breakpoint in VS2005/2008 (Menu: Debug->Exceptions and then click the checkbox at the root of all .Net exceptions - be careful doing this in a web site, though, because a tonne of exceptions get thrown and swallowed by the Asp.Net engine!). The actual error that is occuring is a WebException: [407] Proxy Authentication Required. In my case, I've also been using Fiddler to debug the SOAP that's going to and from (if you haven't got it, then get it now!) my machine, and had also noticed that when it was running, the exception would go away.  This is...

More C# generic extension methods - non-interface interface methods

(This blog post is copied from my old blog at www.lordzoltan.org, it was originally written 1st August 2008) Somebody asked me the other day if there was a generic way of providing a property-based clone operation for classes.  The operation didn't have to recurse into complex objects (just copy the references), but the value types had to be copied. The first part of the solution was a little bit of reflection: public static void Clone( object source, object dest) {      //(omitted) check that the source and destination types      //are equal with Type.IsAssignableFrom        PropertyInfo [] props = source . GetType() . GetProperties(          BindingFlags . Public |          BindingFlags . Instance |          BindingFlags . FlattenHierarchy);      MethodInfo getmethod;      MethodInfo setmethod;      foreach ( var p in props)      {          //make sure the get and set accessors are available.          getmethod = p . GetGetMethod();       ...

Pseudo-Template Meta-Programming in C# - Part 2

Following on from a taster that I posted before: http://lordzoltan.blogspot.com/2010/09/pseudo-template-meta-programming-in-c.html , here goes with the next piece. Firstly, the design I'm showing here can be applied to any reflection scenario - I've deployed it in various guises, including for storing meta-data about methods in a class, properties, and even for compiling dynamic methods that provide late-bound functions.  The primary reason why this design, I feel, is particularly useful is that you're making the .Net type engine do all the hard work of storing all this meta-data for you. I'll go through this by looking at a really simple scenario: You want to write a system whereby you can reflect properties of a type that a developer writes in order to provide some form of functionality.  Let us say that you want to write a dictionary state-bag wrapper, where a property is wrapped around an internal dictionary of values.  What you want is to have a code-generation ...