10

I have an old line of c# code that looks basically like this:

foo.set_Parent(parent);

It has compiled fine for years. Now in VS2015 I get the error:

CS0571 'Foo.Parent.set': cannot explicitly call operator or accessor

So I can rewrite the line as:

foo.Parent=parent;

This builds fine in VS2015, but in VS2013 it gives the error:

'Foo.Parent' is not supported by the language; try directly calling accessor methods 'Foo.get_Parent()' or Foo.set_Parent(Foo)'

So the simple fix is to simply ifdef these two lines based upon which version of the compiler is running. But how do you detect which version of the compiler is executing?

And for the record, no, I can't just dictate that everyone on the team simultaneously upgrades to VS2015.

Additional info - For everyone smelling a rat, I'll go ahead and drag out the ugly truth, although I don't think it will change much of anything. The class Foo is from an ancient Borland assembly that is all bound up in Delphi (and yes, we're migrating away but not there yet). So the actual code, that compiles up to VS2013, looks like this:

using Borland.Vcl;
using RepGen;
using SnapReportsForm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace MigrantCOM {
   [ComVisible(true)]
   [Guid("48245BA3-736B-4F98-BDC5-AD86F77E39F4")]
   [ProgId("MigrantCOM.Exports")]
   [ClassInterface(ClassInterfaceType.AutoDual)]
          public class MigrantCLRExports {   // : MarshalByRefObject 
      public string Test(string s) { return s+s; }
   }

   [ComVisible(true)]
   [Guid("1154D364-B588-4C31-88B9-141072303117")]
   [ProgId("MigrantCOM.SnapRepCOM")]
   [ClassInterface(ClassInterfaceType.AutoDual)]
   public class SnapRepCOM {
      TRepGen repGen;
      TStringList snapRefs=new TStringList();
      TForm parent=new TForm(null);
      TMemo designerMemo;
      List<TReference> references=new List<TReference>();
      TRunAsSnapContext runAsSnapContext=new TRunAsSnapContext();

      public SnapRepCOM() {
         designerMemo=new TMemo(parent); designerMemo.set_Parent(parent);
         ...
      }

So the class being instantiated is Borland.Vcl.TMemo which is part of the old Delphi assembly.

Kevin Donn
  • 528
  • 1
  • 4
  • 12
  • 8
    That looks *very* strange to me. You've never been able to call an accessor directly as far as I'm aware. Can you provide a short but complete example demonstrating the problem? – Jon Skeet Dec 16 '15 at 16:43
  • If all else fails, I imagine reflection could be used as a workaround. – j.i.h. Dec 16 '15 at 16:44
  • foo.Parent = parent should work in any version of c#/.Net... Can you post an example of what foo.Parent is "More Code". – Ryan Mann Dec 16 '15 at 16:48
  • Couldn't you use VS 2015 with an older version of the C# language, so it works with the new tool but with the same code as before? – ken2k Dec 16 '15 at 16:50
  • 1
    Is this question helpful? http://stackoverflow.com/questions/3436526/detect-target-framework-version-at-compile-time – ZeroPhase Dec 16 '15 at 16:51
  • @ZeroPhase From what I understand, it's more about the _language_ version than the _runtime_ version – ken2k Dec 16 '15 at 16:52
  • 1
    Your issue is with the code and being abstracted from you with compiler errors. Post a complete example and we'll provide some aid. – Ryan Mann Dec 16 '15 at 16:53
  • 1
    http://stackoverflow.com/questions/3436526/detect-target-framework-version-at-compile-time – skalinkin Dec 16 '15 at 17:02
  • @ken2k How do you control which version of the C# language is to be used? – Kevin Donn Dec 16 '15 at 17:26
  • 1
    You are asking for the wrong fix. The underlying problem is the interop library you use. Roslyn does have known trouble with libraries that were generated by .NET tooling before 4.0, might be related. Running Tlbimp.exe version 4 or greater should be the real fix. – Hans Passant Dec 16 '15 at 17:47
  • @Hans-Passant - I was afraid all the COM stuff might muddy the water here. Bear in mind that this code isn't attempting to *use* a COM library. It's *building* a COM library. The Delphi assembly being referenced isn't being used as a COM server. It's just a regular old CLR 2.0 assembly. Plus the project being built isn't even a CLR 4.0 project - it's a .NET 3.5 project. So I'm not sure whether tlbimp is appropriate. We're going to have to decide whether to delay VS2015 or all move at once. – Kevin Donn Dec 16 '15 at 20:28

1 Answers1

1

I'm leaving this as an answer, linking an image will fit better here than in a comment.

So if you want to use VS 2015 but still use the same good ol' version of the C# language that worked for years, you can configure your project to target a specific version:

enter image description here

This adds <LangVersion>5</LangVersion> in the csproj.

Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176