zoukankan      html  css  js  c++  java
  • Castle Query返回System.String程序报错的解决方法

      今天在写WebService时遇到一个想不明白的问题,我用Castle Query来执行hql时,居然不能返回System.String类型。我的代码如下:

    public static string[] GetOrderRank(DateTime stime,DateTime etime)
            {
                
                
    try
                {    
                    SimpleQuery query 
    = new SimpleQuery(typeof(string),

          "select o.Num12530 from Order o group by o.Num12530

           where o.UpdateDT between :start and :end ");

                    query.SetParameter("start",stime);
                    query.SetParameter(
    "end",etime);
                    
    return (string[])ExecuteQuery(query);
                }
                
    catch(ActiveRecordException er)
                {
                    
    throw er;
                }
            }

      调试程序报错,错误描述为:"You have accessed an ActiveRecord class that wasn't properly initialized. The only explanation is that the call to ActiveRecordStarter.Initialize() didn't include System.String class" 

    基本意思为:“程序中访问了没有初始化的类, ActiveRecordStarter.Initialize()方法中没有包含要初始化的类(System.String)”。我就不明白,字符串类型怎么可能还要首先初始化,Castle本身就是.net的一个开源框架,而且string为IL的一种类型,编译器能够直接识别,想想我在程序中使用System.Int32没有问题,我排除了这种可能性(绝对不需要首先初始化字符串数据类型)。

      刚开始没想到怎么去解决这个问题,我直接把string 类型换成object类型,程序可以正常运行了,总算出了口气,呵呵!可是程序是有点慢,因为有太多的box and unbox 操作,所以还是决定再看看。

      于是我查看了Castle的文档 SimpleQuery()重载了4次(我用的Castle版本为RC2),如下:

    1.  public SimpleQuery ( System.Type returnType , Castle.ActiveRecord.Queries.QueryLanguage queryLanguage , System.String query , params object[] positionalParameters )
    2. public SimpleQuery ( System.Type targetType , System.Type returnType , Castle.ActiveRecord.Queries.QueryLanguage queryLanguage , System.String query , params object[] parameters )
    3. public SimpleQuery ( System.Type returnType , System.String query , params object[] positionalParameters )
    4. public SimpleQuery ( System.Type targetType , System.Type returnType , System.String query , params object[] parameters )

      我上面的程序中使用是的第三个重载版本,我直接让它返回System.String程序就报错,于是我想到是不是我没有指明HQL要操作的目标类型呢?

      我使用了SimpleQuery()的另一个重载版本,更改如下:

    public static string[] GetOrderRank(DateTime stime,DateTime etime)
            {
                
                
    try
                {    
                    SimpleQuery query 
    = new SimpleQuery(typeof(Order),typeof(string),

        "select o.Num12530 from Order o group by o.Num12530 

        where o.UpdateDT between :start and :end ");

                     //省略

                }
                
    catch(ActiveRecordException er)
                {
                    
    throw er;
                }
            }

        在我指定了SimpleQuery()中的 System.Type returnType之后,可以直接返回string[]类型了!先兴奋下。

      我的理解是:由于我没有指明我的HQL语句所操作的目标类型(System.Type targetType的类型),Castle ActiveRecord把String类型当做一个用户自定的模型来处理,它与C#中的string类型完全不同,所有提示没有初始化该String。事实上,String模型根本就不存在。我没有查看Castle 的源代码,回去得好好研究下,不知道我的解释有没有错误,至少一点,我解决这个问题。希望有朋友能够给我更多的解释,我自己回去也看看。

      总结一点:写文章真不容易啊,得花不少的时间。不过 ,我会继续写下去的。

      God help those who help themselives!

    henry

  • 相关阅读:
    Scala_模式匹配
    Scala_特质
    Scala_继承
    Scala_对象
    Scala_类
    Scala_关键字
    Scala_数据结构
    Scala_方法、函数、柯里化
    Scala_控制结构
    Scala_基本语法
  • 原文地址:https://www.cnblogs.com/repository/p/1355472.html
Copyright © 2011-2022 走看看