LogicalMethodInfo.Invoke(Object, Object[]) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
调用由当前的 LogicalMethodInfo 表示的方法。
public:
cli::array <System::Object ^> ^ Invoke(System::Object ^ target, cli::array <System::Object ^> ^ values);
public object[] Invoke (object target, object[] values);
member this.Invoke : obj * obj[] -> obj[]
Public Function Invoke (target As Object, values As Object()) As Object()
参数
- values
- Object[]
调用的方法的参数列表。 这是一个对象数组,其数量、顺序和类型与该方法的参数相同。 如果该方法不需要任何参数,则 values
参数应为 null
。
返回
Object[]
Object 类型的数组,它们表示被调用的方法的返回值和 out 参数。
例外
target
参数为 null
。
values
参数中参数的数量、类型和顺序与被调用的方法的签名不匹配。
调用方没有调用该方法的权限。
调用的方法引发异常。
示例
#using <System.Web.Services.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Web::Services::Protocols;
public ref class MyService
{
public:
int Add( int xValue, int yValue )
{
return (xValue + yValue);
}
};
int main()
{
Type^ myType = MyService::typeid;
MethodInfo^ myMethodInfo = myType->GetMethod( "Add" );
LogicalMethodInfo^ myLogicalMethodInfo = gcnew LogicalMethodInfo( myMethodInfo );
Console::WriteLine( "\nPrinting properties of method : {0}\n", myLogicalMethodInfo );
Console::WriteLine( "\nThe declaring type of the method {0} is :\n", myLogicalMethodInfo->Name );
Console::WriteLine( "\t {0}", myLogicalMethodInfo->DeclaringType );
Console::WriteLine( "\nThe parameters of the method {0} are :\n", myLogicalMethodInfo->Name );
array<ParameterInfo^>^myParameters = myLogicalMethodInfo->Parameters;
for ( int i = 0; i < myParameters->Length; i++ )
{
Console::WriteLine( "\t {0}", String::Concat( myParameters[ i ]->Name, " : ", myParameters[ i ]->ParameterType ) );
}
Console::WriteLine( "\nThe return type of the method {0} is :\n", myLogicalMethodInfo->Name );
Console::WriteLine( "\t {0}", myLogicalMethodInfo->ReturnType );
MyService^ service = gcnew MyService;
Console::WriteLine( "\nInvoking the method {0}\n", myLogicalMethodInfo->Name );
array<Object^>^values = gcnew array<Object^>(2);
values[ 0 ] = 10;
values[ 1 ] = 10;
Console::WriteLine( "\tThe sum of 10 and 10 is : {0}", myLogicalMethodInfo->Invoke( service, values ) );
}
using System;
using System.Reflection;
using System.Security.Permissions;
using System.Web.Services.Protocols;
public class MyService
{
public int Add(int xValue, int yValue)
{
return (xValue + yValue);
}
}
class LogicalMethodInfo_Constructor
{
[PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
static void Run()
{
Type myType = typeof(MyService);
MethodInfo myMethodInfo = myType.GetMethod("Add");
LogicalMethodInfo myLogicalMethodInfo =
new LogicalMethodInfo(myMethodInfo);
Console.WriteLine("\nPrinting properties of method : {0}\n",
myLogicalMethodInfo.ToString());
Console.WriteLine("\nThe declaring type of the method {0} is :\n",
myLogicalMethodInfo.Name);
Console.WriteLine("\t" + myLogicalMethodInfo.DeclaringType);
Console.WriteLine("\nThe parameters of the method {0} are :\n",
myLogicalMethodInfo.Name);
ParameterInfo[] myParameters = myLogicalMethodInfo.Parameters;
for(int i = 0; i < myParameters.Length; i++)
{
Console.WriteLine("\t" + myParameters[i].Name +
" : " + myParameters[i].ParameterType);
}
Console.WriteLine("\nThe return type of the method {0} is :\n",
myLogicalMethodInfo.Name);
Console.WriteLine("\t" + myLogicalMethodInfo.ReturnType);
MyService service = new MyService();
Console.WriteLine("\nInvoking the method {0}\n",
myLogicalMethodInfo.Name);
Console.WriteLine("\tThe sum of 10 and 10 is : {0}",
myLogicalMethodInfo.Invoke(service,
new object[] {10, 10}));
}
static void Main()
{
Run();
}
}
Imports System.Reflection
Imports System.Security.Permissions
Imports System.Web.Services.Protocols
Public Class MyService
Public Function Add(xValue As Integer, yValue As Integer) As Integer
Return xValue + yValue
End Function 'Add
End Class
Class LogicalMethodInfo_Constructor
<PermissionSetAttribute(SecurityAction.Demand, Name := "FullTrust")> _
Shared Sub Run()
Dim myType As Type = GetType(MyService)
Dim myMethodInfo As MethodInfo = myType.GetMethod("Add")
Dim myLogicalMethodInfo As New LogicalMethodInfo(myMethodInfo)
Console.WriteLine(ControlChars.NewLine + "Printing properties of method : {0}" + _
ControlChars.NewLine, myLogicalMethodInfo.ToString())
Console.WriteLine(ControlChars.NewLine + "The declaring type of the method {0} is :" + _
ControlChars.NewLine, myLogicalMethodInfo.Name)
Console.WriteLine(ControlChars.Tab + myLogicalMethodInfo.DeclaringType.ToString())
Console.WriteLine(ControlChars.NewLine + "The parameters of the method {0} are :" + _
ControlChars.NewLine, myLogicalMethodInfo.Name)
Dim myParameters As ParameterInfo() = myLogicalMethodInfo.Parameters
Dim i As Integer
For i = 0 To myParameters.Length - 1
Console.WriteLine(ControlChars.Tab + myParameters(i).Name + " : " + _
myParameters(i).ParameterType.ToString())
Next i
Console.WriteLine(ControlChars.NewLine + "The return type of the method {0} is :" + _
ControlChars.NewLine, myLogicalMethodInfo.Name)
Console.WriteLine(ControlChars.Tab + myLogicalMethodInfo.ReturnType.ToString())
Dim service As New MyService()
Console.WriteLine(ControlChars.NewLine + "Invoking the method {0}" + _
ControlChars.NewLine, myLogicalMethodInfo.Name)
Console.WriteLine(ControlChars.Tab + "The sum of 10 and 10 is : {0}", _
myLogicalMethodInfo.Invoke(service, New Object() {10, 10}))
End Sub
Shared Sub Main()
Run()
End Sub
End Class