Find out if the reflected field is volatile

I'm trying to grab information from an assembly using reflection, and one of the things I would like to know (let me know you really need to know) is whether the field is volatile or not. In other words, if I have the following class

public class Test {
    public volatile int Counter = 0;
}

      

is it possible in some way (with reflection) to understand that the field is Test.Counter

really unstable? Or is it just not being exported at all?

+3


source to share


1 answer


You can use the method GetRequiredCustomModifiers

:



var field = typeof(Test).GetField("Counter");
bool isVolatile = field
    .GetRequiredCustomModifiers()
    .Any(x => x == typeof(IsVolatile));

      

+5


source







All Articles