Background and Motivation
Inspired by a pair of comments between @GrabYourPitchforks and @jkotas at #4556 (comment)
Say that I have a Predicate<string>, but I need a Func<string, bool>. There's one kinda-hokey workaround right now:
Predicate<string>del1=GetDelegate();Func<string,bool>del2=del1.Invoke;// points to Predicate<string>.Invoke's MethodDesc
But this isn't ideal since it's a hidden recursive operation: every invocation incurs an extra indirection.
Since Jan said this should be a separate API, here we are.
Proposed API
namespaceSystem{publicabstractpartialclassDelegate{[return:NotNullIfNotNull("del")]publicstaticTDelegate?ChangeType<TDelegate>(Delegate?del)whereTDelegate:Delegate;}}This method is Null-in-Null-out. Exception behavior may be as follows:
T is an incompatible delegate type (InvalidCastException?)
Usage Examples
Predicate<string>del1=GetDelegate();Func<string,bool>del2=Delegate.ChangeType<Func<string,bool>>(del1);// points to whatever MethodDesc del1 pointed to
Alternative Designs
¯\_(ツ)_/¯
Risks
None that I can think of.
History
- Removed Try-pattern methods
- Removed non-generic method
Background and Motivation
Inspired by a pair of comments between @GrabYourPitchforks and @jkotas at #4556 (comment)
Since Jan said this should be a separate API, here we are.
Proposed API
This method is Null-in-Null-out. Exception behavior may be as follows:
Tis an incompatible delegate type (InvalidCastException?)Usage Examples
Alternative Designs
¯\_(ツ)_/¯
Risks
None that I can think of.
History