0

i.e. in PHP, you can build a library of your methods in one file, and error is only given, if there are problems in execution (not in compiler). I wonder if something like that is possible in C#, for example, I could put dedicated methods for both .NET 3.5 and 4.5 in same file:

//myFile.cs

public void mymethod_for_v35(object profile)

public async void mymethod_for_v45(dynamic profile)

so, I could include myfile.cs in all projects (whether targeting 3.5 or 4.5) and when I am targeting 3.5, in application I will only call first method . However, as 2nd method is for 4.5 (and 3.5 net compilers dont understand that), we still get compilation errors in IDE.

Does there exist any workaround or Flag, to allow the existence of that method (even though it's unsupported in current .NET version of project) ?

5
  • You would probably need to have different versions of your assembly, one for each version of .Net. But unless it does something fundamentally different you could just use the 3.5 version in 4.5 Commented Mar 13, 2018 at 11:42
  • @phuzi I am trying to avoid different assemblies... Commented Mar 13, 2018 at 11:43
  • 1
    Use conditional compilation symbols. For .NET 3.5 project define some symbol (like "NET35"), for .NET 4.5 project define another ("NET45"). Then use #if NET35 directives and so on. Commented Mar 13, 2018 at 11:45
  • 1
    Some constructs in newer versions of the language can't even be compiled for lower versions, due to missing supporting classes, so in general it would not be possible to produce one assembly that works correctly everywhere and still supports newer features (without resorting to things like runtime code generation). async/await is definitely one of those, as is dynamic. You can have one code base (with either conditional compilation or conditional includes in the project file), but having one binary is not a realistic goal in this case. Commented Mar 13, 2018 at 11:48
  • @JeroenMostert You could submit as answer, i liked it. Commented Mar 13, 2018 at 12:04

1 Answer 1

4

The most convenient way to achieve this is to use a multi-targeted library via the new SDK project syntax (CSP); start by creating a .NET Standard library, then change the <TargetFramework> to <TargetFrameworks>, simply semi-colon delimiting the frameworks you need to support separately, for example:

<TargetFrameworks>net40;netstandard1.3;netstandard2.0</TargetFrameworks>

(note that other frameworks will be implicitly supported - so if I consume it from a net462 project, the net40 build will be used)

Now add #if checks where needed, for example:

#if NET40
...
#endif 

or

#if NETSTANDARD1_3
...
#endif

(note that . is mapped to _)

This means that the appropriate API surface will be surfaced automatically.

If you need to use different references for different target frameworks, that can also be done in the project file via a condition on an item-group:

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="..."/> <!-- whatever you need here -->
</ItemGroup>

At build, each target-framework will be evaluated (including the conditions) and built separately, then bundled together when creating a nupkg.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.