【问题标题】:error : is a field but used as a type错误:是一个字段,但用作类型
【发布时间】:2015-07-02 08:24:09
【问题描述】:
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
    }
    }
}

这是完整的代码。 我不知道为什么它显示错误。我搜索了错误“是一个字段,但用作一种类型”,我试过了,但没有帮助。由于我对此不熟悉,因此请提供解决方案代码。提前谢谢你。

【问题讨论】:

  • 很抱歉不得不这么说;但是您粘贴的代码有问题,或者那里有多个严重错误。我不得不假设这是最后的选择。在这种情况下,最好从基本的 C# 教程开始,然后自己找出错误的原因。
  • 有很多语法错误,你的代码根本无法构建。
  • 你想在这里实现什么?调用包含这些列的列表?然后!我想您应该更新您的问题,因为标题与描述不符。

标签: c#-4.0


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    相关资源
    最近更新 更多