Using reflection to find constants

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Is there a way using reflection to interrogate a .dll and identify all the
declared constants, as well as their initial values?

Thanks in advance.

Mark
 
Is there a way using reflection to interrogate a .dll and identify all the
declared constants, as well as their initial values?

Yes.

- Load the assembly (Assembly.Load[From])
- Get all types (Assembly.GetTypes)
- Get all static fields in each type (Type.GetFields)
- Check if it's a const (FieldInfo.Attributes has Literal set)
- If so retrieve the value (FieldInfo.GetValue)



Mattias
 
Mark,

You should be able to get all of the static fields on a type, and then
cycle through them, checking the IsLiteral property on the FieldInfo
instance representing the field. If IsLiteral returns true, then it is a
constant. To get the value, you can just call GetValue on the FieldInfo
instance, passing null for the instance, and it should return the value of
the constant.

Hope this helps.
 
Back
Top