표준 C# 액세스 규칙을 사용하여 액세스 할 수없는 메서드를 호출하는 호출을 작성하면 액세스 할 수없는 메서드가 호출되지 않고 런타임에서 TryInvokeMember
(호출을 처리 할 수있는 곳)이 호출됩니다. 다른 방법으로). 이제
class Test : DynamicObject {
public void Foo() {
Console.WriteLine("Foo called");
}
protected void Bar() {
Console.WriteLine("Bar called");
}
public override bool TryInvokeMember
(InvokeMemberBinder binder, object[] args, out object result) {
Console.WriteLine("Calling: " + binder.Name);
return base.TryInvokeMember(binder, args, out result);
}
}
, 우리가 개체의 인스턴스를 만들 수 있습니다 및 그 방법 중 몇 가지를 호출하려고 : 당신이를 호출하는 경우, 그래서
dynamic d = new Test();
d.Foo(); // this will call 'Foo' directly (without calling 'TryInvokeMember')
d.Bar(); // this will call 'TryInvokeMember' and then throw exception
을 당신이 그것을 시도 할 수 있도록 다음은 그 예이다 base
구현이 TryInvokeMember
이면 액세스 할 수없는 메서드를 호출 할 때 C# 동적 바인더가 실패하지만 result
을 설정하고 true
을 반환하여 TryInvokeMember
에 사용자가 직접 처리를 정의 할 수 있습니다.
이 시나리오에서는 내가 할 수있는 첫 번째 일은 시도해보아야합니다. – spronkey