I have a class named 'Utility' which contains several static members. On calling any of those static members, i get an exception 'The type initializer for Utility.cs threw an exception'. I can't figure out what is wrong. Please help!
Asked
Active
Viewed 678 times
-3
-
Please show the code! – C-Otto Jan 23 '18 at 12:16
-
A `TypeInitializerException` means that the class failed to instantiate. Please add a [mcve] – Camilo Terevinto Jan 23 '18 at 12:16
-
Read [ask], create a [mcve]. This means a static constructor threw an exception. – CodeCaster Jan 23 '18 at 12:17
1 Answers
1
You have a static constructor (static Utility() {...this bit...}
) or a static field initializer (static SomeType someField = {...this bit...};
) that is failing inside Utility
. Find out why. You could start by looking at the .InnerException
of the exception that is being thrown.
![](../../users/profiles/23354.webp)
Marc Gravell
- 927,783
- 236
- 2,422
- 2,784
-
Its not a static class, and I haven't defined any static constructor in the class scope. I am just calling the static members of the Utility class in other classes with the class name. – Uzair Raza Jan 23 '18 at 12:22
-
@UzairRaza I never said anything about it being a static class; and if you don't have a static constructor, then that leaves: static field initializers. So: check for static field initializers (values on the right of a `=` in a static field declaration), and : look at the `.InnerException` – Marc Gravell Jan 23 '18 at 12:34
-