It is undocumented, but it looks like one of the optimizations in .NET 4.5. It appears to be used to prime the reflection type info cache, making subsequent reflection code on common framework types run faster. There’s a comment about it in the Reference Source for System.Reflection.Assembly.cs, RuntimeAssembly.Flags property:

// Each blessed API will be annotated with a "__DynamicallyInvokableAttribute".
// This "__DynamicallyInvokableAttribute" is a type defined in its own assembly.
// So the ctor is always a MethodDef and the type a TypeDef.
// We cache this ctor MethodDef token for faster custom attribute lookup.
// If this attribute type doesn't exist in the assembly, it means the assembly
// doesn't contain any blessed APIs.
Type invocableAttribute = GetType("__DynamicallyInvokableAttribute", false); 
if (invocableAttribute != null) 
    ConstructorInfo ctor = invocableAttribute.GetConstructor(Type.EmptyTypes); 
    Contract.Assert(ctor != null); 
    int token = ctor.MetadataToken;

This sort of code makes me very angy!

Tell me what’s wrong with this simple piece of code:

public class MyService : IMyService, IDisposable 
    private readonly IOtherService _otherService;
    public MyService(IOtherService otherService)
        _otherService = otherService;

    public void Dispose()

The answer is very simple: “I created you and I will be your end”, yet MyService didn’t create IOtherService, thus it has not right to call Dispose() on the service.”

Yet, at Mizuho, I saw many developers lacking this simple, yet critical design concept!


Unit Testing – Setup pattern (Moq)

Frank Code

In my previous post I mentioned that large unit test setups can be difficult to maintain / understand. This problem can be reduced by following a test pattern. I want to share with you a pattern I use, and I think works really well. In this post I will be using C#, Moq libary and Visual Studio test tools.

To start here are some things I believe make a good suite of unit tests:

  • Easy to identify data dependencies of tests.
  • Mocking is not repeated every test.
  • Test are clear and easy to understand / maintain.
  • New tests can be added with relative ease.

In order to explain this testing pattern I have forked my buddies TodoMVC app, and introduced a service layer, repository pattern and test project. You can see my source code with an example test class on github.

The TodoModule (service layer) has a number of public methods; Get…

View original post 315 more words

Object Oriented, Test Driven Design in C# and Java: A Practical Example Part #1

Download the code in C#

For a brief overview, please refer to this post.

At this point, many tutorials start by launching into a “Hello, World” style tutorial, with very little practical basis.


This isn’t the most exciting concept, so let’s try a more practical example. Instead of churning out boring pleasantries, our application is going to do something a bit more interesting…build robots.


Specifically, our application is going to build awesome robots with big guns.

Ok, let’s get started with a narrative description of what we’re going to do.

“Mechs with Big Guns” is a factory that produces large, robotic vehicles designed to shoot other large, robotic vehicles. Robots are composed of several robotic parts, delivered by suppliers. Parts are loaded into a delivery bay, and are transported by worker drones to various rooms; functional parts such as arms, legs, etc., are dispatched to an assembly room. Guns…

View original post 876 more words

Working on a new compression algoriphm

Can’t disclose how it will work. But early tests indicate that if you take a reasonable sized RAR archive compressed to the max and place it in my container, you should expect to achieve a further 15-17% compression.

Now the plan is to finish the library and post the service as a website where you can compress and uncompress a single file. I will probably put a limit on the file around 50 Mb mark. This will allow me to gather vital statistics (without killing my server).

If all checks out I will add support for multiple files and release it as open source. Still thinking about the last step though 😛

Interesting behaviours with enum in .NET

This is completely plausible and the check is avoided first for performance reasons. Just imagine the CLR having to enumerate all the valid values and check whether yours falls in it, this no longer becomes a simple cast. The other reason is versioning. That is why you have Enum.IsDefined(…). Some developers now rely on this feature when dealing with an enum defining flags. When they want to set all flags they simply cast int.MaxValue to Enum

Bernado Nguyen-Hoan's Blog - Coding Stories from an IT Mercenary

