obsidian/笔记文件/2.笔记/Type.InvokeMember 方法.md

91 lines
4.0 KiB
Markdown
Raw Permalink Normal View History

2025-03-26 00:02:56 +08:00
#unity/日常积累
## 定义
命名空间:
[System](https://learn.microsoft.com/zh-cn/dotnet/api/system?view=net-8.0)
程序集:
System.Runtime.dll
调用当前 [Type](https://learn.microsoft.com/zh-cn/dotnet/api/system.type?view=net-8.0) 的特定成员。
[](https://learn.microsoft.com/zh-cn/dotnet/api/system.type.invokemember?view=net-8.0#--)
## 重载
[InvokeMember(String, BindingFlags, Binder, Object, Object[])](https://learn.microsoft.com/zh-cn/dotnet/api/system.type.invokemember?view=net-8.0#system-type-invokemember(system-string-system-reflection-bindingflags-system-reflection-binder-system-object-system-object()))
使用指定的绑定约束并匹配指定的参数列表,调用指定成员。
[InvokeMember(String, BindingFlags, Binder, Object, Object[], CultureInfo)](https://learn.microsoft.com/zh-cn/dotnet/api/system.type.invokemember?view=net-8.0#system-type-invokemember(system-string-system-reflection-bindingflags-system-reflection-binder-system-object-system-object()-system-globalization-cultureinfo))
使用指定的绑定约束和匹配的指定参数列表及区域性来调用指定成员。
[InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])](https://learn.microsoft.com/zh-cn/dotnet/api/system.type.invokemember?view=net-8.0#system-type-invokemember(system-string-system-reflection-bindingflags-system-reflection-binder-system-object-system-object()-system-reflection-parametermodifier()-system-globalization-cultureinfo-system-string()))
当在派生类中重写时,使用指定的绑定约束并匹配指定的参数列表、修饰符和区域性,调用指定成员。
[](https://learn.microsoft.com/zh-cn/dotnet/api/system.type.invokemember?view=net-8.0#system-type-invokemember(system-string-system-reflection-bindingflags-system-reflection-binder-system-object-system-object()))
## InvokeMember(String, BindingFlags, Binder, Object, Object[])
使用指定的绑定约束并匹配指定的参数列表,调用指定成员。
``` cs
public object? InvokeMember (string name, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object? target, object?[]? args);
```
#### 参数
name
[String](https://learn.microsoft.com/zh-cn/dotnet/api/system.string?view=net-8.0)
字符串,它包含要调用的构造函数、方法、属性或字段成员的名称。
空字符串 (""),表示调用默认成员。
对于 `IDispatch` 成员,则为一个表示 DispID 的字符串,例如"[DispID=3]"。
invokeAttr
[BindingFlags](https://learn.microsoft.com/zh-cn/dotnet/api/system.reflection.bindingflags?view=net-8.0)
枚举值的按位组合,这些值指定如何进行搜索。 访问可以是 `BindingFlags` 之一,如 `Public``NonPublic``Private``InvokeMethod` 和 `GetField` 等。 查找类型无需指定。 如果省略查找的类型,则将使用 `BindingFlags.Public` | `BindingFlags.Instance` | `BindingFlags.Static`
binder
[Binder](https://learn.microsoft.com/zh-cn/dotnet/api/system.reflection.binder?view=net-8.0)
一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。
要使用 `Nothing` 的空引用(在 Visual Basic 中为 [DefaultBinder](https://learn.microsoft.com/zh-cn/dotnet/api/system.type.defaultbinder?view=net-8.0#system-type-defaultbinder))。 请注意,为了成功地使用变量参数来调用方法重载,可能必须显式定义 [Binder](https://learn.microsoft.com/zh-cn/dotnet/api/system.reflection.binder?view=net-8.0) 对象。
target
[Object](https://learn.microsoft.com/zh-cn/dotnet/api/system.object?view=net-8.0)
对其调用指定成员的对象。
args
[Object](https://learn.microsoft.com/zh-cn/dotnet/api/system.object?view=net-8.0)[]
包含传递给要调用的成员的参数的数组。
#### 返回
[Object](https://learn.microsoft.com/zh-cn/dotnet/api/system.object?view=net-8.0)
一个对象,表示被调用成员的返回值。