iframeprocessor.cs

来自「AJAX开发工具包」· CS 代码 · 共 106 行

CS
106
字号
using System;
using System.Web;
using System.Reflection;
using System.IO;

namespace AjaxPro
{
	internal class IFrameProcessor : IAjaxProcessor
	{
		private int hashCode;

		internal IFrameProcessor(HttpContext context, Type type) : base(context, type)
		{
		}

		#region IAjaxProcessor Members

		public override object[] RetreiveParameters()
		{
			ParameterInfo[] pi = method.GetParameters();

			if(pi.Length == 0)
				return null;

			object[] args = new object[pi.Length];

			// initialize default values
			for(int i=0; i<pi.Length; i++)
				args[i] = pi[i].DefaultValue;

			string v = context.Request["data"];

			if(v == null)	// something went wrong, we cannot fill the arguments
				return args;

			hashCode = v.GetHashCode();

			// check if we have to decrypt the JSON string.
			if(Utility.Settings != null && Utility.Settings.Encryption != null)
				v = Utility.Settings.Encryption.CryptProvider.Decrypt(v);

			JavaScriptObject jso = (JavaScriptObject)JavaScriptDeserializer.Deserialize(v, typeof(JavaScriptObject));
			for(int i=0; i<pi.Length; i++)
			{
				if(jso.Contains(pi[i].Name))
					args[i] = JavaScriptDeserializer.Deserialize((IJavaScriptObject)jso[pi[i].Name], pi[i].ParameterType);
			}

			return args;
		}

		public override string SerializeObject(object o)
		{
			// On the client we want to have a real object.
			// For more details visit: http://www.crockford.com/JSON/index.html
			// 
			// JSON is built on two structures:
			//   - A collection of name/value pairs. In various languages, 
			//     this is realized as an object, record, struct, dictionary, 
			//     hash table, keyed list, or associative array. 
			//   - An ordered list of values. In most languages, this is realized 
			//     as an array. 

			string res = JavaScriptSerializer.Serialize(o);
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			sb.Append("<html><body><script language=\"javascript\">document.body.res = ");
			sb.Append(JavaScriptSerializer.SerializeString(res));
			sb.Append("</script></body></html>");

			context.Response.ContentType = "text/html";
			context.Response.Write(sb.ToString());

			return sb.ToString();
		}

		public override MethodInfo Method
		{
			get
			{
				if(context.Request["Ajax-method"] != null)
					method = type.GetMethod(context.Request["Ajax-method"]);

				return method;
			}
		}

		public override bool IsEncryptionAble
		{
			get
			{
				return true;
			}
		}

		public override int GetHashCode()
		{
			return hashCode;
		}

		#endregion

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?