0

我越来越

“发生了‘System.NullReferenceException’类型的未处理异常”

在这条线上

IResultObject newCollection = connection.CreateInstance("SMS_Collection");

在 c# 表单应用程序中,但它适用于控制台应用程序。

非常感谢任何见解。

public void CreateStaticCollection(WqlConnectionManager connection, string newCollectionName, string newCollectionComment, bool ownedByThisSite, string resourceClassName, int resourceID)  
{
    try
    {
        // Create a new SMS_Collection object.
        IResultObject newCollection = connection.CreateInstance("SMS_Collection");
        // Populate new collection properties.
        newCollection["Name"].StringValue = newCollectionName;
        newCollection["Comment"].StringValue = newCollectionComment;
        newCollection["OwnedByThisSite"].BooleanValue = ownedByThisSite;
        //newCollection["LimitToCollectionID"].StringValue = limitToCollectionID;

        // Save the new collection object and properties.  
        // In this case, it seems necessary to 'get' the object again to access the properties.  
        newCollection.Put();
        newCollection.Get();

        // Create a new static rule object.
        IResultObject newStaticRule = connection.CreateInstance("SMS_CollectionRuleDirect");
        newStaticRule["ResourceClassName"].StringValue = resourceClassName;
        newStaticRule["ResourceID"].IntegerValue = resourceID;

        // Add the rule. Although not used in this sample, staticID contains the query identifier.                   
        Dictionary<string, object> addMembershipRuleParameters = new Dictionary<string, object>();
        addMembershipRuleParameters.Add("collectionRule", newStaticRule);
        IResultObject staticID = newCollection.ExecuteMethod("AddMembershipRule", addMembershipRuleParameters);

        // Start collection evaluator.
        Dictionary<string, object> requestRefreshParameters = new Dictionary<string, object>();
        requestRefreshParameters.Add("IncludeSubCollections", false);
        newCollection.ExecuteMethod("RequestRefresh", requestRefreshParameters);

        // Output message.
        Console.WriteLine("Created collection" + newCollectionName);
        Console.ReadKey();
    }

    catch (SmsException ex)
    {
        Console.WriteLine("Failed to create collection. Error: " + ex.Message);
        throw;
    }
}
4

0 回答 0