website类型的项目如何设置/unsafe编译开关?大家都知道,在一个console应用中,这个开关在项目的属性里面设置,很简单,但是website类型的项目属性里面没有这个开关了?请问在那里可以设置?我建了一个IIS网站:首页:Default.aspx
后台代码:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ExtractDBContent();
            GridViewBind();
        }
    }    private void ExtractDBContent()
    {
        m_ds = new DBSET();
        if (!m_ds.DBOpen())
        {
            this.Response.Write("Open database failed!!!");
            this.Response.End();
        }
        else
        {
            int product_id;
            byte kind;
            string exch_code;
            string pf_code;
            string period;
            byte option_type;
            double strike_price;
            double price;
            double d;
            double v;
            double cvf;
            double scaling_factor;
            double ra_01;
            double ra_02;
            double ra_03;
            double ra_04;
            double ra_05;
            double ra_06;
            double ra_07;
            double ra_08;
            double ra_09;
            double ra_10;
            double ra_11;
            double ra_12;
            double ra_13;
            double ra_14;
            double ra_15;
            double ra_16;
            double ra_com_delta;            SqlConnection objConnection = new SqlConnection("Server=192.168.2.134;DataBase=Rmm;User id=sa;password=;");
            string strSQL = "select * from dbo.contract";
            SqlCommand sqlCommand = new SqlCommand(strSQL, objConnection);
            sqlCommand.CommandType = CommandType.Text;
            sqlCommand.Connection.Open();
            SqlDataReader dr = sqlCommand.ExecuteReader();            CContract tb = new CContract();
            int i = 0;            while (dr.Read())
            {
                product_id = dr.GetInt32(1);
                kind = dr.GetByte(2);
                exch_code = dr.GetString(3);
                pf_code = dr.GetString(4);
                period = dr.GetString(5);
                if (!dr.IsDBNull(6))
                    option_type = dr.GetByte(6);
                else
                    option_type = 0;                if (!dr.IsDBNull(7))
                    strike_price = Convert.ToDouble(dr.GetDecimal(7));
                else
                    strike_price = 0.0;                unsafe
                {
                    fixed (byte* pexch_code = &(Encoding.ASCII.GetBytes(exch_code)[0]),
                        ppf_code = &(Encoding.ASCII.GetBytes(pf_code)[0]),
                        pperiod = &(Encoding.ASCII.GetBytes(period)[0]))
                    {
                        tb.Insert(product_id, kind, (sbyte*)pexch_code, (sbyte*)ppf_code, (sbyte*)pperiod,
                           option_type, strike_price, price, d, v, cvf, scaling_factor, ra_01, ra_02, ra_03, ra_04, ra_05,
                           ra_06, ra_07, ra_08, ra_09, ra_10, ra_11, ra_12, ra_13, ra_14, ra_15, ra_16, ra_com_delta);
                    }
                }                i++;            }
            dr.Close();
            sqlCommand.Connection.Close();
        }
    }编译报错:
Error 4 Unsafe code may only appear if compiling with /unsafe D:\fastdb\examples\WebTest\Default.aspx.cs 206 17 http://localhost/Examples/WebTest/请问怎么解决?谢谢了

解决方案 »

  1.   

    我的环境是
    WINXP+SP2
    VS2005
    SQL SERVER2005
    IE6
      

  2.   

    這個aspx.cs偵錯時會出現未使用/unsafe選項的錯誤,
    查詢MSDN说是需要修改Web.Config的內容,
    加入 <compiler language="c#" compilerOptions="unsafe+" />,
    卻會出現無法辨識compiler的錯誤,
    請問這樣的情況該如何解決,
    謝謝
      

  3.   

    C#中为使开发更加简单化,所以隐藏了对内存的直接管理,但有的时候我们也有对内存直接操作的需求,那么unsafe代码块也就应运而生了.
    一般来说,我们在.net 框架中无须使用到unsafe code,但有的应用案例中我们需要用上它,如下:
    适时应用程序:我们也许需要用指针来提供程序的功能;
    外部方法:在非.net的Dll中要求使用指针来作为参数,如在C语言下编写的windows API中;
    调试中:有时我们需要查看内存的内容来达到调试的目的,或者你可能要写一个程序来分析拧一个处理进程或 某个内存的状况;下面我继续介绍unsafe code使用的条件,由于文摘比较长,我只为英文不怎么好的写点概要,下面说说使用中最麻烦的地方,那就是使用unsafe code遇到的错误,我们可能会遇到这样的错误:error CS0227: Unsafe code may only appear if compiling with /unsafe其实不是你的代码出错了,要在vs.net中使用unsafe code 我们必须在项目的属性中设置一下,设置方法如下:点项目属性->配置属性->生成->常规中:允许不安全代码(钩上)好了,我们现在就可以正常的使用unsafe代码了
      

  4.   

    在web.config中添加<compilers><compilerlanguage="c#;cs;csharp" extension=".cs"compilerOptions="/unsafe"type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /></compilers>