RunWithElevatedPrivileges — with/without delegate
-
07-10-2020 - |
Question
Here two methods below that uses SPSecurity.RunWithElevatedPrivileges()
. I just want to know the difference between two?
Method 1:
public void Method1(SPUser user)
{
if (RequireImpersonation == true)
SPSecurity.RunWithElevatedPrivileges(delegate() { PerformRemoveFromVisitorsGroup(user); });
else
PerformRemoveFromVisitorsGroup(user);
}
Mehtod 2:
public void Method2(SPUser user)
{
if (RequireImpersonation == true)
SPSecurity.RunWithElevatedPrivileges(() => PerformRemoveFromVisitorsGroup(user));
else
PerformRemoveFromVisitorsGroup(user);
}
La solution
Once the code is compiled, there is no difference between these two. In this case - delegate { }
is equal to () => { }
. There are a couple of things, but they are not relevant in this case:
- If you assign the lambda to a delegate type (such as Func or Action) you'll get an anonymous delegate. [1]
- If you assign the lambda to an Expression type, you'll get an expression tree instead of a anonymous delegate. The expression tree can then be compiled to an anonymous delegate. [1]
[1] - http://weblogs.asp.net/scottgu/new-orcas-language-feature-lambda-expressions
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange