简体   繁体   English

调用方法重载

[英]Invoke method overloads

I read the following code on this 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);

The problem is that there is not an Invoke(String,Object[]) method in MethodInfo class.问题是 MethodInfo 类中没有 Invoke(String,Object[]) 方法。 (t.GetMethod returns a MethodInfo object right?) (t.GetMethod 返回一个 MethodInfo 对象,对吗?)

Also another question: sometimes I see Object(class/datatype) variables declared as object and sometimes declared as Object.还有另一个问题:有时我看到声明为对象的对象(类/数据类型)变量有时声明为对象。 Is there any difference?有什么区别吗?

  1. The first argument of "invoke" is "instance" of type (t). "invoke" 的第一个参数是 (t) 类型的 "instance"。 In this sample, it is a "string," but in another sample, it might be int.在这个示例中,它是一个“字符串”,但在另一个示例中,它可能是 int。 It is about your starting point.这是关于你的起点。 The signature is "object" because you can pass everything (almost) as "object".签名是“对象”,因为您可以(几乎)将所有内容作为“对象”传递。

  2. They are identical.它们是相同的。 "object" is C# alias of .NETs "Object" type. “object”是 .NETs“Object”类型的 C# 别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM