TheSwamp

Code Red => .NET => ACAD with .NET8+ => Topic started by: 57gmc on January 05, 2024, 04:44:19 PM

Title: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: 57gmc on January 05, 2024, 04:44:19 PM
Ive been sick, so I had some time to watch some episodes from .NET conf. I found a nice overview of C#. Its a lot to learn if you haven't been keeping up with the progress of .NET because you were able to keep working on the AutoCAD api. This speaker came up with the idea of "everyday C#" (https://www.youtube.com/watch?v=xZk8T-9kI3w) (30 min), meaning that you may not use a lot of the new features and you can just add them as you have a need. That means that you don't have to spend a huge amount of time up front just to catch up before you can work in .NET 8. I thought that this was a balanced view. You can read up on the new features of C# here (https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12).

I used the Upgrade Assistant (https://learn.microsoft.com/en-us/dotnet/core/porting/upgrade-assistant-install) in VS. to upgrade my AutoCAD Project (without referencing the new api's) and there weren't any huge changes. At least, not from the .NET perspective. I still have to update my project to point to 2025's api's.
Post here if you have any other upgrading tips.
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: gile on January 07, 2024, 03:27:07 AM
We don't just need to learn what's new in C# 12, but everything that's new since C# 7 (see this history of C# (https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)), especially these (for me):
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: kdub_nz on January 07, 2024, 03:36:45 AM
I think guard clauses will be wirth a look.
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/diagnostics/guard
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: gile on January 07, 2024, 04:19:40 AM
Yes the Guard class looks interesting, but for the most current cases we can use, without the need of Community Toolkits extension, the ArgumentNullException.ThrowIfNull (https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull?view=net-8.0), ArgumentException.ThrowIfNullOrWhiteSpace (https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorempty?view=net-8.0), ArgumentOutOfRangeException.ThrowIfGreaterThan (https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifgreaterthan?view=net-8.0), ... methods.
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: 57gmc on January 08, 2024, 04:47:04 PM
We don't just need to learn what's new in C# 12, but everything that's new since C# 7 (see this history of C# (http://"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history")), especially these (for me):
  • Pattern matching
  • Nullable reference types
  • Records
  • Collection expressions
  • Lambda expression enhancements
Hi Gile, your link doesn't do anything. Perhaps you meant the History of C# (https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)? It is a subset of the link I posted. It just starts with version 12. I like your list. It's helpful to see what you thing is most useful. There's a lot of history to catch up on.
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: gile on January 09, 2024, 01:27:28 AM
Hi Gile, your link doesn't do anything. Perhaps you meant the History of C# (https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)?
That's it. I repaired the link.
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: kdub_nz on January 09, 2024, 02:29:30 AM
Gilles, Post Reply #3 has broken links too.
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: gile on January 09, 2024, 03:45:31 AM
Gilles, Post Reply #3 has broken links too.
Thanks for reporting, repaired.
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: Atook on January 09, 2024, 03:48:10 PM
Any recommendations for structuring a VS Solution for the transition to .NET 8?

Can we use the new C# features and maintain backwards compatibility with Framework?
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: 57gmc on January 09, 2024, 03:58:32 PM
No, the .NET Framework is no longer supported. AutoCAD 2025 will use .NET 8. There shouldn't be much to restructure though. Use the Migration Assistant that I mentioned in the first post. If you need to maintain compatibility with older versions of acad, I recommend keeping the 2024 version too. The Migration Assitant can upgrade your solution in place or upgrade to a copy. Choose the latter to retain the 2024 version. Depending on what features you are using, you might be able to keep just one solution and compile to different dll's using compile directives. But in the end, you will still need 2 dll's. One for 2024 and lower and one for 2025.
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: kdub_nz on January 09, 2024, 04:01:27 PM
Hi Tim,

Any recommendations for structuring a VS Solution for the transition to .NET 8?

There is little difference to currently, in principle. The differences are in the details of language and API references.
Do you have access to the Beta site ? I'm wary of posting specifics ( perhaps unnecessarily so )


Quote
Can we use the new C# features and maintain backwards compatibility with Framework?

Essentially No.

.NET 8+  and Framework 4.8+ are different beasts.
Pre ac2025 requires Framework,
 ac2025=> requires  .NET


added:
backup and archive skills are recommended  :wink:
Initially we don't 'need' to use the C#12 new functionality. ( just use C#12 compiler )
The post by 57gmc covers the essential process.

Regards,
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: Atook on January 09, 2024, 11:51:19 PM
There is little difference to currently, in principle. The differences are in the details of language and API references.
Do you have access to the Beta site ? I'm wary of posting specifics ( perhaps unnecessarily so )

I do have access, just been head down working on other things. Looking forward to the update though, fingers crossed it's pretty smooth.
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: MickD on January 10, 2024, 01:10:54 AM
In my experience working with web tech (ASP.net) moving from Framework to Core was mainly library references, there was only very minor API changes, you just have to know where things got moved to but Intellisense and the compiler help you out here.
They have essentially moved MS specific stuff (WinForms for example) into new/different lib's but the .Net stuff (i.e. C# and others) is now cross platform bringing it more in line with Java.

As kdub_nz pointed out, good old C# the language will still 'just work', no need to stress about the language update :)
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: huiz on January 10, 2024, 02:51:50 AM
There is little difference to currently, in principle. The differences are in the details of language and API references.
Do you have access to the Beta site ? I'm wary of posting specifics ( perhaps unnecessarily so )

I do have access, just been head down working on other things. Looking forward to the update though, fingers crossed it's pretty smooth.


I shared a document with my experiences, might be helpful. You can find the document in this location:


AutoCAD Customer Council > User Forums > ObjectARX API > First experiences with porting code to AutoCAD 2025 and .NET8
Title: Re: Migrating from C# and .NET Framework to C# 12 and .NET 8
Post by: retsameht on March 12, 2024, 04:10:58 AM
Yes the Guard class looks interesting, but for the most current cases we can use, without the need of Community Toolkits extension, the ArgumentNullException.ThrowIfNull (https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull?view=net-8.0), ArgumentException.ThrowIfNullOrWhiteSpace (https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorempty?view=net-8.0), ArgumentOutOfRangeException.ThrowIfGreaterThan (https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifgreaterthan?view=net-8.0), ... methods.

Both the ExceptionXxx.ThrowIfXxxx() methods and the Guard class rely on the CallerArgumentExpression Attribute  (https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerargumentexpressionattribute?view=net-8.0)to communicate better.

I adapted it into my existing RuntimeExtensions

Code - C#: [Select]
  1.    public static class RuntimeExtensions
  2.    {
  3.  
  4.       public static void ThrowIf(this AcRx.ErrorStatus es,
  5.          bool condition,
  6.          [CallerArgumentExpression("condition")] string message = null)
  7.       {
  8.          if(condition)
  9.             throw new AxRx.Exception(es, message);
  10.       }
  11.    }
  12.    
  13.    public static class Test()
  14.    {
  15.       void Run()
  16.       {
  17.          Document doc = Application.DocumentManager.MdiActiveDocument;
  18.          ErrorStatus.NoDocument.ThrowIf(doc is null);
  19.          blah blah blah
  20.       }
  21.        
  22.    }
  23.  
  24.  

If the above Run() throws, the exception message will contain "doc is null".