当前位置:编程学习 > C#/ASP.NET >>

js调用C#时报错

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;
}


 
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;
        }    
    }
}



 
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];
            }
        }
    }
}


最后报错object类型错误 --------------------编程问答-------------------- 最后一段代码79行,

  private void ParseQueryString( string queryString, ref string asmbName, ref string typeName, ref string funcName, ref object[] objectParam  )



看看 --------------------编程问答-------------------- 你这个反射  。。。。。。。。。。。 --------------------编程问答-------------------- 提交传递参数可以用ajax json来做,个人比较喜欢那种方法。
这样的调用为什么不做成 webService 呢
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,