📄 toolkitscriptmanager.cs
字号:
mvid = script;
}
else
{
if (null == resourceNameHashToResourceName)
{
// Populate the "resource name hash to resource name" dictionary for this assembly
resourceNameHashToResourceName = new Dictionary<string, string>();
foreach (string resourceName in (new ScriptEntry(assembly, null, null)).LoadAssembly().GetManifestResourceNames())
{
string hashCode = resourceName.GetHashCode().ToString("x", CultureInfo.InvariantCulture);
if (resourceNameHashToResourceName.ContainsKey(hashCode))
{
// Hash collisions are exceedingly rare, but possible
throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Assembly \"{0}\" contains multiple scripts with hash code \"{1}\".", assembly, hashCode));
}
resourceNameHashToResourceName[hashCode] = resourceName;
}
}
// Map the script hash to a script name
string scriptName;
if (!resourceNameHashToResourceName.TryGetValue(script, out scriptName))
{
throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Assembly \"{0}\" does not contain a script with hash code \"{1}\".", assembly, script));
}
// Create a ScriptEntry to represent the script
ScriptEntry scriptEntry = new ScriptEntry(assembly, scriptName, culture);
scriptEntry.Loaded = loaded;
scriptEntries.Add(scriptEntry);
}
}
}
return scriptEntries;
}
/// <summary>
/// Name of the hidden field used to store loaded scripts
/// </summary>
protected string HiddenFieldName
{
get { return ClientID + "_HiddenField"; }
}
/// <summary>
/// Represents a script reference - including tracking its loaded state in the client browser
/// </summary>
private class ScriptEntry
{
/// <summary>
/// Containing Assembly
/// </summary>
public readonly string Assembly;
/// <summary>
/// Script name
/// </summary>
public readonly string Name;
/// <summary>
/// Culture to render the script in
/// </summary>
public readonly string Culture;
/// <summary>
/// Loaded state of the script in the client browser
/// </summary>
public bool Loaded;
/// <summary>
/// Reference to the Assembly object (if loaded by LoadAssembly)
/// </summary>
private Assembly _loadedAssembly;
/// <summary>
/// Constructor
/// </summary>
/// <param name="assembly">containing assembly</param>
/// <param name="name">script name</param>
/// <param name="culture">culture for rendering the script</param>
public ScriptEntry(string assembly, string name, string culture)
{
Assembly = assembly;
Name = name;
Culture = culture;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="scriptReference">script reference</param>
public ScriptEntry(ScriptReference scriptReference)
: this(scriptReference.Assembly, scriptReference.Name, null)
{
}
/// <summary>
/// Gets the script corresponding to the object
/// </summary>
/// <returns>script text</returns>
public string GetScript()
{
string script;
using (Stream stream = LoadAssembly().GetManifestResourceStream(Name))
{
using (StreamReader reader = new StreamReader(stream))
{
script = reader.ReadToEnd();
}
}
return script;
}
/// <summary>
/// Loads the associated Assembly
/// </summary>
/// <returns>Assembly reference</returns>
public Assembly LoadAssembly()
{
if (null == _loadedAssembly)
{
_loadedAssembly = System.Reflection.Assembly.Load(Assembly);
}
return _loadedAssembly;
}
/// <summary>
/// Equals override to compare two ScriptEntry objects
/// </summary>
/// <param name="obj">comparison object</param>
/// <returns>true iff both ScriptEntries represent the same script</returns>
public override bool Equals(object obj)
{
ScriptEntry other = (ScriptEntry)obj;
return ((other.Assembly == Assembly) && (other.Name == Name));
}
/// <summary>
/// GetHashCode override corresponding to the Equals override above
/// </summary>
/// <returns>hash code for the object</returns>
public override int GetHashCode()
{
return Assembly.GetHashCode() ^ Name.GetHashCode();
}
}
/// <summary>
/// Callable implementation of System.Web.Script.Serialization.JavaScriptString.QuoteString
/// </summary>
/// <param name="value">value to quote</param>
/// <returns>quoted string</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Callable implementation of System.Web.Script.Serialization.JavaScriptString.QuoteString")]
protected static string QuoteString(string value)
{
StringBuilder builder = null;
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
int startIndex = 0;
int count = 0;
for (int i = 0; i < value.Length; i++)
{
char c = value[i];
if ((((c == '\r') || (c == '\t')) || ((c == '"') || (c == '\''))) || ((((c == '<') || (c == '>')) || ((c == '\\') || (c == '\n'))) || (((c == '\b') || (c == '\f')) || (c < ' '))))
{
if (builder == null)
{
builder = new StringBuilder(value.Length + 5);
}
if (count > 0)
{
builder.Append(value, startIndex, count);
}
startIndex = i + 1;
count = 0;
}
switch (c)
{
case '<':
case '>':
case '\'':
{
AppendCharAsUnicode(builder, c);
continue;
}
case '\\':
{
builder.Append(@"\\");
continue;
}
case '\b':
{
builder.Append(@"\b");
continue;
}
case '\t':
{
builder.Append(@"\t");
continue;
}
case '\n':
{
builder.Append(@"\n");
continue;
}
case '\f':
{
builder.Append(@"\f");
continue;
}
case '\r':
{
builder.Append(@"\r");
continue;
}
case '"':
{
builder.Append("\\\"");
continue;
}
}
if (c < ' ')
{
AppendCharAsUnicode(builder, c);
}
else
{
count++;
}
}
if (builder == null)
{
return value;
}
if (count > 0)
{
builder.Append(value, startIndex, count);
}
return builder.ToString();
}
/// <summary>
/// Callable implementation of System.Web.Script.Serialization.JavaScriptString.AppendCharAsUnicode
/// </summary>
/// <param name="builder">string builder</param>
/// <param name="c">character to append</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Callable implementation of System.Web.Script.Serialization.JavaScriptString.AppendCharAsUnicode")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "c", Justification = "Callable implementation of System.Web.Script.Serialization.JavaScriptString.AppendCharAsUnicode")]
protected static void AppendCharAsUnicode(StringBuilder builder, char c)
{
builder.Append(@"\u");
builder.AppendFormat(CultureInfo.InvariantCulture, "{0:x4}", new object[] { (int)c });
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -