在C#中继承.Net接口:如何访问基本成员

时间:2022-09-02 09:35:41

Inheritance of a .Net interface: How to access to base properties

.Net接口的继承:如何访问基本属性

I want to create my own category class inherited from Microsoft.Office.Interop.Outlook.Category interface but I am trying to access members of the base interface without success.

我想创建自己的继承自Microsoft.Office.Interop.Outlook.Category接口的类类,但我试图访问基接口的成员但没有成功。

I tried base.Name and this.Name both give me:

我试过base.Name和this.Name都给了我:

Error 2 'object' does not contain a definition for 'Name'

错误2“对象”不包含“名称”的定义

Using VS 2013, .Net 4.5

使用VS 2013,.Net 4.5

Code:

码:

using System;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace MyCategory
{
        public class MyCategory : Outlook.Category
    {
        private string colorName; 
        public string ColorName
        {
            get
            {
                return this.colorName;
            }
            set
            {
                //Name is a member of Outlook.Category
                //https://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.outlook.category_members.aspx
                this.colorName = base.Name;
                //
            }
        }

    }
}

3 个解决方案

#1


2  

So far I see on your code, you didn't implement interface. You're not inheriting from a class but following contract established by Outlook.Category interface. There is no "base" members here, you have to add members to your class.

到目前为止,我看到你的代码,你没有实现接口。您不是从类继承,而是遵循由Outlook.Category接口建立的合同。这里没有“基础”成员,您必须向您的班级添加成员。

If you put mouse cursor above Outlook.Category it should offer to implement it for you.

如果您将鼠标光标放在Outlook.Category上方,它应该提供为您实现它。

I recommend you to take a deeper look to how interfaces work on C#

我建议您深入了解接口如何在C#上工作

#2


1  

You are mistaking implementing an interface with object inheritance. Even though they both use the same syntax, they are very different.

您错误地实现了具有对象继承的接口。即使它们都使用相同的语法,它们也是非常不同的。

An interface is a contract to allow many different implementations of the same general methods and properties. It is a guarantee that the class you wrote supports certain actions. You have to write the implementations for interfaces. This allows others at a higher level to not care about the details of how something gets accomplished, yet it allows them to know it will get accomplished.

接口是允许相同通用方法和属性的许多不同实现的契约。保证您编写的类支持某些操作。您必须编写接口的实现。这允许更高级别的其他人不关心如何完成某些事情的细节,但它允许他们知道它将会完成。

Object inheritance allows you to use a parent class's (non-private) stuff. It's really taking a parent class and adding more features. In fact, in Java, this is known as "extending" a class. Find a class that already implements the interface Outlook.Category and inherit from that class and then call base.Name(). Then you could override or extend any additional behavior that you need.

对象继承允许您使用父类(非私有)的东西。它真的需要一个父类并添加更多功能。实际上,在Java中,这被称为“扩展”类。找到一个已经实现了Outlook.Category接口并继承该类的类,然后调用base.Name()。然后,您可以覆盖或扩展所需的任何其他行为。

I'm not familiar with the Outlook namespace, but the CategoryClass seems to be a class that implements your interface. You could try inheriting from that.

我不熟悉Outlook命名空间,但CategoryClass似乎是一个实现你的接口的类。你可以尝试从那继承。

#3


1  

What is it exactly that you are trying to do? Add a new category in Outlook? In that case, you simply need to access Outlook category storage (either the registry or the default store in the profile).

你究竟想做什么?在Outlook中添加新类别?在这种情况下,您只需要访问Outlook类别存储(注册表或配置文件中的默认存储)。

Take a look at the IPM.Configuration.CategoryList hidden message in the Calendar folder - you can see it using OutlookSpy: go to the Calendar folder, click IMAPIFolder button on the OutlookSpy ribbon, go to the "Associated Contents" tab, find the message with PR_MESSAGE_CLASS property = "IPM.Configuration.CategoryList", double click on it. The data will be in the PR_ROAMING_XMLSTREAM property. That hidden message can be accessed using MAPIFolder.GetStorage in the Outlook Object Model.

查看Calendar文件夹中的IPM.Configuration.CategoryList隐藏消息 - 您可以使用OutlookSpy查看它:转到Calendar文件夹,单击OutlookSpy功能区上的IMAPIFolder按钮,转到“Associated Contents”选项卡,找到消息使用PR_MESSAGE_CLASS property =“IPM.Configuration.CategoryList”,双击它。数据将位于PR_ROAMING_XMLSTREAM属性中。可以使用Outlook对象模型中的MAPIFolder.GetStorage访问该隐藏消息。

You can also use Redemption to add a new category - see the RDOCategories object. Something like the following will do the job (VBA):

您还可以使用Redemption添加新类别 - 请参阅RDOCategories对象。以下内容将完成工作(VBA):

 set vSession = CreateObject("Redemption.RDOSession")
 vSession.MAPIOBJECT = Application.Session.MAPIOBJECT
 set vStore = vSession.Stores.DefaultStore
 set vCategories = vStore.Categories
 set vCategory = vCategories.Add("Redemption Category", olCategoryColorPeach)

#1


2  

So far I see on your code, you didn't implement interface. You're not inheriting from a class but following contract established by Outlook.Category interface. There is no "base" members here, you have to add members to your class.

到目前为止,我看到你的代码,你没有实现接口。您不是从类继承,而是遵循由Outlook.Category接口建立的合同。这里没有“基础”成员,您必须向您的班级添加成员。

If you put mouse cursor above Outlook.Category it should offer to implement it for you.

如果您将鼠标光标放在Outlook.Category上方,它应该提供为您实现它。

I recommend you to take a deeper look to how interfaces work on C#

我建议您深入了解接口如何在C#上工作

#2


1  

You are mistaking implementing an interface with object inheritance. Even though they both use the same syntax, they are very different.

您错误地实现了具有对象继承的接口。即使它们都使用相同的语法,它们也是非常不同的。

An interface is a contract to allow many different implementations of the same general methods and properties. It is a guarantee that the class you wrote supports certain actions. You have to write the implementations for interfaces. This allows others at a higher level to not care about the details of how something gets accomplished, yet it allows them to know it will get accomplished.

接口是允许相同通用方法和属性的许多不同实现的契约。保证您编写的类支持某些操作。您必须编写接口的实现。这允许更高级别的其他人不关心如何完成某些事情的细节,但它允许他们知道它将会完成。

Object inheritance allows you to use a parent class's (non-private) stuff. It's really taking a parent class and adding more features. In fact, in Java, this is known as "extending" a class. Find a class that already implements the interface Outlook.Category and inherit from that class and then call base.Name(). Then you could override or extend any additional behavior that you need.

对象继承允许您使用父类(非私有)的东西。它真的需要一个父类并添加更多功能。实际上,在Java中,这被称为“扩展”类。找到一个已经实现了Outlook.Category接口并继承该类的类,然后调用base.Name()。然后,您可以覆盖或扩展所需的任何其他行为。

I'm not familiar with the Outlook namespace, but the CategoryClass seems to be a class that implements your interface. You could try inheriting from that.

我不熟悉Outlook命名空间,但CategoryClass似乎是一个实现你的接口的类。你可以尝试从那继承。

#3


1  

What is it exactly that you are trying to do? Add a new category in Outlook? In that case, you simply need to access Outlook category storage (either the registry or the default store in the profile).

你究竟想做什么?在Outlook中添加新类别?在这种情况下,您只需要访问Outlook类别存储(注册表或配置文件中的默认存储)。

Take a look at the IPM.Configuration.CategoryList hidden message in the Calendar folder - you can see it using OutlookSpy: go to the Calendar folder, click IMAPIFolder button on the OutlookSpy ribbon, go to the "Associated Contents" tab, find the message with PR_MESSAGE_CLASS property = "IPM.Configuration.CategoryList", double click on it. The data will be in the PR_ROAMING_XMLSTREAM property. That hidden message can be accessed using MAPIFolder.GetStorage in the Outlook Object Model.

查看Calendar文件夹中的IPM.Configuration.CategoryList隐藏消息 - 您可以使用OutlookSpy查看它:转到Calendar文件夹,单击OutlookSpy功能区上的IMAPIFolder按钮,转到“Associated Contents”选项卡,找到消息使用PR_MESSAGE_CLASS property =“IPM.Configuration.CategoryList”,双击它。数据将位于PR_ROAMING_XMLSTREAM属性中。可以使用Outlook对象模型中的MAPIFolder.GetStorage访问该隐藏消息。

You can also use Redemption to add a new category - see the RDOCategories object. Something like the following will do the job (VBA):

您还可以使用Redemption添加新类别 - 请参阅RDOCategories对象。以下内容将完成工作(VBA):

 set vSession = CreateObject("Redemption.RDOSession")
 vSession.MAPIOBJECT = Application.Session.MAPIOBJECT
 set vStore = vSession.Stores.DefaultStore
 set vCategories = vStore.Categories
 set vCategory = vCategories.Add("Redemption Category", olCategoryColorPeach)