Is there a tool to check the minimum version of the .NET framework?

I recently wrote a C # application that didn't run on some computers. The problem was that I was using a method that was only added in .NET SP1 .NET.

When the method was called, I would get a MethodNotFound exception, like in this problem: Exception only happens on my machine: Method not found: WaitHandle.WaitOne (Int32)

My question is, is there a tool that will check my code (statically?) And tell me the minimum .NET framework version I need (including service packs)?

It seems like it would be handy, although I'm not sure how common it is.

+3


source to share


2 answers


Everything written here is of course correct, but the original question hasn't been answered yet. In theory, you could write such a tool yourself, iterate through the code and see if you find a reference to a method that first existed in this Service Pack 1. Here's a crude example that Cecil uses to parse an assembly:

namespace SampleNamespace
{
  using System;
  using Mono.Cecil;
  using Mono.Cecil.Cil;

  internal static class Program
  {
    public static void Main(string[] args)
    {
      foreach (string arg in args)
      {
        Console.WriteLine("{0}: {1}", arg, NeedsDotNet35SP1(arg));
        Console.ReadLine();
      }
    }

    private static bool NeedsDotNet35SP1(string fileName)
    {
      return NeedsDotNet35SP1(ModuleDefinition.ReadModule(fileName));
    }

    private static bool NeedsDotNet35SP1(ModuleDefinition module)
    {
      if (TargetRuntime.Net_2_0 != module.Runtime)
      {
        // I don't care about .NET 1.0, 1.1. or 4.0 for this example.
        return false;
      }

      foreach (TypeDefinition type in module.Types)
      {
        if (NeedsDotNet35SP1(type))
        {
          return true;
        }
      }

      return false;
    }

    private static bool NeedsDotNet35SP1(TypeDefinition type)
    {
      foreach (MethodDefinition method in type.Methods)
      {
        if (NeedsDotNet35SP1(method))
        {
          return true;
        }
      }

      return false;
    }

    private static bool NeedsDotNet35SP1(MethodDefinition method)
    {
      return NeedsDotNet35SP1(method.Body);
    }

    private static bool NeedsDotNet35SP1(MethodBody body)
    {
      foreach (Instruction instruction in body.Instructions)
      {
        if (NeedsDotNet35SP1(instruction))
        {
          return true;
        }
      }

      return false;
    }

    private static bool NeedsDotNet35SP1(Instruction instruction)
    {
      if (OperandType.InlineMethod != instruction.OpCode.OperandType)
      {
        return false;
      }

      return NeedsDotNet35SP1((MethodReference)instruction.Operand);
    }

    private static bool NeedsDotNet35SP1(MethodReference method)
    {
      return method.FullName.Equals(
        "System.Boolean System.Threading.WaitHandle::WaitOne(System.Int32)",
        StringComparison.OrdinalIgnoreCase);
    }
  }
}

      



Obviously, this example only counts one method, but you should extend it if you really need to. :)

+1


source


I don't know of any tools for determining the minimum framework version your code should run. However, you can specify it yourself using requiredRuntime and the supported Runtime configuration parameters to declare which version of the framework your application requires or supports.



When loading the application, the CLR then raises an error and informs the user if the installed versions are not sufficient to run your code.

0


source







All Articles