Esempio n. 1
0
        public static DynamicMethod CopyToDynamicMethod([NotNull] MethodDefinition method)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            var typeMapper   = new TypeReferenceMapper();
            var methodMapper = new MethodReferenceMapper(typeMapper);

            var dynamicMethod = new DynamicMethod(
                method.Name,
                typeMapper.GetRuntimeType(method.ReturnType),
                method.Parameters.Select(param => typeMapper.GetRuntimeType(param.ParameterType)).ToArray(),
                true);

            dynamicMethod.InitLocals = method.Body.InitLocals;

            var ilGenerator = dynamicMethod.GetILGenerator();

            var copier = new ILCopier(ilGenerator, typeMapper, methodMapper);

            copier.CopyFrom(method);

            return(dynamicMethod);
        }
Esempio n. 2
0
 private ILCopier([NotNull] ILGenerator ilGenerator, [NotNull] TypeReferenceMapper typeMapper, [NotNull] MethodReferenceMapper methodMapper)
 {
     _ilGenerator  = ilGenerator ?? throw new ArgumentNullException(nameof(ilGenerator));
     _typeMapper   = typeMapper ?? throw new ArgumentNullException(nameof(typeMapper));
     _methodMapper = methodMapper ?? throw new ArgumentNullException(nameof(methodMapper));
 }
Esempio n. 3
0
 public MethodReferenceMapper([NotNull] TypeReferenceMapper typeMapper)
 {
     _typeMapper = typeMapper ?? throw new ArgumentNullException(nameof(typeMapper));
 }