当我尝试从MethodBody获取指令时,为什么会出现InvalidCastException? (Mono.Cecil能做到)

时间:2016-12-29 17:53:15

标签: c# mono mono.cecil

我正在使用此代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
char *stringToBinary(char *s)
{
  if (s == NULL) {
    // NULL might be 0 but you cannot be sure about it
    return NULL;
  }
  // get length of string without NUL
  size_t slen = strlen(s);

  // we cannot do that here, why?
  // if(slen == 0){ return s;}

  errno = 0;
  // allocate "slen" (number of characters in string without NUL)
  // times the number of bits in a "char" plus one byte for the NUL
  // at the end of the return value
  char *binary = malloc(slen * CHAR_BIT + 1);
  if(binary == NULL){
     fprintf(stderr,"malloc has failed in stringToBinary(%s): %s\n",s, strerror(errno));
     return NULL;
  }
  // finally we can put our shortcut from above here
  if (slen == 0) {
    *binary = '\0';
    return binary;
  }
  char *ptr;
  // keep an eye on the beginning
  char *start = binary;
  int i;

  // loop over the input-characters
  for (ptr = s; *ptr != '\0'; ptr++) {
    /* perform bitwise AND for every bit of the character */
    // loop over the input-character bits
    for (i = CHAR_BIT - 1; i >= 0; i--, binary++) {
      *binary = (*ptr & 1 << i) ? '1' : '0';
    }
  }
  // finalize return value
  *binary = '\0';
  // reset pointer to beginning
  binary = start;
  return binary;
}


int main(int argc, char **argv)
{
  char *output;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s string\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  // TODO: check argv[1]
  output = stringToBinary(argv[1]);
  printf("%s\n", output);

  free(output);
  exit(EXIT_SUCCESS);
}

我每次都会收到InvalidCastException methodDefinition.Body.Instructions是 Mono.Collections.Generic.Collection
如何解决?

堆栈追踪:http://image.prntscr.com/image/e20e4b00143140a79497b71f0906119e.png

我试图调试它很长时间了。 似乎当我将MethodDefinition传递给函数时 - 它不是null。 但是当函数的第一行执行时,由于某种原因它的设置为null。什么?..

0 个答案:

没有答案