Can the GetField () method of the PrivateObject class access a string constant string in C # 4.0?

I am writing a unit test that accesses a file in an isolated data warehouse. The file name is stored in a private string const inside the class, because I do not use it anywhere in the entire application.

However, when I run my unit test, I get a "Field not found" error when calling the GetField ("fieldName") method of an instance of the PrivateObject class.

string historyFileName = (string)history.GetField("ISOLATED_HISTORY_FILE");
+3
source share
3 answers

, . const . -, , . , , ISOLATED_HISTORY_FILE HistoryFile, .

0

, BindingFlags BindingFlags.NonPublic | BindingFlags.Instance.

. >


UPDATE

, . Const . static readonly BindingFlags.NonPublic | BindingFlags.Static.

,

+5

Use PrivateType, then call the GetStaticField method to get the private const value.

+2
source

All Articles