子类化Microsoft.Exchange.WebServices.Data.Item

时间:2018-11-29 13:33:00

标签: c# subclass

如何从中创建子类?

我想向此类添加一些方法,但是当我尝试创建子类时,出现此错误:

{{2} }

这是我编写的代码(大多数代码是自动生成的):

<br/>

此基本代码给出了错误。据我所知,Item还没有构造函数,那么C#真正想要什么?

2 个答案:

答案 0 :(得分:1)

如果我们看一下documentation(或here),它实际上并没有显示类型Microsoft.Exchange.WebServices.Data.Item的任何构造函数,但是,如果您查看全部从Item继承的类型,它们都实现以下构造函数:

public InheritedFromItem(
    ExchangeService service
)

所以我想也许您也应该实现它。
刚刚确认查看了Item类型的源代码:

  

ews-managed-api / Item.cs ,掌握者·OfficeDev / ews-managed-api-GitHub   https://github.com/OfficeDev/ews-managed-api/blob/master/Core/ServiceObjects/Items/Item.cs

namespace Microsoft.Exchange.WebServices.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Represents a generic item. Properties available on items are defined in the ItemSchema class.
    /// </summary>
    [Attachable]
    [ServiceObjectDefinition(XmlElementNames.Item)]
    public class Item : ServiceObject
    {
        private ItemAttachment parentAttachment;

        /// <summary>
        /// Initializes an unsaved local instance of <see cref="Item"/>. To bind to an existing item, use Item.Bind() instead.
        /// </summary>
        /// <param name="service">The ExchangeService object to which the item will be bound.</param>
        internal Item(ExchangeService service)
            : base(service)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Item"/> class.
        /// </summary>
        /// <param name="parentAttachment">The parent attachment.</param>
        internal Item(ItemAttachment parentAttachment)
            : this(parentAttachment.Service)
        {
            // [...]
        }

        // [...]

看到它实际上有两个内部构造函数,一个内部构造函数接收ExchangeService对象,另一个内部构造函数ItemAttachment对象。

Contact的外观为例,该外观继承自Item,例如,它实现了ExchangeService构造函数为 public 和{{1} }构造器为内部

  

ews-managed-api / Contact.cs ,掌握者·OfficeDev / ews-managed-api-GitHub
  https://github.com/OfficeDev/ews-managed-api/blob/master/Core/ServiceObjects/Items/Contact.cs

ItemAttachment

因此,尝试在您的代码中模仿它:

namespace Microsoft.Exchange.WebServices.Data
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Represents a contact. Properties available on contacts are defined in the ContactSchema class.
    /// </summary>
    [Attachable]
    [ServiceObjectDefinition(XmlElementNames.Contact)]
    public class Contact : Item
    {
        private const string ContactPictureName = "ContactPicture.jpg";

        /// <summary>
        /// Initializes an unsaved local instance of <see cref="Contact"/>. To bind to an existing contact, use Contact.Bind() instead.
        /// </summary>
        /// <param name="service">The ExchangeService object to which the contact will be bound.</param>
        public Contact(ExchangeService service)
            : base(service)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Contact"/> class.
        /// </summary>
        /// <param name="parentAttachment">The parent attachment.</param>
        internal Contact(ItemAttachment parentAttachment)
            : base(parentAttachment)
        {
        }

        // [...]

但是您不能像这样实例化类的对象:

using Microsoft.Exchange.WebServices.Data;

public class ItemEx : Item
{
    public ItemEx(ExchangeService service)
        : base(service)
    {
    }

    internal ItemEx(ItemAttachment parentAttachment)
        : base(parentAttachment)
    {
    }
}

您应该这样做:

ItemEx myItem = new ItemEx();

更新

对不起,我以前的无知。 ExchangeService service = new ExchangeService(); ItemEx myItem = new ItemEx(service); 类的构造函数上的internal访问修饰符使它们只能在同一程序集中的文件中访问。

因此,这意味着该Item类不能在Item程序集之外的其他子类中继承/继承。一些参考:

  

答案 1 :(得分:0)

虽然不是完美的解决方案,但似乎没有办法用ToString方法扩展类,因为它们通常已经在其上实现了此方法。

另一个解决方案将像这样使用它:

class MailData
{
    public string subject;
    public Item mailItem;

    public MailData(string subject, Item mailItem)
    {
        this.subject = subject;
        this.mailItem = mailItem;
    }

    public override string ToString() => subject;

}

有了这个,可以像这样使用它:

        if (findResults.Items.Count > 0)
        {
            foreach (Item item in findResults.Items)
                comboBox1.Items.Add(new MailData(item.Subject,item));
        }

并稍后使用它:

                    EmailMessage item = (selectedItem as MailData).mailItem as EmailMessage;
                    textBox1.Text = $"From: {item.From.Address}";
                    wb.DocumentText = item.Body.Text;
                    wb.Update();

是的,这有点令人费解,虽然不是我想要的,但确实达到了目的。

PS:我也将物品用于其他目的,但在这里我将其用于电子邮件。