📄 extendercontrolbasedesigner.pagemethodsignatures.cs
字号:
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.Reflection;
using System.Web.UI.Design;
using System.Web.Services;
using System.Web.Script.Services;
using System.Windows.Forms;
namespace AjaxControlToolkit.Design
{
/// <summary>
/// Designer support for adding and navigating to page methods
/// </summary>
public partial class ExtenderControlBaseDesigner<T> : ExtenderControlDesigner, System.ComponentModel.IExtenderProvider
where T : ExtenderControlBase
{
/// <summary>
/// Action lists including the PageMethodDesignerActionList
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
public override DesignerActionListCollection ActionLists
{
get
{
// Add the page methods
if (_actionLists == null)
{
// Start with the original list
_actionLists = new DesignerActionListCollection();
_actionLists.AddRange(base.ActionLists);
// Look for delegates on the designer with PageMethodSignature attributes
foreach (Type delegateType in GetType().GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
{
if (!delegateType.IsSubclassOf(typeof(Delegate)))
{
continue;
}
PageMethodSignatureAttribute attribute = Attribute.GetCustomAttribute(delegateType, typeof(PageMethodSignatureAttribute)) as PageMethodSignatureAttribute;
if (attribute == null)
{
continue;
}
MethodInfo signature = delegateType.GetMethod("Invoke");
if (signature == null)
{
continue;
}
// Add the page method
_actionLists.Add(new PageMethodDesignerActionList(Component, signature, attribute));
}
}
return _actionLists;
}
}
private DesignerActionListCollection _actionLists;
/// <summary>
/// Action list for a page method
/// </summary>
private class PageMethodDesignerActionList : DesignerActionList
{
/// <summary>
/// Method signature of the page method
/// </summary>
private MethodInfo _signature;
/// <summary>
/// Attribute describing properties of the page method
/// </summary>
private PageMethodSignatureAttribute _attribute;
/// <summary>
/// Constructor
/// </summary>
/// <param name="component">Component the action list is associated with</param>
/// <param name="signature">Method signature of the page method</param>
/// <param name="attribute">Attribute describing properties of the page method</param>
public PageMethodDesignerActionList(IComponent component, MethodInfo signature, PageMethodSignatureAttribute attribute)
: base(component)
{
_signature = signature;
_attribute = attribute;
}
/// <summary>
/// Add the page method
/// </summary>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
public override DesignerActionItemCollection GetSortedActionItems()
{
DesignerActionItemCollection items = new DesignerActionItemCollection();
// Don't add the item if there's a service path specified or we can't get the event binding service
PropertyDescriptor pagePathProperty = TypeDescriptor.GetProperties(Component)[_attribute.ServicePathProperty];
if ((pagePathProperty == null || (pagePathProperty != null && string.IsNullOrEmpty(pagePathProperty.GetValue(Component) as string))) && (GetService(typeof(IEventBindingService)) != null))
{
string name = string.Format(CultureInfo.CurrentCulture, "Add {0} page method", _attribute.FriendlyName);
items.Add(new DesignerActionMethodItem(this, "AddPageMethod", name, "Page Methods", name, true));
}
return items;
}
/// <summary>
/// Add the code for the page method
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Reporting all exceptions as errors")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Method is neccesarily complex")]
private void AddPageMethod()
{
try
{
// Reset to the default name of the page method
string name = _signature.DeclaringType.Name;
// Ensure we don't have a ServicePath set
PropertyDescriptor pagePathProperty = TypeDescriptor.GetProperties(Component)[_attribute.ServicePathProperty];
if (pagePathProperty != null)
{
string servicePath = pagePathProperty.GetValue(Component) as string;
if (!string.IsNullOrEmpty(servicePath))
{
ShowMessage(string.Format(CultureInfo.CurrentCulture, "Cannot create page method \"{0}\" because the extender is using web service \"{1}\" instead!", name, servicePath));
return;
}
}
// Optionally set UseContextKey to true
if (_attribute.IncludeContextParameter)
{
PropertyDescriptor useContextKeyProperty = TypeDescriptor.GetProperties(Component)[_attribute.UseContextKeyProperty];
if (useContextKeyProperty != null)
{
useContextKeyProperty.SetValue(Component, true);
}
}
// Open the code for the page
IEventBindingService bindingService;
if (!EnsureService(out bindingService))
{
return;
}
bindingService.ShowCode();
// Load the automation service
object _dte = GetService(ReferencedAssemblies.EnvDTE.GetType("EnvDTE._DTE"));
if (_dte == null)
{
ShowMessage(string.Format(CultureInfo.CurrentCulture, "Cannot create page method \"{0}\" because {1} could not be acquired!", _signature.DeclaringType.Name, "EnvDTE._DTE"));
return;
}
DTE2 automation = new DTE2(_dte);
try
{
// Get the CodeModel for the file
FileCodeModel2 fileCodeModel = LoadFileCodeModel(automation.ActiveDocument.ProjectItem);
if (fileCodeModel == null || fileCodeModel.Reference == null)
{
ShowMessage(string.Format(CultureInfo.CurrentCulture, "Cannot create page method \"{0}\" because no CodeBehind or CodeFile file was found!", name));
return;
}
// Get the class for the page
IDesignerHost host;
if (!EnsureService(out host))
{
return;
}
CodeClass2 classModel = FindClass(fileCodeModel, host.RootComponentClassName);
if (classModel == null || classModel.Reference == null)
{
ShowMessage(string.Format(CultureInfo.CurrentCulture, "Cannot create page method \"{0}\" because no CodeBehind or CodeFile file was found!", name));
return;
}
// Either set the ServiceMethod to the default name, or use an existing value to look it up
PropertyDescriptor pageMethodProperty = TypeDescriptor.GetProperties(Component)[_attribute.ServiceMethodProperty];
if (pageMethodProperty != null)
{
string value = pageMethodProperty.GetValue(Component) as string;
if (!string.IsNullOrEmpty(value))
{
// Use the existing value as the name
name = value;
}
else
{
// Ensure we get a unique name when use the default value
string defaultName = name;
int i = 2;
while (FindMethod(classModel, name, _signature) != null)
{
name = defaultName + i++;
}
// Set the value to the default name
pageMethodProperty.SetValue(Component, name);
}
}
// Get the UndoContext for the currently open document
// (since creating a DesignerTransaction will refer to the aspx page,
// not the code behind that we opened)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -