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

C# 子类化Microsoft.Exchange.WebServices.Data.Item,c#,subclass,C#,Subclass,我如何从中创建子类 我想向这个类添加一些方法,但是当我尝试创建一个子类时,我得到了以下错误: Error CS1729 'Item' does not contain a constructor that takes 0 arguments 以下是我编写的代码(大部分是自动生成的): 这个基本代码给出了一个错误。另外,据我所知,该项没有构造函数,那么c#真正想要什么呢?如果我们看一下(或),它实际上没有显示类型Microsoft.Exchange.WebServices.Data.Ite

我如何从中创建子类

我想向这个类添加一些方法,但是当我尝试创建一个子类时,我得到了以下错误:

Error   CS1729  'Item' does not contain a constructor that takes 0 arguments
以下是我编写的代码(大部分是自动生成的):

这个基本代码给出了一个错误。另外,据我所知,该项没有构造函数,那么c#真正想要什么呢?

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

public InheritedFromItem(
    ExchangeService service
)
所以我想也许你也应该实施它。
刚刚确认查看
项的源代码
类型:

ews管理api/Item.cs位于master·OfficeDev/ews管理api-GitHub

因此,试着在代码中模仿它:

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);
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));
        }
你应该这样做:

ItemEx myItem = new ItemEx();
ExchangeService service = new ExchangeService();
ItemEx myItem = new ItemEx(service);
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));
        }

更新

对不起,我以前不知道。
Item
类的构造函数上的访问修饰符使它们只能在同一程序集中的文件中访问

因此,这意味着这个
类不能在
Microsoft.Exchange.WebServices.dll
程序集之外从其他类继承子类。一些参考:


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

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

ItemEx myItem = new ItemEx();
ExchangeService service = new ExchangeService();
ItemEx myItem = new ItemEx(service);
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));
        }
有了它,可以像这样使用它:

ItemEx myItem = new ItemEx();
ExchangeService service = new ExchangeService();
ItemEx myItem = new ItemEx(service);
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:我也将项目用于其他目的,但在这里我将其用于电子邮件。

首先为什么需要从
项目继承?正如您所知,每个项目都是邮件、联系人或日历项目等。。我想把它们全部放在一个组合框中,并希望它以组合框文本的形式显示主题(通过使用上面的代码创建一个ToString方法)。。选择组合框后,我希望在另一个控件中显示其他属性。。可以将其存储在其他位置,然后通过索引访问,但这将是最简单的。@Plato,您可以使用扩展方法,正如我在我的答案中添加的链接上所建议的那样。@Plato,我的回答是否回答了您最初的问题?我想扩展方法将涵盖我想做的事情。。所以没关系。。不,这也不会编译,因为“Item”不包含也接受1个参数的构造函数。@s.Akbari,我已经更新了答案,现在看看。此响应没有回答原始问题:如何子类
Microsoft.Exchange.WebServices.Data.Item
?原来问题的答案是:你不能。你最终改变了问题的目标(基于我的答案?)。当这种情况发生时(根据答案出现另一个问题),您应该创建另一个问题,因为它是。。。另一个问题。