Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
How can I change field value with Mono Cecil?

I have a C# program and it has a class:

public class Test
{
    internal const string a = "some value";
    private DateTime b = new DateTime();
}

How can I use Mono Cecil to change their initial value so that it looks like this:

public class Test
{
    internal const string a = "test";
    private DateTime b = DateTime.MaxValue;
}

Right now I only have the following skeleton code and I don't know how to modify the fields.

void Main()
{
    var input = @"C:\my_projects\a.exe";
    var asm = AssemblyDefinition.ReadAssembly(input);
    foreach (ModuleDefinition module in asm.Modules)
    {
        foreach (TypeDefinition type in module.GetTypes())
        {

            foreach (var field in type.Fields)
            {
                if (field.Name == "a")
                {

                }

                else if (field.Name == "b")
                {

                }

            }
        }
    }
    asm.Write(@"c:\my_projects\b.exe");
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Disclaimer

Very brittle code ahead

For the constant it is a matter of setting fld.Constant property.

For instance/static fields the C# compiler will emit the initialization code in the constructor, so you'll need to find the instruction that is loading the value that will be stored in the field and replace it.

using System.IO;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;

namespace x
{
    class Program
    {
        public const string ConstValue = "old";
        public int instanceField = 1;

        static void Main(string[] args)
        {
            var o = new Program();
            if (o.instanceField == 1)
            {
                using var a = AssemblyDefinition.ReadAssembly(typeof(Program).Assembly.Location);
                var t = a.MainModule.Types.Single(ct => ct.Name == "Program");

                var constant = t.Fields.First(f => f.Name == "ConstValue");
                constant.Constant = "new value";

                var ctor = t.Methods.Single(m => m.IsConstructor);
                System.Console.WriteLine(ctor);
                var il = ctor.Body.GetILProcessor();

                var inst = il.Body.Instructions.First();
                while (inst != null)
                {
                    System.Console.WriteLine($"Checking {inst}");
                    if (inst.OpCode == OpCodes.Stfld && ((FieldReference) inst.Operand).Name == "instanceField")
                    {
                        // notice that this is very fragile. For this specific
                        // case I'd say it is safe but depending on how you
                        // initialize the field the instruction that loads 
                        // the value to be assigned to the field may be located 
                        // several instructions prior to the actual assignment
                        // but you can keep track of the stack state and figure 
                        // out which instruction is pushing the value that
                        // will end up being poped into the field.
                        
                        il.Replace(inst.Previous, il.Create(OpCodes.Ldc_I4, 42));                        
                        System.Console.WriteLine("Found");
                        break;
                    }
                    inst = inst.Next;
                }

                var  p = typeof(Program).Assembly.Location;
                var newBinaryPath =  Path.Combine(Path.GetDirectoryName(p), Path.GetFileNameWithoutExtension(p) + "2" +Path.GetExtension(p));
                a.Write(newBinaryPath);
                System.Console.WriteLine($"New binary writen to {newBinaryPath}");
            }

            System.Console.WriteLine(o.instanceField);

            // Notice that no matter what you do, this will always print the 
            // old value; that happens because the C# compiler will emit the
            // constant at the call site (instead of referencing the field
            // at runtime)
            System.Console.WriteLine(ConstValue);
        }
    }
}

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!