AliGrant.com
 
Valid XHTML 1.0!

Valid CSS!

Powered by PHP

Get FireFox

1&1 Internet


Archived News

Late static binding in C# - virtual staticTuesday 24th May 2016
In PHP there is a really handy concept called late static binding. You can run a piece of code such as static::MyMethod() and it will execute the method "MyMethod" as defined in the implemented class. This means you can have a default MyMethod() implementation in an abstract class and a different one in a child class.

I hit a requirement for similar functionality today in C# - we have child classes that define particular implementations of an abstract dataset. This is fine and we process this using Generics. But then an issue came out that we need to physically load the data differently for one implementation. As each class instance represents a record and not the whole implementation we need a static method that may or may not have to do custom processing.

Our nice Generics are now out of the window, as we have to run specific code for different implementations, and know when and where to run each one.

Fortunately, you can hack around this a bit using slow, but handy, reflection.
public class Processor where T : BaseClass, new() {
public static void DoSomething() {
System.Reflection.MethodInfo mi = typeof(T).GetMethod("MyStatic");
if(mi != null) mi.Invoke(null, new object[] {}); else BaseClass.MyStatic(); }
}
}

The "MyStatic" static method is defined in the BaseClass, but if blocked with the "new" keyword and reimplemented in a child implementation of BaseClass it is found via reflection and executed instead.

It's not as elegant as PHP but cracks the problem.


XSLT Map error: btm1050Thursday 5th May 2016
error btm1050: XSL transform error: Unable to write output instance to the following <output.xml%gt;. Value cannot be null.
Parameter name: extension

You what now? This error was being produced when testing a BizTalk map that uses XSLT and XML Extensions (i.e. external assemblies). Debugging the map produces a slightly different error:
XslTransformException
---------------------
Cannot find a script or an extension object associated with namespace 'http://whatever/your/namespace/is'.

Where the namespace relates to the first called bound namespace for external assemblies.

This one took me a long time to figure out. In this situation there is a shared extensions XML file with a bunch of possible classes that are used in various different XSLT maps. The error was being produced by a reference to a class that wasn't bound or in use by the XSLT map - this class had been refactored to be abstract. The error I assume was happening when the class was trying to be instantiated - obviously something you can't do when it's abstract.

Removing these old class references from the extensions XML solved all the problems.


Previous Next