lundi 1 octobre 2018

how to check if method body has changed runtime c#

I have a requirement where i want to check if my method body is changed on run time. This is specifically for the test case using unit. I have steps in the test which i want to save in the database. But most of the time this steps will not change, hence don't want to overload my database by saving this step into the database every time it runs. I only want to save the new steps if new steps are added or modified.

e.g.

[Test]
 public void Should_Pass_When_SomethingHappened()
      {
        File.Open(TestFile);      //step 1
        File.Close()   //step2 
     }

Now if someone change this method to say below

[Test]
 public void Should_Pass_When_SomethingHappened()
      {
        File.Open(TestFile); //step1 
        File.Copy(Source, Desitnation); //step2      
        File.Close(); //step 3
     }

I want to capture this change in the test while running the test. So that i can mark in database that this test case is change save new steps in the database other keep original steps

I tried this.

public int CheckIfMethodIfModified()
{
   Type TestType = typeof(MyTypeName);
   MethodInfo myMethodInfo = 
   TestType.GetMethod("Should_Pass_When_SomethingHappened");
   MethodBody mb = myMethodInfo.GetMethodBody();
   string result = System.Text.Encoding.UTF8.GetString(mb.GetILAsByteArray());
   result.GetHashCode();
}

This Hashcode is not changing even if my method is changed.





Aucun commentaire:

Enregistrer un commentaire