It’s a very rare requirement, but sometimes in .NET you have to create your own primitive and make it behave as close as possible to a native CTS (common type system) type. “That shouldn’t be hard” would be your first thought, until you start considering all the scenarios in which it could be used. Continue reading “Creating your own primitive type”
Invoking event handlers in C# has always been a bit of a pain, because an event with no subscribers is usually represented as a null reference. This leads to code like this:
It’s important to use the
handler local variable, as if instead you access the field twice, it’s possible that the last subscriber will unsubscribe between the check and the invocation:
Now this can be simplified slightly using an extension method:
Then in each event, you can write a single line:
However, this means having a different extension method for each delegate type. It’s not too bad if you’re using
EventHandler but it’s still not ideal.
C# 6 to the rescue!
The null-conditional operator (
?.) in C# 6 isn’t just for properties. It can also be used for method calls. The compiler does the right thing (evaluating the expression only once) so you can do…
View original post 407 more words
Here is an excellent article on how to write your own shell extensions in C#
So it happens that the company I started working for yesterday uses Git for all their source control needs. Having heard a lot of positive stuff about Git, but having no experience of actually using it,I had to dig around to find some decent integration tools that work well with Visual Studio 2013.
To my surprise, as of Visual Studio 2012.2, Microsoft already supports Git (I had to double check this and pinch myself!) There are Visual Studio Tools for Git available for download directly from Microsoft and there is quite a chunky write up Getting Started with Git in Visual Studio and Team Foundation Service
Will try it out at work today!
In the Visual Studio 2012 IDE, a new feature is introduce to convert XML document into C# classes as a Serializable type.
In the .NET framework 4.5 there is another Paste Special option is exists in the Edit menu which enables you to copy the XML as C# Classes.
Copy the XML you want to create a class/classes for, place the cursor in a class file on the location you want the code to be added and select the following menu items:
- Paste Special.
- Paste XML as Classes.
And you’re done.
Suppose you have following XML File.
And when you paste the above XML using Paste XML as classes. It will looks like that.
In order to configure Visual Studio 2013 do the following in the Tools -> Options -> Debugging -> General menu:
- Disable just my code
- Disable step over properties and operators
- Disable require source files to exactly match the original version
- Enable .NET framework source stepping
- Enable source server support
This is what you need to do:
An application domain is the run-time unit of isolation in which a .NET program runs. It provides a managed memory boundary, a container for loaded assemblies and application configuration settings, as well as delineating a communication boundary for distributed applications. Each .NET process usually hosts just one application domain: the default domain, created automatically by the CLR when the process starts. It’s also possible — and sometimes useful — to create additional application domains within the same process. This provides isolation while avoiding the overhead and communication complications that arise with having separate processes. It’s useful in scenarios such as load testing and application patching, and in implementing robust error recovery mechanisms.
Application Domain Architecture
In most cases, the processes housing the application domains are created implicitly by the operating system — when the user double-clicks your .NET executable file or starts a Windows service. However, an application domain can also be hosted in other processes such as IIS or in SQL Server through CLR integration. In the case of a simple executable, the process ends when the default application domain finishes executing. With hosts such as IIS or SQL Server, however, the process controls the lifetime, creating and destroying .NET application domains as it sees fit.
The keyword partial has been with us for a while now. Until recently I couldn’t find a use for partial classes/structs unless some of the code is auto generated and you want to make sure you don’t interfere with auto generated code. That is until recently I have been asked to create a struct representing a local date – a data that is not affected by time-zones and lacks a time component,
To make it as close as a native CLR type I had to implement a bunch of interfaces, ISerializable, IEquitable<T>, IXmlSerializable. In addition it had to implement some explicit and implicit convertions, !=, ==, >=, <=, > and < operators. Soon, what seemed like a simple struct grew into couple of thousands lines of code,
There is nothing preventing you from putting all the code into one big file, however I think I found a more elegant solution – use the partial keyword and split one large LocalDate by either the interface being implemented or things it’s implementing. So I ended up with:
I have also used one of the Visual Studio plugins to nest all of those files under a single LocalData.cs.
So far I couldn’t come back with a single draw back of such approach. Please let me know if there are any!
Improvements when debugging .NET Reference Source
Historically since the inception of this effort, Microsoft have published sources and PDBs for every major .NET framework update namely .NET framework 4.0 and 4.5. However these builds would be rendered effectively useless the moment any update to the framework was released, since the binaries on the updated box no longer matched the PDBs that were indexed on the reference source server. Unfortunately the design of the system that they had in place was geared towards doing single and infrequent pushes of sources and symbols out and did not account for the sheer volume of builds and patches that come are produced out of the .NET framework build system.