【问题标题】:C#, Method creates a list but is not of type list?C#,方法创建一个列表但不是列表类型?
【发布时间】:2013-06-07 01:48:39
【问题描述】:

来自 PHP 我不习惯分配或返回特定类型,因为 PHP 真的不在乎。但是回到 Java 和 C# 的世界,这些语言确实在乎,当你说传递给我这个类型时,它期望那个类型。那么我做错了什么以及如何将其创建为 SPList

类型

我有一个非常基本的功能,例如:

    protected void createNewList(SPFeatureReceiverProperties properties)
    {
        Dictionary<string, List<AddParams>> param = new Dictionary<string, List<AddParams>>();

        // Create the keys
        param.Add("Name", new List<AddParams>());
        param.Add("Type", new List<AddParams>());
        param.Add("Description", new List<AddParams>());

        // Set the values
        param["Name"].Add(new AddParams { type = SPFieldType.Text, required = true });
        param["Type"].Add(new AddParams { type = SPFieldType.Text, required = true });
        param["Description"].Add(new AddParams { type = SPFieldType.Text, required = true });

        // Create the really simple List.
        new SPAPI.Lists.Create(properties, param, "Fake List", "Sample Description", SPListTemplateType.GenericList, "Sample View Description");
    }

这将为您创建一个列表,即激活 Web 部件后的 SharePoint 2010 列表。这个名字是假列表,我们看到我们传入了一些列及其受尊重的参数。让我们看看这个SPAPI.Lists.Create 方法:

    public Create(SPFeatureReceiverProperties properties, Dictionary<string, List<AddParams>> columns, 
        string name, string description, SPListTemplateType type, string viewDescription)
    {

        SPSite siteCollection = properties.Feature.Parent as SPSite;
        if (siteCollection != null)
        {
            SPWeb web = siteCollection.RootWeb;
            Guid Listid = web.Lists.Add(name, description, type);
            web.Update();

            // Add the new list and the new content.
            SPList spList = web.Lists[name];
            foreach(KeyValuePair<string, List<AddParams>> col in columns){
                spList.Fields.Add(col.Key, col.Value[0].type, col.Value[0].required);
            }

            spList.Update();

            //Create the view? - Possibly remove me.
            System.Collections.Specialized.StringCollection stringCollection =
                new System.Collections.Specialized.StringCollection();

            foreach (KeyValuePair<string, List<AddParams>> col in columns)
            {
                stringCollection.Add(col.Key);
            }

            //Add the list.
            spList.Views.Add(viewDescription, stringCollection, @"", 100,
                true, true, Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false);
            spList.Update();
        }
    }

我们可以看到这里所做的只是创建一个用于 Sharepoint 的 SPList 对象。部署后,我们有一个可以添加到页面的新列表。那么有什么问题呢?

在 Php 中,我可以将 createNewList(SPFeatureReceiverProperties properties) 传递给请求 SPList 类型对象的函数,它会起作用(除非我遗漏了某些东西 >.>)就像这样,不,这不是 SPList 消失。

所以我的问题是:

要创建列表并返回 SPLSt 对象,我需要进行哪些更改? 是否像 return new SPAPI.Lists.Create(properties, param, "Fake List", "Sample Description", SPListTemplateType.GenericList, "Sample View Description"); 一样简单

因为这对我来说似乎是正确的。

更新

将方法签名转为 SPList 并返回 return new .... 无效。

【问题讨论】:

  • 您似乎正在调用名为Create 的类的构造函数。这似乎不是一个好主意。远不清楚你在这里真正想要做什么......

标签: c# sharepoint-2010 type-hinting splist


【解决方案1】:

您需要从两种方法中返回一个 SPList:

protected SPList createNewList(SPFeatureReceiverProperties properties)
{
    //Do the stuff

    SPList result = new SPAPI.Lists.Create(properties, param, "Fake List", "Sample Description", SPListTemplateType.GenericList, "Sample View Description");
    return result;
}

public SPList Create(SPFeatureReceiverProperties properties, Dictionary<string, List<AddParams>> columns, 
    string name, string description, SPListTemplateType type, string viewDescription)
{
    // Do the stuff

    return spList;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2012-10-27
    • 2012-03-21
    相关资源
    最近更新 更多