Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Seriously? user-defined conversions to or from an interface are not allowed

Seriously? user-defined conversions to or from an interface are not allowed

Scheduled Pinned Locked Moved C#
comcsharphelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alaric_
    wrote on last edited by
    #1

    the relevant code

    public static explicit operator ExcelWorksheet(Worksheet worksheet)
    {
    return new ExcelWorksheet(worksheet, 1,1);
    }
    public static explicit operator Worksheet(ExcelWorksheet worksheet)
    {
    return worksheet._worksheet;
    }

    ...I'm trying to introduce a cast from the Interop.Excel.Worksheet to our Biz.Custom.ExcelWorksheet so that I can prep the COM proxy outside of the existing process, and allow an explicit cast from a valid COM Worksheet to be accepted as the custom's structurally subtyped property. I researched the error and found out that this did not make the prioritization list to be removed from C#4 but could not find a workaround. Does anyone have the proper incantation that will allow me to cast from the COM object to my custom? Being required to convert these to "FromWorksheet" and "ToWorksheet" methods seems *completely* asinine.

    "I need build Skynet. Plz send code"

    B 1 Reply Last reply
    0
    • A Alaric_

      the relevant code

      public static explicit operator ExcelWorksheet(Worksheet worksheet)
      {
      return new ExcelWorksheet(worksheet, 1,1);
      }
      public static explicit operator Worksheet(ExcelWorksheet worksheet)
      {
      return worksheet._worksheet;
      }

      ...I'm trying to introduce a cast from the Interop.Excel.Worksheet to our Biz.Custom.ExcelWorksheet so that I can prep the COM proxy outside of the existing process, and allow an explicit cast from a valid COM Worksheet to be accepted as the custom's structurally subtyped property. I researched the error and found out that this did not make the prioritization list to be removed from C#4 but could not find a workaround. Does anyone have the proper incantation that will allow me to cast from the COM object to my custom? Being required to convert these to "FromWorksheet" and "ToWorksheet" methods seems *completely* asinine.

      "I need build Skynet. Plz send code"

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      This is by design, as shown by this quote from the C# spec: "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". (source[^]). I don't really understand why you can't cast from an interface, but there you have it. Having something that looks like a cast that actually switches to a completely different COM service is probably a bad thing anyway, though. When the conversion is so simple I recommend you just use 'new ExcelWorksheet(...)' and 'worksheet._worksheet' inline (well okay that last one should probably use a read property not the field directly).

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups