javascriptutil.cs
来自「AJAX开发工具包」· CS 代码 · 共 145 行
CS
145 行
/*
* MS 06-04-04 fixed GetEnumRepresentation if type.FullName has no "."
* MS 06-04-12 fixed \0 return value for QuoteString, will be simply removed from the string
*
*
*/
using System;
using System.Xml;
using System.Text;
namespace AjaxPro
{
/// <summary>
/// Provides helper methods for JavaScript.
/// </summary>
public sealed class JavaScriptUtil
{
/// <summary>
/// Quote the fiven string to be used in JSON.
/// </summary>
/// <param name="s">The string to quote.</param>
/// <returns>Returns the quoted string.</returns>
public static string QuoteString(string s)
{
if(s != null)
return s.Replace("\0", "").Replace("\\", "\\\\").Replace("\t", "\\t").Replace("\r", "\\r").Replace("\n", "\\n").Replace("\"", "\\\"");
return s;
}
/// <summary>
/// Converts a enum type to a JavaScript representation.
/// </summary>
/// <param name="type">The type of the enum.</param>
/// <returns>Returns a JavaScript that will add a local variable to the page.</returns>
public static string GetEnumRepresentation(Type type)
{
if(type.IsEnum == false)
return "";
StringBuilder sb = new StringBuilder();
AjaxNamespaceAttribute[] ema = (AjaxNamespaceAttribute[])type.GetCustomAttributes(typeof(AjaxNamespaceAttribute), true);
if(ema.Length > 0 && ema[0].ClientNamespace.Replace(".", "").Length > 0)
{
sb.Append("addNamespace(\"" + ema[0].ClientNamespace + "\");\r\n");
sb.Append(ema[0].ClientNamespace + ".");
sb.Append(type.Name);
}
else
{
sb.Append("addNamespace(\"" + (type.FullName.IndexOf(".") > 0 ? type.FullName.Substring(0, type.FullName.LastIndexOf(".")) : type.FullName) + "\");\r\n");
sb.Append(type.FullName);
}
sb.Append(" = {\r\n");
string[] names = Enum.GetNames(type);
int c=0;
foreach(int i in Enum.GetValues(type))
{
sb.Append("\t\"");
sb.Append(names[c]);
sb.Append("\":");
sb.Append(i);
if(c < names.Length -1)
sb.Append(",\r\n");
c++;
}
sb.Append("\r\n}\r\n");
return sb.ToString();
}
/// <summary>
/// Converts an IJavaScriptObject to an XML document.
/// </summary>
/// <param name="o">The IJavaScript object to convert.</param>
/// <returns>Returns an XML document.</returns>
public static XmlDocument ConvertIJavaScriptObjectToXml(IJavaScriptObject o)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root/>");
AddIJavaScriptObject(doc.DocumentElement, o);
return doc;
}
internal static void AddIJavaScriptObject(XmlElement n, IJavaScriptObject o)
{
if(o is JavaScriptArray)
{
XmlElement p = n.OwnerDocument.CreateElement("array");
foreach(IJavaScriptObject a in (JavaScriptArray)o)
AddIJavaScriptObject(p, a);
n.AppendChild(p);
}
else if(o is JavaScriptBoolean)
{
XmlElement p = n.OwnerDocument.CreateElement("boolean");
p.InnerText = o.Value;
n.AppendChild(p);
}
else if(o is JavaScriptNumber)
{
XmlElement p = n.OwnerDocument.CreateElement("number");
p.InnerText = o.Value;
n.AppendChild(p);
}
else if(o is JavaScriptString)
{
XmlElement p = n.OwnerDocument.CreateElement("string");
p.InnerText = o.Value;
n.AppendChild(p);
}
else if(o is JavaScriptObject)
{
XmlElement p = n.OwnerDocument.CreateElement("object");
foreach(string key in ((JavaScriptObject)o).Keys)
{
XmlElement e = n.OwnerDocument.CreateElement("property");
e.SetAttribute("name", key);
p.AppendChild(e);
AddIJavaScriptObject(e, (IJavaScriptObject)((JavaScriptObject)o)[key]);
}
n.AppendChild(p);
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?