Javascript调用C#静态函数

Client Code

function WriteLog( assetID, billID, billState, bizType, logInfo, linkUrl, note )
{
    var tmp = new CSFunction();
    tmp.AsseblyName = "ZCGL_Com.dll";
    tmp.TypeName = "Genersoft.JTGL.ZCGL.ZCGL_Com.AssetLogMgr";
    tmp.FunctionName = "WriteLog";
    tmp.AddParameter( "string", assetID );
    tmp.AddParameter( "string", billID );
    tmp.AddParameter( "string", billState );
    tmp.AddParameter( "string", bizType );
    tmp.AddParameter( "string", logInfo );
    tmp.AddParameter( "string", linkUrl );
    tmp.AddParameter( "string", note );
    var rtn = tmp.Execute();
    tem = null;
    return rtn;
}

Javascript 核心调用类

function CSFunction()
{
    this.AsseblyName = "";
    this.TypeName = "";
    this.FunctionName = "";
    this._paramString = "";
    this._split0 = "#;#";
    this._split1 = "#:#";
    this.AddParameter = function( type, value )
    {
        var exps = type + this._split1 + value + this._split0;
        this._paramString += exps;
    }
    this.Execute = function()
    {
        if( this.AsseblyName == "" )
        {
            window.alert( "缺少程序集参数" );
            return false;
        }
        if( this.TypeName == "" )
        {
            window.alert( "缺少类名参数" );
            return false;
        }
        if( this.FunctionName == "" )
        {
            window.alert( "缺少方法名参数" );
            return false;
        }
        var param = "";
        param += "asmb" + this._split1 + this.AsseblyName + this._split0;
        param += "type" + this._split1 + this.TypeName + this._split0;
        param += "func" + this._split1 + this.FunctionName + this._split0;
        param += this._paramString;
        param = escape( param );
        var xhttp= new ActiveXObject("Microsoft.XMLHTTP");
        try
        {
            xhttp.open("GET", "../../Public_Web/ExecCSFunction.aspx?param="+param, false);
            xhttp.send();
            var vsRetval = xhttp.responseText;       
            xhttp = null;
            if( vsRetval.substring( 0, 2 ) == "/1" )    { window.alert( vsRetval.substring( 2 ) ); return false; }
            return vsRetval;
        }
        catch(error)
        {
            window.alert( "CSFunction处理失败:" + error.description );
            return false;
        }   
    }
}

C#后台程序

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.Text.RegularExpressions;
using Genersoft.JTGL.Public_Com;

namespace Genersoft.JTGL.Public_Web
{
    /// <summary>
    /// Summary description for ExecCSFunction.
    /// </summary>
    public class ExecCSFunction : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            #region clear cache

            Response.Buffer=true;
            Response.ExpiresAbsolute=System.DateTime.Now.AddSeconds(-1);
            Response.Expires=-1;
            Response.CacheControl="no-cache";

            #endregion

            string dllPath = Request.PhysicalApplicationPath + @"bin\";
            string AssemblyName = "", TypeName = "", FunctionName = "";
            ArrayList ReceiveParams = new ArrayList();
            object[] SendParams = new object[]{};

            Assembly assmebly;
            Type type;
            object rtnValue = new object();

            try
            {
                //接收参数
                string tempParam = Request.QueryString["param"];
                if( CommonFunction.IsEmptyOrNull( tempParam ) )
                {
                    throw new Exception( "缺少param参数" );
                }

                //解析参数
                ParseQueryString( tempParam, ref AssemblyName, ref TypeName, ref FunctionName, ref SendParams );

                //加载程序集           
                assmebly = Assembly.LoadFrom( dllPath + AssemblyName );   
                //调用方法
                type = assmebly.GetType( TypeName );
                rtnValue = type.InvokeMember( FunctionName,  BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, SendParams);
            }
            catch( Exception ex )
            {
                Response.Write( @"/1ExecCSFucntion处理失败:" + ex.Message );
                Response.End();
            }

            if( rtnValue == null )    rtnValue = @"/0";//表示没有返回值或返回值为null
            Response.Write( rtnValue );
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {   
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

        private void ParseQueryString( string queryString, ref string asmbName, ref string typeName, ref string funcName, ref object[] objectParam  )
        {
            ArrayList param = new ArrayList();
            string split0 = "#;#", split1 = "#:#";
            typeName = "";
            funcName = "";
            asmbName = "";

            string[] splitParam = Regex.Split( queryString, split0 );
            for( int i=0; i<splitParam.Length; i++ )
            {
                if( splitParam[i] == "" )    continue;
                string[] exps = Regex.Split( splitParam[i], split1 );
                if( exps[0] == "type" )
                {
                    typeName = exps[1];
                }
                else if( exps[0] == "func" )
                {
                    funcName = exps[1];
                }
                else if( exps[0] == "asmb" )
                {
                    asmbName = exps[1];
                }
                else
                {
                    switch( exps[0] )
                    {
                        case "string":
                            param.Add( exps[1] );
                            break;
                        case "int":
                            param.Add( Convert.ToInt32( exps[1] ) );
                            break;
                        case "decimal":
                            param.Add( Convert.ToDecimal( exps[1] ) );
                            break;
                    }
                }
            }

            objectParam = new object[ param.Count ];
            for( int i=0; i<param.Count; i++ )
            {
                objectParam[i] = param[i];
            }
        }
    }
}

你可能感兴趣的:(JavaScript)