0

所以我使用这个从我的dll中调用一个方法。

string sp = "dynamicmethodname";

Type TypeObj = typeof(DLLclass);
Object MyObj = Activator.CreateInstance(TypeObj);
TypeObj.InvokeMember(sp, BindingFlags.InvokeMethod | BindingFlags.Default, null, MyObj, new Object[] { gp });  

如果我的方法就在我的公共课程之下,它将起作用。但是当我试图做这样的事情时。

public class Test {
   public static class Met1{
     public static void _Validate(string gp){
      functions here.....
    }
  }
}

invokemember 方法将不再到达我的 _Validate 方法。我想知道为什么它不再起作用了。

4

1 回答 1

0

@Charleh 所写内容的完整示例:

public class Test
{
    public static class Met1
    {
        public static void _Validate(string gp)
        {
            Console.WriteLine(gp);
        }
    }
}

MethodInfo method = typeof(Test.Met1).GetMethod("_Validate");
// Or 
// MethodInfo method = typeof(Test.Met1).GetMethod("_Validate", BindingFlags.Static | BindingFlags.Public);
// or
// MethodInfo method = typeof(Test.Met1).GetMethod("_Validate", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string) }, null);

method.Invoke(null, new object[] { "Hello world " });

编译:https ://ideone.com/JIoHar

于 2015-06-24T09:32:44.920 回答