Linq Expressions By Sample: Calling Generic Methods With Lambda Parameters For Unknown Types

Suppose you need to call a generic Method that receives a generic delegate parameter. Now suppose that you neither know the type of the generic argument for the method nor the implementation of the delegate parameter at compile time. You could build a Linq Expression at runtime to solve this problem. Here’s how.Read More »

Loading Assemblies In New AppDomain (And Unloading Them)

Today I had an issue with assemblies in an AppDomain. What I tried to do was to create a new AppDomain, load assemblies into that AppDomain, analyze the types in these assemblies, and unload the AppDomain afterwards. Sounds like straight-forward, but I overlooked a little details and ended up with these assemblies being loaded into my main AppDomain and me not being able to unload them.Read More »

Change Current Thread’s Currency Symbol

Consider the following code. It shows how a decimal value is converted into a string that contains an amount and a currency symbol.

var value = 1099.99m;
Console.WriteLine("{0:C}", value);

// Output: 1,099.99 $

What if we wanted to output a different currency symbol (e.g. “USD”, “€” or “Darsek”) but keep the current number formatting and culture info? If you try the following code, you will probably get an InvalidOperationException stating that the “Instance is read-only“.

Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol = "Darsek";

This happens because usually the current thread’s culture info is set to a read-only instance that cannot be modified. You will need to create a new instance of CultureInfo. Thankfully, there is a method called “Clone” that we can use to create a new CultureInfo instance with the same properties as the current culture.

var culture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
culture.NumberFormat.CurrencySymbol = "Darsek";
Thread.CurrentThread.CurrentCulture = culture;

var value = 1099.99m;
Console.WriteLine("{0:C}", value);

// Output: 1,099.99 Darsek