简体   繁体   English

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

[英]Inheritance of a .Net interface in C#: How to access base members

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;
                //
            }
        }

    }
}

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.您不是从类继承,而是遵循Outlook.Category接口建立的合同。 There is no "base" members here, you have to add members to your class.这里没有“基本”成员,您必须将成员添加到您的班级。

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# 上工作

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.实际上,在 Java 中,这称为“扩展”类。 Find a class that already implements the interface Outlook.Category and inherit from that class and then call base.Name() .找到一个已经实现接口Outlook.Category从该类继承,然后调用base.Name() Then you could override or extend any additional behavior that you need.然后,您可以覆盖或扩展您需要的任何其他行为。

I'm not familiar with the Outlook namespace, but the CategoryClass seems to be a class that implements your interface.我不熟悉Outlook命名空间,但CategoryClass似乎是一个实现您的接口的类。 You could try inheriting from that.你可以尝试继承它。

What is it exactly that you are trying to do?你到底想要做什么? Add a new category in Outlook?在 Outlook 中添加新类别? In that case, you simply need to access Outlook category storage (either the registry or the default store in the profile).在这种情况下,您只需访问 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.查看日历文件夹中的 IPM.Configuration.CategoryList 隐藏消息 - 您可以使用OutlookSpy看到它:转到日历文件夹,单击 OutlookSpy 功能区上的 IMAPIFolder 按钮,转到“相关内容”选项卡,找到消息使用 PR_MESSAGE_CLASS 属性 = "IPM.Configuration.CategoryList",双击它。 The data will be in the PR_ROAMING_XMLSTREAM property.数据将位于 PR_ROAMING_XMLSTREAM 属性中。 That hidden message can be accessed using MAPIFolder.GetStorage in the Outlook Object Model.可以使用 Outlook 对象模型中的 MAPIFolder.GetStorage 访问该隐藏消息。

You can also use Redemption to add a new category - see the RDOCategories object.您还可以使用Redemption添加新类别 - 请参阅RDOCategories对象。 Something like the following will do the job (VBA):像下面这样的东西将完成这项工作(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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM