.NET: Customizing XML Object Serialization


Serializing C# objects using the .NET framework is a simple task, as is shaping the XML which results from serializing a C# object in terms of XML elements, attributes and namespaces. However, whats not so straightforward, or at least sparsely documented, is controlling what if any namespace and Xml declarations are emitted during serialization. The … Continue reading .NET: Customizing XML Object Serialization

C#: Implementing Generic Method of Generic Interface Causes Runtime Exception


You have an interface declaration which includes a method signature that has a generic parameter and generic constraints. Such an interface can be seen below; And you also have a implementation class, as shown; This compiles perfectly fine. At runtime however, if you attempt to instanciate the implementation class you will receive an Exception; “Method XXX … Continue reading C#: Implementing Generic Method of Generic Interface Causes Runtime Exception

C#: Interruptable Background Processing Service


Working on a prototype this week I needed a console app to create and start a periodic background processing activity, then sit waiting while the background processing activity executed every X seconds, untill you pressed the Ctrl+C key, which stopped the background processing and terminated the application. Ok, so I needed; A background thread to … Continue reading C#: Interruptable Background Processing Service

C#: Determine if a Method is Overridden in a Derived Class


The other day I was doing some work in which I needed to be able to determine from a base class whether a particular method had been overridden in a derived class, and once again reflection comes to the rescue. Consider the class declarations below; We have some simple inheritance here, a base class BaseA and two … Continue reading C#: Determine if a Method is Overridden in a Derived Class

.NET (C#) Impersonation with Network Credentials


I required a C# class to enable ad-hoc user account impersonation for accessing resources both on the local machine and also on network machines, which I’ve reproduced here. Of note, if you require impersonation in order to access network resources, you would intuitively select the logon type of LOGON32_LOGON_NETWORK, this however doesn’t work, as according to … Continue reading .NET (C#) Impersonation with Network Credentials

Debugging .NET Serialization Code


Just what is your XmlSerializer doing? You can find out by debugging the serialization code which is generated automatically at runtime; 1. Modify your .config file to include the following snippet 2. Rebuild your code and set a breakpoint on or just after where you create an instance of the XmlSerializer, but before you call … Continue reading Debugging .NET Serialization Code