Seriously? user-defined conversions to or from an interface are not allowed
-
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"
-
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"
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).