【问题标题】:Invokemember method cannot call method under a public static classInvokemember 方法不能调用公共静态类下的方法
【发布时间】:2015-06-24 08:32:26
【问题描述】:

所以我正在使用这个从我的 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 方法。我想知道为什么它不再起作用了。

【问题讨论】:

标签: c# invokemember


【解决方案1】:

@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

【讨论】:

    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    相关资源
    最近更新 更多