Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write Implicit Conversion from an Interface to another type?
    primarykey
    data
    text
    <p>I am trying to do something like below:</p> <pre><code>public class SomeWrapper : ISomeWrapper{ public static implicit operator ActualRec(ISomeWrapper someWrapper) { return ((SomeWrapper)someWrapper).SomeInfo; } } </code></pre> <p>But this code fails, saying: "Either parameter or return type must be of type SomeWrapper".</p> <p>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).</p> <p>Is there any way to do the implicit conversion if ISomeWrapper interface to the type that i know in my concrete class?</p> <p>Edit: As suggested by all, implicit casting from interface is not possible in C#.</p> <p>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.</p> <p>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.</p> <p>That's the reason for trying to find some implicit conversion.</p> <p>Also, I do not want to user SomeWrapper across application.</p> <p>Please suggest if there is some concept/pattern to get this done.</p> <p>Thanks for your interest.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload