Using C# 10 with .NET 4.7 Framework

Using C# 10 with .NET 4.7 Framework

Gepaha
Collaborator Collaborator
968 Views
9 Replies
Message 1 of 10

Using C# 10 with .NET 4.7 Framework

Gepaha
Collaborator
Collaborator

This question is more directed to @ActivistInvestor and of course answer if you want. Anyone else who responds is always welcome and I thank you all very much.

 

I'm trying to use the AcMgdLib library from @ActivistInvestor (thanks so much for this and so many other solutions). I followed the instructions at https://github.com/ActivistInvestor/AcMgdLib  and when I went to test I received a series of errors. Based on research on the internet, I tried to reduce the number of errors by adding packages using PolySharp, Mirosoft.BCL.HashCode, System.Memory, etc. Some problems have been resolved but there are still errors.

 

I use Visual Studio 2022, AutoCAD 2020 and .NET 4.7 Framework and added the instructions in the “AcMgdLib.csproj” file:

<PropertyGroup>
   ...
   <TargetFramework>net4.7</TargetFramework>
   <LangVersion>10</LangVersion>
   <ImplicitUsings>disable</ImplicitUsings>
   <Nullable>disable</Nullable>
   ...
</PropertyGroup>


Reading the text at https://github.com/ActivistInvestor/AcMgdLib  it seems that there shouldn't be all these compilation errors and it shouldn't be necessary to use NuGet packages.

 

Errors resolved with PolySharp:

  • The type or namespace name 'CallerArgumentExpressionAttribute' could not be found (are you missing a using directive or an assembly reference?)
  • The type or namespace name 'CallerArgumentExpression' could not be found (are you missing a using directive or an assembly reference?)

Unresolved errors:

  • Operator '>' cannot be applied to operands of type 'IntPtr' and 'int'
  • The name 'CollectionsMarshal' does not exist in the current context
  • Argument 1: cannot convert from 'char' to 'string'
  • The type or namespace name 'Span<>' could not be found (are you missing a using directive or an assembly reference?)
  • 'Span<double>' does not contain a definition for 'Sort' and no accessible extension method 'Sort' accepting a first argument of type 'Span<double>' could be found (are you missing a using directive or an assembly reference?)
  • 'nint' does not contain a constructor that takes 1 arguments
  • Operator '>' cannot be applied to operands of type 'IntPtr' and 'int'

Before posting here, I invested some time looking for a solution but made no progress. So, If @ActivistInvestor or anyone else can help I would be grateful.

0 Likes
969 Views
9 Replies
Replies (9)
Message 2 of 10

ActivistInvestor
Mentor
Mentor

Sorry, I have a pretty long to-do list with AcMgdLib, that requires adding some #ifdefs to the code to make it buildable with .NET 4.8.  There may also be some classes that will not work at all with .NET 4.7, and I've yet to identify those.

 

For Span<T>, the only solution is to install the System.Memory package for .NET 4.7.

 

For CollectionsMarshal, there is no package for .NET 4.7, so it will require a proxy class with #ifdef's, and that's only for using the AsSpan() method to get a span from a List<T>.  Not sure how that can be handled for .NET 4.7 yet, but I will have a look at it ASAP.

 

I opened the discussions area for this library which you can find here, where you can post issues. I'm a bit busy with other things right now, but will try to get to testing a .NET 4.7 build as soon as I can and resolve whatever issues can be resolved. In the mean time, if you can post compiler output showing files and line numbers of the errors you cited above in this discussion, I will prioritize those issues.

0 Likes
Message 3 of 10

Gepaha
Collaborator
Collaborator

Thank you very much for the answer.
As much as possible, I will try to post errors in the repo's  discussions.

0 Likes
Message 4 of 10

18348401357
Enthusiast
Enthusiast

just use net48 , net48(not net4.8) can work on autocad2013-2024
You should install the sdk for Microsoft net8 to use the new features on the framework

like this

18348401357_0-1737513766806.png

 

0 Likes
Message 5 of 10

Gepaha
Collaborator
Collaborator

Thank you very much for your reply.
I use Visual Studio 2022 Community, AurtoCAD 2020, .NET 4.7 Framework.
Could you provide more details on how I should proceed, creating a new project from scratch so that it compiles using C# 10?

0 Likes
Message 6 of 10

ActivistInvestor
Mentor
Mentor

Does editing the project file and editing the property group as shown in your initial post not work?

0 Likes
Message 7 of 10

Gepaha
Collaborator
Collaborator

It didn't work for me, it generated several errors.
I'm using Visual Studio 2022 Community, project file is attached.

Error ListError List

0 Likes
Message 8 of 10

ActivistInvestor
Mentor
Mentor

I thought you were talking about creating a new/empty project using .NET 4.7 and setting the language to C#10.0, without referencing AcMgdLib.  

 

The way to consume AcMgdLib and avoid the language version issue altogether is to build AcMgdLib itself as a class library and reference it into projects where it's used. However, until I can resolve the issues with .NET 4.8 I don't think that's going to work.

0 Likes
Message 9 of 10

18348401357
Enthusiast
Enthusiast

<LangVersion>preview</LangVersion>

 

0 Likes
Message 10 of 10

ActivistInvestor
Mentor
Mentor

Just an update.

 

Multi-targeting .net 4.8 and .net 8.0 AcMdgLib is going to involve a major effort, that requires a lot of existing code to be modified using conditional compilation.

 

Here is a few of hundreds of examples, of what must be done in that ongoing effort. Notice the #if/#else/#endif, which the code base is becoming littered with.

 

#if NET8_0_OR_GREATER
   record Box
#else
   class Box
#endif
   {
      public Box(int value = 1) { Value = value; }
      public int Value;

      public static Box operator ++(Box box)
      {
         box.Value++;
         return box;
      }

      public static Box operator --(Box box)
      {
         box.Value--;
         return box;
      }

      public static implicit operator int(Box box)
      {
         return box.Value;
      }
   }

 

      public bool Equals(Database a, Database b)
      {
         if(a is null)
            return b is null;
         if(b is null)
            return false;
         if(ReferenceEquals(a, b)) 
            return true;
#if NET8_0_OR_GREATER    // e.g., AutoCAD 2025+
         return a.RuntimeId() == b.RuntimeId();         // fixed
#else
         return a.UnmanagedObject == b.UnmanagedObject; // can't be fixed
#endif
      }

 

0 Likes