简体   繁体   中英

function returns type A, make it type B (inherited from A)

I'm using Microsoft Exchange Web Services (EWS) Managed API 1.2 to access data from Exchange mailboxes using C3. Some of the properties of objects returned are difficult to access because they are custom properties.

To simplify accessing these properties, I wrote (as one example) a class called Contact2 that extends the Contact class that EWS gives me.

When calling a function that returns a Contact object, how can I "upgrade" it to a Contact2?

Here is the Contact2 class I wrote:

namespace ExchangeContacts
{
    class Contact2 : Microsoft.Exchange.WebServices.Data.Contact
    {

        public ExchangeService ex = new ExchangeService();
        public Dictionary<string, ExtendedPropertyDefinition> propDefs = new Dictionary<string, ExtendedPropertyDefinition>();
        public PropertySet myPropSet = new PropertySet();

        public Contact2(ExchangeService service)
            : base(service)
        {
            propDefs.Add("MarketingGuid", new ExtendedPropertyDefinition(new Guid("3694fe54-daf0-49bf-9e37-734cfb8521e1"), "MarketingGuid", MapiPropertyType.String));
            myPropSet = new PropertySet(BasePropertySet.FirstClassProperties) { propDefs["MarketingGuid"] };
            ex = service;
        }

        new public void Load()
        {
            base.Load(myPropSet);
        }

        new public void Load(PropertySet propertySet)
        {
            propertySet.Add(propDefs["MarketingGuid"]);
            base.Load(propertySet);
        }

        public string MarketingGuid
        {
            get
            {
                string g;
                if (TryGetProperty(propDefs["MarketingGuid"], out g))
                    return g;
                else
                    return "";
            }
            set
            {
                SetExtendedProperty(propDefs["MarketingGuid"], value);
            }
        }

    }
}

You need to define an explicit static method or constructor in Contact2 that accepts a Contact object. ie

public static Contact2 FromContact(Contact cnt)
{
    return new Contact2(cnt.x, cnt.y, ...)
}

If you really want to, you could define an implicit or explicit conversion. This is generally discouraged because it can cause confusion, so read up on explicit and implicit conversions before you do this. I won't tell you how it's done :P

You cannot magically convert an existing Contact instance into another class, even if that other class inherits Contact .

You need to modify the code that creates the Contact to create a Contact2 instead, or write your own function that creates a Contact2 that wraps an existing Contact instance.

You could add an operator overload for Contact then you would not need the static methods or at least have to make them public..

This is a bit difficult to get exactly right... see How can i override a base class's == operator, so the override gets called

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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