When defining an enum type, we can assign an int value to each of the enum members. Below is an example.

Now there are two behaviours that you may not be aware of. Firstly, given the above definition, you can actually assign any int value to a variable of type Status. Let say I have a class as below.

The following code will then happily compile and execute:

OK, who on earth would be doing something like that? Probably not many, but this leads on to the second behaviour: by default, the value of the enum will be 0, even if 0 is not one of the defined enum members.

So with the code below, the value of record.Status will be 0, which does not match any of the defined status values:

This behaviour could have an impact on your code if not handled properly. This is because when defining an enum…

View original post 95 more words

IEnumerable vs IQueriable



-IEnumerable exists in System.Collections Namespace.
-IEnumerable can move forward only over a collection, it can’t
move backward and between the items.
-IEnumerable is best to query data from in-memory collections like
List, Array etc.
-While query data from database, IEnumerable execute select query
on server side, load data in-memory on client side and then filter
-IEnumerable is suitable for LINQ to Object and LINQ to XML
-IEnumerable supports deferred execution.
-IEnumerable doesn’t supports custom query.
-IEnumerable doesn’t support lazy loading. Hence not suitable for
paging like scenarios.
-Extension methods supports by IEnumerable takes functional

IEnumerable Example

MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p =>
list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee]
AS [t0]
WHERE [t0].[EmpName] LIKE @p0

Notice that in this query “top 10” is missing since IEnumerable
filters records on…

View original post 155 more words

Converting XML or JSON to C# Classes in Visual Studio

Kapil's space

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:

  • Edit.
  • 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.


View original post

Covariance and Contravariance. What do they mean again?

My Software Notes

I don’t have to deal with covariance and contravariance very often, so every time I do I forget which is which. So, just for my own benefit, here is a note describing them:

Covariance and contravariance are terms that refer to the ability to use a less derived or more derived type than originally specified.

Covariance – Enables you to use a more specific type than originally specified

E.g., in generics

We have a base type called “Feline” and we have a derived type called “HouseCat”.

IEnumerable<HouseCat> cats = new List<HouseCat>();
IEnumerable<Feline> gods = cats;
//Note: felines have a very high opinion of themselves 🙂

Contravariance – Enables you to use a less derived type than originally specified.

E.g., in generics

Using the same base and derived types as above.

IEnumerable<Feline> gods = new List<Feiline>();
IEnumerable<HouseCat> cats = gods;


Covariance and Contravariance (C# and Visual Basic)

Covariance and Contravariance…

View original post 2 more words

ASP.NET MVC – If only we had it 10 years ago

I realised that one of the technology or rather a group of the technologies that stops me from applying for some jobs is ASP.NET MVC + the usual JavaScript stack.

So I bought 2 books on the subject and have been playing with the technology for couple of days now. And I must say things have improved A LOT since I’ve been doing Web Development.

Back in 2003 I were working on an Order Management System and my boss being a perfectionist and a great fan of Google, requested that we created an order input form, comprised of 6 tabs without any postbacks to the server (‘coz they are evil)

All we had at our disposal was ASP.NET and JavaScript. But somehow we managed to archive what he wanted. We of course had to use some serious black magic to fetch data asynchronously (remember no AJAX back then) instead we realised we could use hidden Iframes to fetch all the required data. And the application (the order entry form) became extremely nice to use.

The traders would no longer have to remember the exact ticker, as long as they knew the name the app would asynchronously go and fetch corresponding tickers.

Once you selected a ticker, you would be presented with some information about the instrument, settlement date and the fees would automatically default based on the market where the instrument was traded on, if it was a bond it will also calculate yield, time to maturity, for convertible bonds it will show you all the greeks and much more (limits, holiday calender per market, settlement periods, compliance stuff, history of changes etc). Not only that but the could switch to a different tab effortlessly to see the executions and modify them if necessary. I’m not going to go to cover the other 95% of features, but the app was brilliant in terms of usability, only Google could complete.


Continue reading “ASP.NET MVC – If only we had it 10 years ago”

Blog at WordPress.com.

Up ↑