Compiler: static constructors and field init compilation order

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Hi everyone,

Is it possible, using an Attribute or by some other means, to notify the C#
Compiler to serialize all static field's that have initializers before code
in an explicit static constructor?

Example:

public class MyClass
{
public static readonly int MyStaticField = 1;

static MyClass()
{
// Compiler emits the code to initialize MyStaticField as 1 here

Console.WriteLine();

// I need the compiler to emit the code here
}
}

-- I know it might not be clear as to why I need this functionality by
looking at the example above, but it would be helpful in my real-world
situation.

If you need details as to why I need this functionality from the compiler,
just let me know and I'll post it.

Thanks in advance,

~ Dave
 
Dave said:
Is it possible, using an Attribute or by some other means, to notify the C#
Compiler to serialize all static field's that have initializers before code
in an explicit static constructor?

I'm not sure what the benefit would be - surely all the fields will
just have the default values for their types, won't they?
Example:

public class MyClass
{
public static readonly int MyStaticField = 1;

static MyClass()
{
// Compiler emits the code to initialize MyStaticField as 1 here

Console.WriteLine();

// I need the compiler to emit the code here
}
}

If you really need some code to be executed before the rest of the
variable initializers, why not have one variable initializer at the
top:

bool ignored = DoStuffNow();

static bool DoStuffNow()
{
// Do everything here...
return true;
}
-- I know it might not be clear as to why I need this functionality by
looking at the example above, but it would be helpful in my real-world
situation.

If you need details as to why I need this functionality from the compiler,
just let me know and I'll post it.

I don't *need* details as to why you want this, but I must say I'm
intrigued and would like to know more.
 
Hi Jon,

Thanks for responding.

I will try to explain this the best that I can:

I have about 20 of the "MyStaticField" inline initializers, all of them of
type SqlCommand. I expect to have more in the future and use #region to
categorize them in an orderly fashion. I would like to keep them inline if
possible (ie not in a Method "DoStuffNow" or a static constructor) because
it would be somewhat of a hassle to manage the constructor code and the
various readonly variable declarations. Each of the intializers use a
helper function I've written that requires a certain static field (in the
helper library) to be set. The field is set to a default value per a custom
section in the web.config file. I'd like to programmatically set this field
to a different "default" value upon static construction of the class being
discussed.

In other words...

Each inline initializer depends upon a variable that must be changed before
they are "initialized". The most appropriate place to set this variable
would be in the static constructor of this class. The C# compiler
automatically serializes all inline statements before the statements in the
explicit static constructor. Obviously this is not what I need, as I want
my constructor statements to change the "default" value before the commands
are actually created.

I hope I've made my situation clear enough to you.

I'm open to any work-arounds or solutions that are different than my
expectations, although I've researched on google groups and some books and
have found no way of doing this... or even something like it for that
matter. :(

Well, I appreciate your time and thank you for your responses in advance...

~ Dave
 
Dave said:
I have about 20 of the "MyStaticField" inline initializers, all of them of
type SqlCommand. I expect to have more in the future and use #region to
categorize them in an orderly fashion. I would like to keep them inline if
possible (ie not in a Method "DoStuffNow" or a static constructor) because
it would be somewhat of a hassle to manage the constructor code and the
various readonly variable declarations. Each of the intializers use a
helper function I've written that requires a certain static field (in the
helper library) to be set. The field is set to a default value per a custom
section in the web.config file. I'd like to programmatically set this field
to a different "default" value upon static construction of the class being
discussed.

Right. I can't say it sounds like a terribly pleasant design to me, but
there we go. The only thing I can suggest is the example I gave before
- put a "dummy" value at the top of your class file, and make that use
a method which sets the default value you want the rest of your
initializers to use.

You should be aware, however, that if you've got two different classes
being initialized at the same time, everything could go very pear-
shaped.
 
Back
Top