【问题标题】:Invoke method overloads调用方法重载
【发布时间】:2022-07-18 21:15:04
【问题描述】:

我在这个microsoft page上阅读了以下代码

Type t = typeof(String);

        MethodInfo substr = t.GetMethod("Substring", 
            new Type[] { typeof(int), typeof(int) });

        Object result = 
            substr.Invoke("Hello, World!", new Object[] { 7, 5 });
        Console.WriteLine("{0} returned \"{1}\".", substr, result);

问题是 MethodInfo 类中没有 Invoke(String,Object[]) 方法。 (t.GetMethod 返回一个 MethodInfo 对象对吧?)

还有另一个问题:有时我看到 Object(class/datatype) 变量被声明为对象,有时被声明为 Object。有什么区别吗?

【问题讨论】:

标签: c#


【解决方案1】:
  1. “invoke”的第一个参数是(t)类型的“instance”。在这个示例中,它是一个“字符串”,但在另一个示例中,它可能是 int。这是关于你的起点。签名是“对象”,因为您可以(几乎)将所有内容作为“对象”传递。

  2. 它们完全一样。 "object" 是 .NETs "Object" 类型的 C# 别名。

【讨论】:

    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多