typeinitializer

Try/catch blocks inside constructors

和自甴很熟 提交于 2019-12-31 22:04:27
问题 Is it a bad programming practice to have try/catch blocks inside constructors? Or does it make no difference as long as our programs handle typeinitializer exceptions gracefully. In C# if there are any exceptions inside a constructor the framework always throws typeinitilizer exceptions. Thanks, Shamika 回答1: System.TypeInitializationException is thrown when a static constructor throws an exception, not on an instance constructor. Exceptions are thrown normally in instance constructors. That

Why would finding a type's initializer throw a NullReferenceException?

大城市里の小女人 提交于 2019-12-18 09:57:09
问题 This has got me stumped. I was trying to optimize some tests for Noda Time, where we have some type initializer checking. I thought I'd find out whether a type has a type initializer (static constructor or static variables with initializers) before loading everything into a new AppDomain . To my surprise, a small test of this threw NullReferenceException - despite there being no null values in my code. It only throws the exception when compiled with no debug information. Here's a short but

Forcing class load

空扰寡人 提交于 2019-12-04 17:21:02
问题 Is there a way in C# or .net IL to force a class that has a type initializer (static constructor) to load itself, without accessing any of its parameters? Assuming I've got the class public static class LogInitialization { static LogInitialization() { System.Console.WriteLine("Initialized"); } } Is there a way to get this line to print? Note that the class is static so I can't instantiate it to force initialization, and it has no public members so I can't access them to start it. 回答1:

Forcing class load

本秂侑毒 提交于 2019-12-03 11:17:14
Is there a way in C# or .net IL to force a class that has a type initializer (static constructor) to load itself, without accessing any of its parameters? Assuming I've got the class public static class LogInitialization { static LogInitialization() { System.Console.WriteLine("Initialized"); } } Is there a way to get this line to print? Note that the class is static so I can't instantiate it to force initialization, and it has no public members so I can't access them to start it. Rummaging in the CLI spec, I found a reference to the method RuntimeHelpers.RunClassConstructor If a language

Try/catch blocks inside constructors

 ̄綄美尐妖づ 提交于 2019-12-03 03:11:20
Is it a bad programming practice to have try/catch blocks inside constructors? Or does it make no difference as long as our programs handle typeinitializer exceptions gracefully. In C# if there are any exceptions inside a constructor the framework always throws typeinitilizer exceptions. Thanks, Shamika System.TypeInitializationException is thrown when a static constructor throws an exception, not on an instance constructor. Exceptions are thrown normally in instance constructors. That aside, there's nothing "wrong" with it any more than it is anywhere else; handle exceptions that you can

Why doesn't the CLR always call value type constructors

久未见 提交于 2019-11-29 22:12:04
I have a question concerning type constructors within a Value type . This question was inspired by something that Jeffrey Richter wrote in CLR via C# 3rd ed, he says (on page 195 - chapter 8) that you should never actually define a type constructor within a value type as there are times when the CLR will not call it. So, for example (well...Jeffrey Richters example actually), I can't work out, even by looking at the IL, why the type constructor is not being called in the following code: internal struct SomeValType { static SomeValType() { Console.WriteLine("This never gets displayed"); }

Why would finding a type's initializer throw a NullReferenceException?

牧云@^-^@ 提交于 2019-11-29 18:55:29
This has got me stumped. I was trying to optimize some tests for Noda Time, where we have some type initializer checking. I thought I'd find out whether a type has a type initializer (static constructor or static variables with initializers) before loading everything into a new AppDomain . To my surprise, a small test of this threw NullReferenceException - despite there being no null values in my code. It only throws the exception when compiled with no debug information. Here's a short but complete program to demonstrate the problem: using System; class Test { static Test() {} static void Main

Why doesn't the CLR always call value type constructors

南楼画角 提交于 2019-11-28 18:34:01
问题 I have a question concerning type constructors within a Value type . This question was inspired by something that Jeffrey Richter wrote in CLR via C# 3rd ed, he says (on page 195 - chapter 8) that you should never actually define a type constructor within a value type as there are times when the CLR will not call it. So, for example (well...Jeffrey Richters example actually), I can't work out, even by looking at the IL, why the type constructor is not being called in the following code: