gsirianni gsirianni - 24 days ago 17
C# Question

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

I'm writing a unit test that accesses a file in isolated data storage. The file name is stored in a private const string within the class because I don't use it anywhere else in the whole application.

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

string historyFileName = (string)history.GetField("ISOLATED_HISTORY_FILE");

Answer

I found the problem for any one who may have the same issue. I changed the filed from private const string to private string. Apparently, reflection doesn't recognize fields marked with const. It isn't the name I gave the filed because I tried the test with ISOLATED_HISTORY_FILE and isolatedHistoryFile and it worked both ways.

Comments