appa yip yip
appa yip yip

Reputation: 1454

Weird exception being thrown when initializing static variable

I recently had a very weird System.ArgumentException.

The following code was at my MainWindow constructor on my WPF Application

CodeDefinitions.DEFAULT_AVALIABLE = (() => { return true; });

But every time a run the app, this is what I get: System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

So obviusly there where something wrong, so I put the line inside a try/catch block, like this:

try
{
    CodeDefinitions.DEFAULT_AVALIABLE = (() => { return true; });        
}
catch()
{

}

And put an breakpoint at catch's '{', and this is what I got as $exception:

[System.TypeInitializationException] {"The type initializer for 'ComunicadorSerial.Classes.Utils.CodeDefinitions' threw an exception."}

That tells me nothing, so I took a look at _innerException:

_innerException {"An item with the same key has already been added."}   System.Exception {System.ArgumentException}

As far as I know, this excepton is throw when using a Dictionary, but the most curious thing is that DEFAULT_AVALIABLE is a Func<bool>:

internal static Func<bool> DEFAULT_AVALIABLE;

Does anyone knows anything about this? It seems so dumb, but I just can't figure it out.

Thanks in advance!

Upvotes: 3

Views: 555

Answers (1)

nvoigt
nvoigt

Reputation: 77324

Initializing a static variable invokes the static constructor if it has not been invoked previously.

Without seeing actual code it's impossible to tell whats wrong, but check your static constructor and the other static members for anything that might throw this exception.

Upvotes: 4

Related Questions