.Net中var的使用方式

The following restrictions apply to implicitly-typed variable declarations:

  • var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.
  • var cannot be used on fields at class scope.
  • Variables declared by using var cannot be used in the initialization expression. In other words, this expression is legal: int i = (i = 20); but this expression produces a compile-time error: var i = (i = 20);
  • Multiple implicitly-typed variables cannot be initialized in the same statement.
  • If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration.

You may find that var can also be useful with query expressions in which the exact constructed type of the query variable is difficult to determine. This can occur with grouping and ordering operations.

The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code. One example where var is helpful in this manner is with nested generic types such as those used with group operations. In the following query, the type of the query variable is IEnumerable>. As long as you and others who must maintain your code understand this, there is no problem with using implicit typing for convenience and brevity.

However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.

Link: http://msdn.microsoft.com/en-us/library/bb384061.aspx

Handler the UnauthorizedAccessException in SharePoint

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool AccessDeniedflag = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
try
{
SPList list = web.GetList(web.ServerRelativeUrl+"/Lists/ListName");
SPListItem item = list.Items[0];
item["title"] = "ayman-elhattab.blogspot.com";
item.Update();
}
catch(UnauthorizedAccessException ex)
{
// Redirect to your custom page
SPUtility.Redirect("MyAccessDeniedPage.aspx",
    SPRedirectFlags.RelativeToLayoutsPage,this.Context);
}
finally
{
SPSecurity.CatchAccessDeniedException = AccessDeniedflag;
}

判断是否一个SharePoint Publishing页处于编辑模式

… 并且避免 ASP.NET 验证控件错误:

This page contains content or formatting that is not valid. You can find more information in the affected sections.

or

Input string was not in a correct format.

当创建一个WebPart来显示包含验证控件的表单时, ASP.NET 验证控件会阻止你签入和发布当前的Publishing页面.

解决方案:

  • Check if the web part is in edit or design mode and only add the validator if not:if (this.WebPartManager.DisplayMode != WebPartManager.EditDisplayMode &&
    this.WebPartManager.DisplayMode != WebPartManager.DesignDisplayMode)
    {
    // in display mode
    }

    This solution applies only if you have a single web part on the page, because you can only check if the current web part is in display/edit/design mode.

  • Another solution is using the EditModePanel. It allows you to add contols to the page in display or edit mode.
    For example:

    // add the EditBox to the page in edit mode
    EditModePanelpanel = newEditModePanel();
    TextBoxtextBox = newTextBox();

    panel.Controls.Add(textBox);
    panel.PageDisplayMode = PageDisplayMode.Edit;
    this.Controls.Add(panel);

    This solution didn’t work. Some replection shows me that the panel uses this function (which is obfuscated): public staticSPControlModeGetContextualFormModeFromPostedForm()
    This function comes from the internal class ConsoleUtilities.

  • The solution that worked for me was quite simple:

    // check if the form is in display mode
    bool inDisplayMode = SPContext.Current.FormContext.FormMode == SPControlMode.Display;

Well, good luck ;)

来源: http://blogs.microsoft.co.il/blogs/justguy/archive/2008/08/13/checking-if-a-publishing-page-is-in-edit-mode.aspx