简体   繁体   English

C#中没有参数类型的方法参数

[英]Method Parameter Without A Parameter Type In C#

I try to use trial version of third party software written C#. 我尝试使用C#编写的第三方软件的试用版。 While I try to use its API, I face up with a strange problem. 当我尝试使用它的API时,我遇到了一个奇怪的问题。

I am using visual studio 2015 and I found an interesting extended method signature in their API-SDK 我正在使用visual studio 2015,我在他们的API-SDK中发现了一个有趣的扩展方法签名

  public static class CallExtendedInfoEx
  {
        public static void AddToSIPMessage(this CallExtendedInfo info,  msg);
  }

The second parameter type is not exist .Never seen before in C#. 第二个参数类型不存在 。以前在C#中见过。 I try to give a generic Object parameter as second parameter, and C# Compiler gives a strange error 我尝试将通用的Object参数作为第二个参数,并且C#Compiler给出了一个奇怪的错误

Severity Code Description Project File Line 严重性代码描述项目文件行

Error CS1503 Argument 2: cannot convert from 'object' to '.' 错误CS1503参数2:无法从'object'转换为'。'

It works with "null"...No compiler error or run time error [ ignored i think ] 它使用“null”...没有编译器错误或运行时错误[忽略我认为]

How those guys can able to write such a code in C#? 这些人怎么能用C#编写这样的代码? Is It Posssible? 它可以吗? Any idea? 任何想法?

Note : I suspect from a code obscurification problem...But still i do not understand what is going here 注意:我怀疑代码存在问题...但我仍然不明白这里发生了什么

  IL_0073:  call       void [CallExtendedInfoEx::AddToSIPMessage(class [VoIP.CallExtendedInfo,
                                                                                  class [VoIPSDK]''.'')

Well, looking at the ILDASM output, it's rather obvious that the argument has a perfectly valid type. 好吧,看看ILDASM输出,很明显,该参数具有完全有效的类型。 Sure, it's a type that you can't actually name in C#, but it's perfectly valid in CLR at large. 当然,它是一种你无法在C#中实际命名的类型,但它在CLR中完全有效。

The fact that you can pass null shouldn't be surprising - the argument is a reference type (or convertible from null ), so null is a perfectly valid value. 您可以传递null的事实不应该令人惊讶 - 参数是引用类型(或可以从null转换),因此null是一个完全有效的值。 On the other hand, expecting passing an object instance is entirely wrong - you can only pass more derived types as argument, not less . 另一方面,期望传递object实例是完全错误的 - 您只能传递更多派生类型作为参数,而不是更少 It's fine to pass string (more specific) instead of object (less specific), but not the other way around. 传递string (更具体)而不是object (不太具体)是可以的,但不是相反。

Most likely, this is either a method you shouldn't touch (ie not part of the public contract of the API/SDK), or you're expected to get instances of the argument from some other method - never using the explicit type anywhere. 最有可能的是,这是一个你不应该触摸的方法(即不是API / SDK的公共合同的一部分),或者你应该从其他方法获取参数的实例 - 从不在任何地方使用显式类型。

This is not valid C# code. 这不是有效的C#代码。 Some decompiler probably gave you that code, or the documentation where you copied this out of is faulty. 一些反编译器可能会给你那些代码,或者你复制它的文档是错误的。

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

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