473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Static Constructor throws exception before called.

Fr33dan
57
Hi,

Long Version with potentially superfluous details:
I have a C# project that I use as a library for multiple programs (The point being it has be tested to and functioned properly). However with my latest program it has been giving me problems. From a static constructor on a custom form I call a static methods of a Config class. This is when I get a TypeInitalizationException from the Config class. The inner exception is a SecurityException which according the stack trace is from "System.Reflection.Assembly.get_Location()" called from Config's constructor. I have never heard of this method and don't call it in Config's constructor (do use other parts of Reflection.Assembly). The weirdest part is that if I put a breakpoint in the first line of the constructor the exception is thrown before it can get there.

Short Version:
A static constructor is throwing an error from an System.Reflection.Assembly method I don't call and a breakpoint of the constructor on the first line isn't reached.

I can post code but there is alot of it and I'm not entirely sure where the problem code is.
Nov 5 '10 #1

✓ answered by Plater

I had trouble when I would do the following:
Expand|Select|Wrap|Line Numbers
  1. MyCustomClass Myobj=null;
  2. //stuff
  3. Myobj=new MyCustomClass();
  4.  
And I would get the TypeInitalizationException on the line: MyCustomClass Myobj=null;
It turned out that if a failure happens durring the creation of the class like "footprint"(creating the Type, not the instance) I was getting that error.
In my class I had something like:
Expand|Select|Wrap|Line Numbers
  1. private int _startpoint=2;
  2. private string _myname=Assembly.(something)("Name");
  3.  
Or something of the sort and THOSE class were failing and giving me the TypeInitalizationException.

Maybe you can peek through that?
Set a breakpoint at the first "thing" in your class, not the constructor, but the first member variable or whatever, and use F10 to step on single statements until you get the exception?

3 2987
Christian Binder
218 Expert 100+
Maybe the assembly which contains the Config-class references to another assembly which can't be found. Does the TypeInitializationException tell you which class it couldn't find?

I think this exceptions are thrown when the class (Config) is loaded, so it's before the static-ctor is called.
Nov 8 '10 #2
Plater
7,872 Expert 4TB
I had trouble when I would do the following:
Expand|Select|Wrap|Line Numbers
  1. MyCustomClass Myobj=null;
  2. //stuff
  3. Myobj=new MyCustomClass();
  4.  
And I would get the TypeInitalizationException on the line: MyCustomClass Myobj=null;
It turned out that if a failure happens durring the creation of the class like "footprint"(creating the Type, not the instance) I was getting that error.
In my class I had something like:
Expand|Select|Wrap|Line Numbers
  1. private int _startpoint=2;
  2. private string _myname=Assembly.(something)("Name");
  3.  
Or something of the sort and THOSE class were failing and giving me the TypeInitalizationException.

Maybe you can peek through that?
Set a breakpoint at the first "thing" in your class, not the constructor, but the first member variable or whatever, and use F10 to step on single statements until you get the exception?
Nov 8 '10 #3
Fr33dan
57
Sorry for the long delay (I only work on this project 2 days a week). That was a push in the right direction. The error was on a static variable that I wrote before the static constructor and forgot about (And thus wasn't checking the initialization of). Now I just need to figure out why this line is throwing a SecurityException:
Expand|Select|Wrap|Line Numbers
  1. private static string defaultsLocation = (System.Reflection.Assembly.GetEntryAssembly() == null? null: System.Reflection.Assembly.GetEntryAssembly().Location.Replace(System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ".exe", ""));
Since the problem that I started the thread with is solved I will mark your answer as the correct one and start a new thread if I can't figure it out.

Thanks
Nov 10 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: A.M | last post by:
Hi, I have i utility class contains static functions (and also members) I usually use in may applications. Can i have a static like constructor so any time the app starts, The constructor...
10
by: Marc Selis | last post by:
Hi, I have a class with a static constructor in which the class registers itself as capable of doing something (using a delegate) The problem is that the static constructor is never called, as...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
5
by: | last post by:
Hi, How long do webservice objects live for? In particular, if i have static variables filled with data from a static constructor in a webservice, how long will that data persist? thxs
8
by: Jeff Brown | last post by:
I understand that a static constructor is not called until and unless the class is instantiated, or any of its static members are referenced. But suppose (for the sake of argument) a class is...
1
by: Martin | last post by:
Hi. I've found a few topics on this subject, but still not sure and decided to post mine. Here's my example: #include <iostream> using namespace std;
8
by: =?Utf-8?B?cnZtYWNjb3VudA==?= | last post by:
It seems the static constructor is supposed to be called, ONLY once for a particular type. If that's the case, why does the following code throws an exception? using System; using...
4
by: indrawati.yahya | last post by:
According to the FAQ, the best way to inform a class user of an error that occurs inside a constructor is to throw an exception. My question is, what happens when an object is instantiated using...
6
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The...
5
by: Dave | last post by:
Hello, Suppose you have a class with a static property with only a get (read only). You also have code in a static constructor that sets these properties but takes 1 hour to run. Now suppose...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.