❔ Static Field get not called!
Hey i try to make a Registry a static field call the Register Method but the static field get never called, if i would remove the static it would works but it should possible to get it anyway.
22 Replies
i hope anyone know why my static field get not called
What do you mean by "field not get called"?
Do you mean the
Register
method is never invoked?yea
but just if the field is static
A type's static constructor is called at any point between the assembly being loaded, and a member of the type being accessed for the first time
(it's a little bit more complex, with beforefieldinit etc)
But basically,
Register
will be called the first time that someone accesses a member on the type, or at any unspecified point beforehää
but static works normaly like class get created this field get created too or not?
so to fix that i need to remove the static thing or call it?
is there no other way? like static and get created instand
I'm afraid I'm having a hard time working out what you're saying
"get created instand"?
like if it not static
it get created if a class get created
Right, and static fields are created the first time that someone accesses a member on the type. E.g. do
var x = WhateverType.CrosshairOverlay
, and the static constructor will definitely be runmhh ok
and is there nothing else like static
like you can acces with the class (no new object)
but it get created when a class get created
(like in java)
Like what in java?
in java get the field called when the program start
what ever i see its not possible with c#
The JLS doesn't think so. It seems to have the same behaviour as C#: https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4.1
A class or interface type T will be initialized immediately before the first occurrence of any one of the following: - T is a class and an instance of T is created. - A static method declared by T is invoked. - A static field declared by T is assigned. - A static field declared by T is used and the field is not a constant variable (§4.12.4). - T is a top level class (§7.6) and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
thats wrong, even in java only if the class will be initialized (which might not be at program start) it executes that.
u could force it to load with a module initializer method: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute?view=net-7.0
ModuleInitializerAttribute Class (System.Runtime.CompilerServices)
Used to indicate to the compiler that a method should be called in its containing module's initializer.
Yes but thats already better too
I mean i could but its not really clesn
I mean, this design isn't really clean. It relies on a static constructor having side-effects which are visible outside of that type
well, yeah, one (small) thing would be adding a constraint to
T
for private static T Register<T>(string name, T renderer)
instead of blindly castingYea
Well
System.Runtime.CompilerServicesRuntimeHelpers.RunClassConstructor(typeof(YourClass).TypeHandle);
is also an option, you can couple that with reflection of the assembly.GetTypes() if you'd want (It runs the static constructor as well as initializing all static readonly
fields
I don't advise this reflection approach for SingleFile builds though
if you know that this wont mess up your code, you can do
nahh i just stay then by the not static one
?
ohh i understand
you mean that
yep
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.