예제 #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);
        }
예제 #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));
 }