Share via


No LocalVariableInfo.Name?

This was
one ladybug in MSDN product feedback center. First of all, thanks to those
who filed bugs or suggestions through it, which really help improve our product
quality.

No Name property in the
LocalVariableInfo class is "by design" (possibly forever). Inside a .Net
assembly, there is no metadata (or table) keeping this information. However, sometimes
when using ildasm to view the method, we do see those local variable names, such
as

.locals init ([0] int32 m_int32,

       [1] object m_object,

       [2] int32[] m_array,

       [3] int32[] CS$0$0000)

Those names are from the pdb file, not embedded in the exe or dll. If we move the
pdb to other directory, and open the ildasm again, we will see those variable names
become to V_0, V_1...

.locals init (int32 V_0,

       object V_1,

       int32[] V_2,

       int32[] V_3)

If you *really* need those names in your managed application, you may want to take
a look at
Mdbg source codes; but here are the general steps to get it:

  1. Get IMetaDataImport, say import (I found examples
    here or here);
  2. Call Marshal.GetComInterfaceForObject(import, typeof(IMetaDataImport)) to
    get the raw interface IntPtr;
  3. Call new SymBinder().GetReader() with that IntPtr to get ISymbolReader
  4. Call ISymbolReader.GetMethod() on the SymbolToken, which is constructed based on
    the MetadataToken of the method you want to get local variable names, and get back
    ISymMethod
  5. Then play with ISymMethod.RootScope, ISymbolScope.GetChildren(), ISymbolScope.GetLocals();

Yes, there is ISymbolVariable.Name.