📄 extendercontrolbasedesigner.pagemethodsignatures.cs
字号:
/// Convert a type's name into a valid CodeTypeRef name
/// </summary>
/// <param name="t">Type</param>
/// <returns>Name</returns>
private static string CreateCodeTypeRefName(Type t)
{
return t.FullName.Replace('+', '.');
}
/// <summary>
/// Determine whether a CodeTypeRef is describing a particular type
/// </summary>
/// <param name="modelType">CodeTypeRef</param>
/// <param name="type">Type</param>
/// <returns>True if they're describing the same type, false otherwise</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
private static bool AreSameType(CodeTypeRef modelType, Type type)
{
if (modelType == null || modelType.Reference == null)
{
return type == null;
}
// If we have an array, verify its rank and element type matches
else if (modelType.TypeKind == vsCMTypeRef.vsCMTypeRefArray)
{
if (!type.IsArray)
{
return false;
}
if (modelType.Rank != type.GetArrayRank())
{
return false;
}
return AreSameType(modelType.ElementType, type.GetElementType());
}
// If we have a reference type, verify the reference matches
else if (modelType.TypeKind == vsCMTypeRef.vsCMTypeRefPointer)
{
if (!type.IsPointer)
{
return false;
}
return AreSameType(modelType.ElementType, type.GetElementType());
}
// Match simple types based on their names
else
{
return string.CompareOrdinal(modelType.AsFullName, CreateCodeTypeRefName(type)) == 0;
}
}
/// <summary>
/// Create the page method
/// </summary>
/// <param name="classModel">Class to contain the method</param>
/// <param name="name">Name of the method</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
private void CreatePageMethod(CodeClass2 classModel, string name)
{
ParameterInfo[] parameters = _signature.GetParameters();
CodeFunction2 method = classModel.AddFunction(name, vsCMFunction.vsCMFunctionFunction, CreateCodeTypeRefName(_signature.ReturnType), -1, vsCMAccess.vsCMAccessPublic, null);
method.IsShared = true;
foreach (ParameterInfo param in parameters)
{
method.AddParameter(param.Name, CreateCodeTypeRefName(param.ParameterType), -1);
}
method.AddAttribute(typeof(WebMethodAttribute).FullName, "", -1);
method.AddAttribute(typeof(ScriptMethodAttribute).FullName, "", -1);
}
/// <summary>
/// Check if an existing page method needs repair
/// </summary>
/// <param name="method">Method to check</param>
/// <returns>Whether or not the page method needs repair</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
private bool PageMethodNeedsRepair(CodeFunction2 method)
{
if (method == null || method.Reference == null)
{
return false;
}
ParameterInfo[] parameters = _signature.GetParameters();
// Tweak the signature so it's appropriate
if (!method.IsShared)
{
return true;
}
if (method.Access != vsCMAccess.vsCMAccessPublic)
{
return true;
}
int i = 0;
foreach (object p in method.Parameters)
{
CodeParameter2 parameter = new CodeParameter2(p);
if ((parameter.Reference == null) || (string.Compare(parameter.Name, parameters[i++].Name, StringComparison.Ordinal) != 0))
{
return true;
}
}
// Add the necessary attributes
bool hasWebMethod = false;
bool hasScriptMethod = false;
foreach (object attr in method.Attributes)
{
CodeAttribute2 attribute = new CodeAttribute2(attr);
if (attribute.Reference == null)
{
continue;
}
hasWebMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("WebMethod");
hasScriptMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("ScriptMethod");
if (hasWebMethod && hasScriptMethod)
{
break;
}
}
return !hasWebMethod || !hasScriptMethod;
}
/// <summary>
/// Repair the page method
/// </summary>
/// <param name="method">Method to repair</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
private void RepairPageMethod(CodeFunction2 method)
{
if (method == null || method.Reference == null)
{
return;
}
// Tweak the signature so it's appropriate
method.IsShared = true;
method.Access = vsCMAccess.vsCMAccessPublic;
int i = 0;
ParameterInfo[] parameters = _signature.GetParameters();
foreach (object p in method.Parameters)
{
CodeParameter2 parameter = new CodeParameter2(p);
if (parameter.Reference != null)
{
parameter.Name = parameters[i++].Name;
}
}
// Add the necessary attributes
bool hasWebMethod = false;
bool hasScriptMethod = false;
foreach (object attr in method.Attributes)
{
CodeAttribute2 attribute = new CodeAttribute2(attr);
if (attribute.Reference == null)
{
continue;
}
hasWebMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("WebMethod");
hasScriptMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("ScriptMethod");
if (hasWebMethod && hasScriptMethod)
{
break;
}
}
if (!hasWebMethod)
{
method.AddAttribute(typeof(WebMethodAttribute).FullName, "", -1);
}
if (!hasScriptMethod)
{
method.AddAttribute(typeof(ScriptMethodAttribute).FullName, "", -1);
}
}
/// <summary>
/// Load the FileCodeModel associated with a web project item
/// </summary>
/// <param name="projectItem">Active ProjectItem</param>
/// <returns>FileCodeModel</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
private FileCodeModel2 LoadFileCodeModel(ProjectItem projectItem)
{
if (projectItem == null || projectItem.Reference == null)
{
throw new ArgumentNullException("ProjectItem cannot be null");
}
VSWebProjectItem webProjectItem = new VSWebProjectItem(projectItem.Object);
if (webProjectItem.Reference != null)
{
webProjectItem.Load();
return webProjectItem.ProjectItem.FileCodeModel;
}
return projectItem.FileCodeModel;
}
/// <summary>
/// Unload the WebProjectItem if it was loaded
/// </summary>
/// <param name="projectItem">Active ProjectItem</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods", Justification = "Security handled by base class")]
private void UnloadWebProjectItem(ProjectItem projectItem)
{
VSWebProjectItem webProjectItem = new VSWebProjectItem(projectItem.Object);
if (webProjectItem.Reference != null)
{
webProjectItem.Unload();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -