我不是很喜欢C#(我来自Java)并且我对以下与此示例相关的委托方法的确切工作方式存在以下疑问:
List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);
foreach (string currentUrl in urlList)
{
Debug.Print("Current url: " + currentUrl);
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
using (SPSite oSiteCollection = new SPSite(currentUrl))
{
using (SPWeb oWebsite = oSiteCollection.OpenWeb())
{
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解阅读官方文档:https: //docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/
的委托()用于传递的方法作为另一种方法的输入参数.
例如,如果我有类似的东西:
public delegate int MyDelegate (string s);
Run Code Online (Sandbox Code Playgroud)
它表示对具有此方法签名的任何方法的引用(返回类型,方法名称,在put参数中).
如果它是正确的,究竟是什么意味着我的第一个原始例子?为什么我有一个使用(...){...}块的方法签名?
这种语法的确切含义是什么?
在delegate () { }刚刚指示匿名联方法/委托传递给函数.该方法的主体就像任何C#代码块一样,并且可以包含using语句或任何其他语句.
这类似于:
private void Method()
{
using (SPSite oSiteCollection = new SPSite(currentUrl))
{
using (SPWeb oWebsite = oSiteCollection.OpenWeb())
{
}
}
});
SPSecurity.RunWithElevatedPrivileges(Method);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |