Last post Apr 04, 2020 01:20 AM by bruce (sqlwork.com)
Apr 03, 2020 07:59 PM|TechAspirant|LINK
I am getting exception in my Asp.Net core 3.1 application as below
System.FieldAccessException: Cannot set initonly static field 'Field' after type 'TestReadOnly' is initialized.
Test.GetType().GetField("ImmediateTest", BindingFlags.NonPublic | BindingFlags.Static)
Apr 03, 2020 08:16 PM|bruce (sqlwork.com)|LINK
the error is pretty clear, you can not set the value of an initonly static field even with reflection. there was a bug in early version of the runtime, where reflection would work.
public static class MyClass
private static readonly string foo;
foo = "hi"; // only way to set - reflection no longer allowed
Apr 03, 2020 08:20 PM|TechAspirant|LINK
Means SetValues is the problem here. Can You show me How my code will look like after changes ? Do I have to create class and I have to call class to set value.
Apr 04, 2020 01:20 AM|bruce (sqlwork.com)|LINK
no, you need to change the "ImmediateTest" field to not be a readonly.