We have a solution that compiles perfectly in VS2013 but fails with a compilation error in VS2015.
The problem has been narrowed down to this:
We have a c# project A which defines a generic struct like this:
public struct MyStruct<T>
{
public MyStruct(T b)
{
}
}
We have a c++/cli project B which defines a function like this:
public ref class Class1
{
public:
void BadMethod(MyStruct<int> ^){};
};
And finally I have a c# command line project C referencing Project A and B and trying to call BadMethod:
class Program
{
static void Main(string[] args)
{
var c = new Class1();
var s = new MyStruct<int>(0);
c.BadMethod(s);
}
}
In Visual Studio 2013 (and older) this compiles without any issues, in Visual Studio 2015 however we get:
application\Program.cs(18,15,18,24): error CS0570: 'Class1.BadMethod(?)' is not supported by the language
I have tried compiling the c++/cli project using the Visual Studio 2015 toolset but the error remains.
Using a generic class instead of a struct seems to work.
void BadMethod(MyStruct<int> ^){};
Fairly remarkable how often a new VS version gets blamed for a programming bug. The MyStruct type is a value type. But yet you declared the argument type as a reference type variable. Note the ^ hat you used.
Somewhat unfortunately, the C++/CLI compiler does support this. And so does the CLR, the value must be boxed first before it can be passed to the method call. Very inefficient, the point of using value types is always to avoid boxing. There is however no type annotation available in the CLR for such a boxed type, it becomes System::ValueType in the metadata. With a runtime check that the value is of the expected type. More inefficiency, this adds up to big bucks.
The C# language does not support this at all and spits bullets at having to box the value. Without any way to check that it is the required value type, it takes static type checking very seriously.
The fix is very simple, just omit ^ so the argument is passed by value and doesn't have to be boxed. If you meant to pass the value by reference, not clear from the question, then you must use % instead.
And probably a good idea to check the rest of your code, C++/CLI makes it far too easy to fumble this and lose gobs of perf without any peep from the compiler. Apply the simple rule, variables of a value type must never have the hat. Variables of a reference type should always have the hat unless stack semantics (using statement in C#) is intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With