Hi Jb,

Sorry for the very late reply, as I was busy with other projects...

I didn't see what u mentioned below when it is assigned in the constructor, only see it in methods.
Can u double confirm it by testing it out?

Then I realised it may be because I didn't visit constructors... But even when I visit constructor n try to print out from the VisitConstructor, there is no difference in the output. Since ctor is actually a mtd, shldn't it work even I don't visit constructor?

public virtual void VisitConstructorCollection (ConstructorCollection ctors)
       {
           foreach (MethodDefinition item in ctors)
           {
Console.WriteLine("in ctor of ctors"); //I direct all my Console.WriteLine() to a file.
               item.Accept(this);
           }
       }
       public virtual void VisitConstructor(MethodDefinition ctor)
       {
           if (ctor.Body != null)
           {
               Console.WriteLine("in ctor");
               ASTCodeVisitor astHelperVisitor = new ASTCodeVisitor();
astHelperVisitor.CurrMethod = ctor.DeclaringType.FullName + "." + ctor.Name;
               ctor.Body.Accept(astHelperVisitor);
           }
       }


Below is the program I created separately to test this:
The output shows instructions can detect pi2 being assigned in the method MethodA but not pi1 in the constructor Test.

namespace TestTest
{
   class Test
   {
       public static double pi1;
       public static double pi2;

       public Test()
       {
           pi1 = Math.PI;
       }

       public virtual void MethodA()
       {
           pi2 = Math.PI;
       }

       static void Main(string[] args)
       {
       }
   }
}

Here is the output (btn "###" are the instructions detected):

----------------------[assembly] TestTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null----------------------
[type_decl] Test
        [extends_fr] Object
        [method_decl] MethodA
                [modifiers] public virtual
###nop ###
###ldc.r8 3.14159265358979###
###stsfld System.Double TestTest.Test::pi2###
                [field(currAsm)_used] pi2 (stsfld)  (is written)
###ret ###
                [return] Void
        [method_decl] Main
                [modifiers] private static
###nop ###
###ret ###
                [return] Void
                [parameter] args
                        [type] String[]
        [field_decl] pi1
                [modifiers] public static
                [type] Double
        [field_decl] pi2
                [modifiers] public static
                [type] Double


Thanks,
LY



--------------------------------------------------
From: "Jb Evain" <[email protected]>
Sent: Friday, January 15, 2010 8:32 PM
To: <[email protected]>
Subject: Re: [mono-cecil] Field read and write

Hey,

On Fri, Jan 15, 2010 at 11:33 AM, Wee Li Yen <[email protected]> wrote:
Field declaration eg: "public double pi = Math.PI;"

But I can only detect this field when I assign this value to the field in a
method.
If I put it as a field declaration or assign this value to the field in a
ctr, I won't be able to see the above instr.

You will, it will be assigned in the constructor of the type. You'll
see something like:

ldc.r8 3.14....
stfld pi

--
Jb Evain  <[email protected]>




--
--
mono-cecil

--
--
mono-cecil

Reply via email to