Posts tagged “.net”

Warning - ExtensionAttribute is defined in multiple assemblies

I’ve noticed a warning in Visual Studio in a few projects where it gives me the following warning after building,

The predefined type "System.Runtime.CompilerServices.ExtensionAttribute" is defined in multiple assemblies in the global alias

I don’t like leaving warnings in my build but I didn’t have time to look into it until today. This handy tip from Remco te Wierik led me to the root of the issue. The .net Lucene contrib nuget package contains a reference Lucene.Net.FastVectorHighlighter which defines an ExtensionAttribute class in the System.Runtime.CompilerServices namespace.

Updating the assembly reference alias from “global” to a custom one, fixed the warning.

Read more →

Instant insanity solver

I was visiting my sister for Christmas and they had a puzzle for us to play with. It’s called the Instant Insanity puzzle, or at least it was a variation of it with old comic book covers instead of plain colours like the one on Wikipedia.

Pretty much everyone had a shot but nobody could quite get all 4 cubes to have different sides. Like a true geek I decided to write some code to solve it for me.

I put a little sticker with an arrow on each cube so I could identify them and what direction was “north”. This allowed me to map the cubes in code and also reverse the map when the code found a solution.

You can find the code here: GitHub

As it says in the Readme, it definitely needs refactored but it got the job done. It ran pretty much instantly and found 4 solutions (you can turn the stack 90 degrees and still have the same solution). It also finds 65,532 non solutions but I have to assume there are four of each one, making there 16,383 invalid solutions. A lot less than claimed on the box…

Read more →

Caching file contents with CacheDependency

The CacheDependency will monitor a file or folder for changes. When it detects a change it will remove the associated key from the cache.

This comes in quite handy for my hangman game which stores the film titles in an xml file.

Below is how I cache the data and setup the CacheDependency. Some validation has been removed to keep it clear.

private HangmanGameCollection ReadGameFile()
{
    if (HttpRuntime.Cache["HangmanGame"] != null)
    {
        return (HangmanGameCollection)HttpRuntime.Cache["HangmanGame"];
    }

    HangmanGameCollection hangmanGames;

    FileInfo xmlFile = new FileInfo(Server.MapPath("~/TopSecret.xml"));

    XmlSerializer xmlSerializer = new XmlSerializer(typeof(HangmanGameCollection));

    using (FileStream fileStream = xmlFile.OpenRead())
    {
        hangmanGames = (HangmanGameCollection)xmlSerializer.Deserialize(fileStream);

        // Put it in cache with a dependency on the xml file
        HttpRuntime.Cache.Insert("HangmanGame", hangmanGames, new CacheDependency(xmlFile.FullName));
    }

    return hangmanGames;
}

Should maybe do something about fitting in long lines of code :\

Read more →