I am in situation where i have ref parameters supplied by a event delegate. The delegate handler must modify those values and provide them to the caller. The problem is that in some cases the code must be executed under specific user so i need to use the WindowsIdentity.RunImpersonated
The RunImpersonated function accepts an anonymous delegate in form of Action or Func. The problem is that i cant pass any ref variables into this action or func. The purpose of sticking to ref is that i need to avoid constant value copy as event delegate might be called thousand of times per second.
I researched the subject but there seem to be no way to pass the parameters by ref into the RunImpersonated action.
Here is small example of the problem.
private void ByRef(ref int number)
{
var accessToken = WindowsIdentity.GetCurrent().AccessToken;
WindowsIdentity.RunImpersonated(accessToken, () =>
{
//this will not work
number = 200;
});
}