static-constructor

Assembly.GetCallingAssembly() and static constructors?

Ok, so I just ran into the following problem that raised an eyebrow. For various reasons I have a testing setup where Testing classes in a TestingAssembly.dll depend on the TestingBase class in a BaseTestingAssembly.dll. One of the things the TestBase does in the meantime is look for a certain embedded resource in its own and the callin...

Static initializer in Objective-C upon Class Loading

I am trying to build something to dynamically instantiate an object from class-name similar to how Java's Class.forName method works, e.g. Class klass = Class.forName("MyClass"); Object obj = klass.instantiate(... I didn't see any such behavior in Objective-C so I would like to call a method to register Class when an Objective-C class...

static constructors in C++? need to initialize private static objects

I want to have a class with a private static data member (a vector that contains all the characters a-z). In java or C#, I can just make a "static constructor" that will run before I make any instances of the class, and sets up the static data members of the class. It only gets run once (as the variables are read only and only need to be...

Type initializer (static constructor) exception handling

I'm writing a WCF service in C#. Initially my implementation had a static constructor to do some one-time initialization, but some of the initialization that is being done might (temporarily) fail. It appears that static constructors are only called once, even if the first (failed) attempt threw an exception? Any subsequent attempts to ...

Assigning to static readonly field of base class

public class ClassA { public static readonly string processName; } pubic class ClassB : ClassA { static ClassB() { processName = "MyProcess.exe"; } } I am getting an error while compiling the above C# code. The error says -- "A static readonly field cannot be assigned to (except in a static constructor or a ...

Using Static Constructor (Jon Skeet Brainteaser)

As a relative newbie I try to read as much as I can about a particular subject and test/write as much code as I can. I was looking at one of Jons Brainteasers (question #2) and my output was different than the answer. Which makes brings me here to ask if something has changed in recent versions and to see what output others are getti...

Controlling when the Static Constructor is called

In my custom attribute's static constructor, I search the loaded assembly for all classes decorated with my attribute and perform some action on them. I would like the static constructor to be called as soon as possible during runtime, preferably before execution of the static void Main() entry point. Currently it only gets called afte...

Creating a singleton ChannelFactory<T> and reusing for client connections

In our SharePoint/ASP.NET environment we have a series of data retriever classes that all derive from a common interface. I was assigned the task of creating a data retriever that could communicate remotely with other SharePoint farms using WCF. The way I have it implemented at the moment is a singleton ChannelFactory<T> is created in a ...

Is it possible to call an instance method from a static constructor in WCF service?

Is it possible to call an instance method from a static constructor in WCF service? Is there something like current context through which I can get the current instance of MyService? public class MyService : IMyService { static MyService() { //how to call Func? } private void Func() { } } EDIT: This q...

Is a Java static block equivalent to a C# static constructor?

What is the real difference between a C# static constructor and a Java static block? They both must be parameterless. They are both called only once, when the related class is first used. Am I missing something, or are they the same thing, just with different names? ...

Returning in a static initializer

Hello, This isn't valid code: public class MyClass { private static boolean yesNo = false; static { if (yesNo) { System.out.println("Yes"); return; // The return statement is the problem } System.exit(0); } } This is a stupid example, but in a static class const...

Why isn't the static constructor of the parent class called when invoking a method on a nested class?

Given the following code, why isn't the static constructor of "Outer" called after the first line of "Main"? namespace StaticTester { class Program { static void Main( string[] args ) { Outer.Inner.Go(); Console.WriteLine(); Outer.Go(); Console.ReadLine(); ...

How can I run a static constructor?

I'd like to execute the static constructor of a class (i.e. I want to "load" the class) without creating an instance. How do I do that? Bonus question: Are there any differences between .NET 4 and older versions? Edit: The class is not static. I want to run it before creating instances because it takes a while to run, and I'd like to...

Is RunClassConstructor guaranteed to run a type's static constructor only once?

I'm calling the static ctor of a class using this code: Type type; System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle); Can this cause the cctor to be run twice? ...

Freestanding ARM C++ Code - empty .ctors section

I'm writing C++ code to run in a freestanding environment (basically an ARM board). It's been going well except I've run into a stumbling block - global static constructors. To my understanding the .ctors section contains a list of addresses to each static constructor, and my code simply needs to iterate this list and make calls to each...

Public constructor and static constructor

I am reading a code in C# that uses two constructors. One is static and the other is public. What is the difference between these two constructors? And for what we have to use static constructors? ...

Why doesn't the CLR always call value type constructors

Hi, 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...

What is the earliest entrypoint that the CLR calls before calling any method in an assembly?

In the past years I've occasionally been wondering what equivalent of the (in)famous DLL_PROCESS_ATTACH was available in the .NET world. Any documentation I have says, slightly simplified, that the earliest entry point to a class is the static constructor (cctor), but you cannot influence when it is called, nor can you define one cctor t...

How do I implement a static dictionary<T> with parameters at Runtime in C#?

I have the following code: public static class ScraperMasterUriDetails { public static Dictionary<Guid, string> MasterUriDetails; } However I've decided that I need to add an integer to the dictionary Dictionary<ScraperMasterUriDetails>, so I thought I'd add properties and a few parameters to the constructor. But,...

C# static garbage collector?

I have a simple class which has a static constructor and a instance constructor. Now when i initialized the class , both static and instance constructor are called. Only static is referred once in a application domain . Can i again call the same class initialization and static constructor initialize again? I have tried but it didn't happ...