How to write Implicit Conversion from an Interface to another type?

与世无争的帅哥 提交于 2021-02-19 02:36:05

问题


I am trying to do something like below:

public class SomeWrapper : ISomeWrapper{

  public static implicit operator ActualRec(ISomeWrapper someWrapper)
        {
            return ((SomeWrapper)someWrapper).SomeInfo;
        }
}

But this code fails, saying: "Either parameter or return type must be of type SomeWrapper".

I Understand the problem that compiling is stating. But I need this type conversionb because throughout my application I am using ISomeWrapper as the variable, storing SomeWrapper instance. (Also, SomeWrapper is the only class implementing ISomeWrapper).

Is there any way to do the implicit conversion if ISomeWrapper interface to the type that i know in my concrete class?

Edit: As suggested by all, implicit casting from interface is not possible in C#.

The reason I need to do this? I want to allow (implicitly) to the user of ISomeWrapper to invoke methods that need ActualRec as the parameter WITHOUT giving access to the users of ISomeWrapper to call methods/properties of ActualRec.

For instance, If I include a property ActualRec in ISomeWrapper then users of ISomeWrapper would be able to call methods available in ActualRec ( Say, someWrapper.ActualRec.Dispose() ), which I DO NOT want to expose.

That's the reason for trying to find some implicit conversion.

Also, I do not want to user SomeWrapper across application.

Please suggest if there is some concept/pattern to get this done.

Thanks for your interest.


回答1:


I would say there is a flaw in your design. You're casting to a concrete type from an interface, that makes the use of the interface pretty pointless. The interface is a contract through which implementing types will conform to provide a required set of services. In your example, is the SomeInfo property not defined in the interface (contract)? If not, why are you trying to cast using the interface at all? You should be using SomeWrapper as the input argument itself.




回答2:


This is restricted in C#. Read below.

http://msdn.microsoft.com/en-us/library/aa664464%28VS.71%29.aspx

A class or struct is permitted to declare a conversion from a source type S to a target type T provided all of the following are true:

...

Neither S nor T is object or an interface-type.

Also,

User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction ensures that no user-defined transformations occur when converting to an interface-type, and that a conversion to an interface-type succeeds only if the object being converted actually implements the specified interface-type.




回答3:


C# does not allow this, because such feature would easily lead to unreadable code that would dazzle other developers. Try using an extension method instead:

public static class SomeWrapperExtensions
{
    public static ActualRec ToActualRec(
        this ISomeWrapper wrapper)
    {
        return ((SomeWrapper)someWrapper).SomeInfo;
    }
}

This allows you to do the convention in a way that would not surprise other developers:

ActualRec rec = wrapper.ToActualRec();

Note that this still if fragile though, because you don't know whether the ISomeWrapper actually represents a SomeWrapper implementation.



来源:https://stackoverflow.com/questions/5746161/how-to-write-implicit-conversion-from-an-interface-to-another-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!