error:是一个字段但用作类型

时间:2015-07-02 08:24:10

标签: c#-4.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication5
{
    public class ClientContext
    {
        private string p;

        public ClientContext(string p)
        {
            // TODO: Complete member initialization
            this.p = p;
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


    //First construct client context, the object which will be responsible for
    //communication with SharePoint:
        private ClientContext context = new ClientContext("@url"); 


    //then get a hold of the list item you want to download, for example
    public List list;
        public ClientContext
        {
           list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING");
        }

    //note that data has not been loaded yet. In order to load the data
    //you need to tell SharePoint client what you want to download:

    context.Load(result, items=>items.Include(
        item => item["Title"],
        item => item["FileRef"]
    ));

    //now you get the data
    context.ExecuteQuery();

    //here you have list items, but not their content (files). To download file
    //you'll have to do something like this:

    var item = items.First();

    //get the URL of the file you want:
    var fileRef = item["FileRef"];

    //get the file contents:
    FileInformation fileInfo = File.OpenBinaryDirect(context, fileRef.ToString());

    using (var memory = new MemoryStream())
    {
          byte[] buffer = new byte[1024 * 64];
          int nread = 0;
          while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
          {
              memory.Write(buffer, 0, nread);
          }
          memory.Seek(0, SeekOrigin.Begin);
          // ... here you have the contents of your file in memory, 
          // do whatever you want
    }
    }
}

这是完整的代码。 我不知道为什么会出现错误。我搜索了错误"是一个字段,但用作类型"我试过了,但没有帮助。因为我是新手,请帮助解决这个问题的解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:0)

你想通过这行代码实现什么目标?

public partial class Form1 : Form
{
    ...
    public ClientContext
    {
       list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING");
    }    
}

Form1类中的public ClientContext {}是什么?

似乎你打算在另一个类中为一个类创建构造函数,而对于编译器来说,它看起来更像是一个属性,但没有访问器(get,set),好像它是一个Type或类似这样的smth。

尝试把get;组;如果您打算创建属性,请在里面访问:

public List Context
{ 
   get 
   { 
       list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING");
       return list;
   } 
}

或者将其更改为方法:

   public void GetClientContext()
   {
      list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING");
   }