question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Framework defines can easily break code

See original GitHub issue

With #309 we’ve added an implicit define based on the target framework, which is super useful for customers when using conditional compilation.

Unfortunately, this makes it extremely easy to write code that breaks during retargeting. Let’s say I’m in a .NET Standard project and I’d like to add a code path that uses some new feature in .NET Standard 2.1, so I multi-target for .NET Standard 2.0 and .NET Standard 2.1. The code would look as follows:

public void SomeMethod()
{
    #if NETSTANDARD2_1
        // Write some code that uses Span<T>
    #else
        // Write fallback logic
    #endif
}

Fast forward a year. Now I’d like to add some logic that can light-up on .NET Standard 2.2. So in a different area in my code I’m writing this:

public void SomeOtherMethod()
{
    #if NETSTANDARD2_2
        // Write some code that uses some new feature in .NET Standard 2.2
    #else
        // Write fallback logic
    #endif
}

The code will compile just fine and everything looks dandy until you realize that your .NET Standard 2.1 binary no longer uses Span<T> in SomeMethod() but emitted the fallback logic.

Ideally, we’d want to write code like this:

public void SomeMethod()
{
    #if NETSTANDARD >= 2.1
        // Write some code that uses Span<T>
    #else
        // Write fallback logic
    #endif
}

Which C# doesn’t support (and likely never will). However, we could instead change the SDK to define more symbols, such as:

public void SomeMethod()
{
    #if NETSTANDARD2_1_OR_HIGHER
        // Write some code that uses Span<T>
    #else
        // Write fallback logic
    #endif
}

The _OR_HIGHER symbols would all be defined.

Thoughts?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
terrajobstcommented, Apr 3, 2019

If we’re pushing for a C# change it’s worth pointing out numeric symbols won’t work for cases where we have three digit version numbers (net > 4.5.1). MSBuild has this problem today where two digits end up being compared how you would expect while three digits are ending up being a string comparison and thus 4.5.10 isn’t considered larger than 4.5.9. Oops.

2reactions
danmoseleycommented, Mar 18, 2021

$([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '3.1')) should work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is a Framework?
A framework is a structure that you can build software on. It serves as a foundation, so you're not starting entirely from scratch....
Read more >
The Difference Between a Framework and a Library
Both libraries and frameworks are reusable code written by someone else. Their purpose is to help you solve common problems in easier ways....
Read more >
Top 10 Most Common Spring Framework Mistakes
Common Mistake #3: Lacking Separation of Concerns. As your application grows, code organization increasingly starts becoming an ever more important matter.
Read more >
What is a Framework? Why We Use Software Frameworks
A framework, or software framework, is a platform that provides a foundation for developing software applications. Think of it as a template of...
Read more >
Common Language Runtime (CLR) overview - .NET
The common language runtime makes it easy to design components and applications whose objects interact across languages. Objects written in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found