Getting the ResourceManager from a strongly typed resource file

I had a little bit of difficulty messing around getting the ResourceManager the class generated when making a resource file (resx). I wanted to get the value of something using a string and do it by accessing the ResourceManager property and calling the GetString() method myself. This is what i used to get the resource manager

Note you will need to make the resource file public if referencing as resource file in another assembly.

// Get the ResourceManager on the type
var resourceManager = (ResourceManager)ResourceType.GetProperty(
"ResourceManager",
    System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static).GetValue(ResourceType, null);

// Get information on the getstring method
return resourceManager.GetString(_resourceKey, CultureInfo.CurrentCulture);

Advertisements

FlagsAttribute [Flags]

Just used this for something I’m working on and thought I better write the details down due to a slight oddity I found while using it.

Note that the important point that you need to remember is that you must start the numbering at 1… not 0(zero)!!!

Take a flags enum like this:

[Flags]
public enum MyFlagsEnum
{
    Archived = 1,
    Closed = 2,
    Open = 4
}

The important thing about the above is that I’ve specified the values and started at one(1) Don’t start with zero(0)

Another way to do this (especially if you’ve got a fair few items:

[Flags]
public enum MyFlagsEnum
{
    Archived = 1,
    Closed = 1 << 1,
    Open = 1 << 2
}

Which generated the same enumeration with the same values.

If you want to combine values into another option, you can do this…

[Flags]
public enum MyFlagsEnum
{
    Archived = 1,
    Closed = 1 << 1,
    Open = 1 << 2,
    All = Archived | Closed | Open
}

The following unit test shows assigning of a new variable of type MyFlagsEnum and assigns the Open and Archived option…

[TestMethod]
public void OpenAndArchived()
{
    MyFlagsEnum options = MyFlagsEnum.Open | MyFlagsEnum.Archived;

    Assert.IsTrue((options & MyFlagsEnum.Open) == MyFlagsEnum.Open);
    Assert.IsFalse((options & MyFlagsEnum.Closed) == MyFlagsEnum.Closed);
    Assert.IsTrue((options & MyFlagsEnum.Archived) == MyFlagsEnum.Archived);
}

As you can see you do this by ORing the flags together

MyFlagsEnum options = MyFlagsEnum.Open | MyFlagsEnum.Archived;

To test whether the  options variable has the Open flag you AND the option against the type that you want to check the variable contains (for lack of a better word!) This anding should return either zero(0) which is why you don’t want to include an enum with a value of zero(0), or will return just the value you are looking for (The ANDing essentially returns a MyFlagsEnum(in this case) which doesn’t includes the value of only the item that you ANDed the variable with)

Assert.IsTrue((options & MyFlagsEnum.Open) == MyFlagsEnum.Open);

Windows Identity Foundation Services WIF (Geneva)

ProxyAttribute… Interesting… Might have to check this out…

Currency Formatting using Culture Name

I’ve been trying to figure out a way to format monetary values based on the ISO Currency Symbol which is are 3 letter string like: USD, AUD, CRC, EUR; that define a currency.

In .NET the RegionInfo class contains an ISOCurrencySymbol property which returns the Currency Symbol for the given region. Now on my system there are seven regions that have “USD” and I can’t use that to format monetary values.

A RegionInfo is constructed takes a culture string like “ar”, “ar-DZ", “en”, “en-US” etc. and each of these cultures has a specific Currency Symbol. So to format a monetary value we only need the culture string/name.

With the Culture Code we can get all the RegionInfo as well as the benefits of being able to format the currency suitably.

Code Example Available Here

The DoCulture method (badly named) shows how to get relevant RegionInfo like the ISO currency symbol as well as how to format based on the culture.

Posted in Uncategorized. Tags: . 2 Comments »

.NET Cryptography

Found some articles which explains cryptography principles which i’ve found helpeful

Cryptography in Microsoft .NET Part 1: Encryption

Cryptography the .NET Way (good articles)

Cryptography in .NET

http://www.developer.com/net/net/article.php/1548761/Cryptography-in-NET.htm
See the first comment! :o) Important!

RSA Encryption in .NET — Demystified!

Writing Culture-Safe Managed Code

Found a link on MSDN which was really helpful regarding culture specific sorting, casing etc.

Writing Culture-Safe Managed Code