BenchmarkDotNet - Benchmark Multi-Targeted Project - benchmarkdotnet

I have a project which targets net35, net40, net45 and netstandard2.0. (https://weblog.west-wind.com/posts/2017/Jun/22/MultiTargeting-and-Porting-a-NET-Library-to-NET-Core-20). I would like to benchmark each version of dotnet I am targeting to ensure the polyfills I have created for missing features in older versions of .Net perform decently in comparison to the dot net feature. For example net35 does not have any of the types in the System.Collections.Concurrent namespace. Is it possible to do this type of benchmarking with BenchmarkDotNet, and if so how?
new CachingActivatorFactory(
#if NET35
new ReaderWriterCache<ConstructorInfo, Func<object[], object>>(),
#else
new ConcurrentCache<ConstructorInfo, Func<object[],object>>(),
#endif
new LambdaExpressionActivatorFactory(
#if NET45 || NETSTANDARD
new IlEmitCompiler()
#else
new SystemExpressionCompiler()
#endif
)
)

Well given what each version of benchmarkdotnet supports you will be constrained to that.
The current version 0.11.5 supports .net standard 2.0 which will support .net core > 2.0 and .net framework > 4.6.1 so those would be the target you can test.
So for instance with this I could target the benchmark project to net461 and netcoreapp2.2. You also included .net standard in your list, you cannot target that though as that is not a runtime. In any case you would have to set your project target as:
<TargetFrameworks>netcoreapp2.2;net461;</TargetFrameworks>
Then you decorate your test class with:
[CoreJob, ClrJob]
There is a work around however to being constrained to what the benchmarkdotnet library supports shows in the FAQ.
Q My source code targets old versions of .NET Framework or .NET Core, but BenchmarkDotNet requires net461 and netcoreapp2.0. How can I
run benchmarks in this case?
A It's a good practice to introduce an additional console application (e.g. MyAwesomeLibrary.Benchmarks) which will depend on
your code and BenchmarkDotNet. Due to the fact that users usually run
benchmarks in a develop environment and don't distribute benchmarks
for users, it shouldn't be a problem.

Related

How do your write Xamarin platform specific code in a .net standard library?

How do your write Xamarin platform specific code in a .net standard library?
I want to use namespaces like Xamarin.Forms.Platform.iOS in a .Net Standard Library..
Use case: I want to develop a .net library for my apps which includes a video player for the various platforms. This video player also has to interact with other code in the .net library.
Or is the answer I need to use a shared project or portable library?
You should not include platform specific code or use namespaces like the one you mentioned in your .NET Standard Library, the reason is that .NET is just a runtime environment.
It’s not the the main runtime environment that you would use on iOS or Android. These platforms use Mono - not .NET. Check this or this to see more details for how its structured.
If you need to execute something from your .NET Standard project which is related to platform-specific behavior, use Dependency Injection or Custom Renderers.

What are the equivalent classes for ContractClass and ContractClassFor in .Net standard Library 1.2

I am converting my .Net 4.5 class library to .Net Standard Library 1.2 (as we want to support Cross platform.
Few of my classes and interfaces use attributes ContractClassFor and ContractClass. These classes belongs to System.Diagnostics.Contracts. But I can not find thess classes in XAMARIN.
What are the equivalent classes for those I can use? Or if no classes present, how can I replace the code with ?
Thanks so much
If you are using .NETStandard, you need to use NuGet packages for any missing namespaces. In your case you would need to install the System.Diagnostics.Contracts NuGet to unlock the desired functionality.
As the NuGet description says, it provides the ContractClass and ContractClassFor attributes you are missing.

Recommended Cross-Platform (Windows and Android) Project Set Up using OpenTK

I am starting to develop a scientific software that I hope I will be able to run on multiple platforms. My plan is to use OpenTK for the rendering of the scientific models and plots. As of the moment I have a prototype that runs on Windows using OpenTK 1.1 libraries from http://www.opentk.com/ (a simpler version just with OpenTK and a more complicated one with OpenTK + WindwosForms). I am trying to port that prototype to Android.
It seems that the syntax using by the Xamarin.Android OpenTK library is nearly identical to the one that I am currently using for Windows (with the only difference that OpenGL -> OpenGL ES and GameWindow -> AndroidGameView) so the porting shouldn't be an issue. However, I was hoping that I could avoid a copy-paste method and get a more permanent solution having a shared OpenTK code between the Windows and the Android version.
I have read trough the Xamarin documentation about the shared vs PCL methods for cross-platform development. However, I still struggle to figure out how to set-up a Visual Studio solution with an Android and Windows project and a shared code that will include OpenTK. Is that even possible and can someone give me an example of how to do it? I did explore an example I found for rendering a rotating cube using OpenTK for a shared Android/iOS project (http://developer.xamarin.com/content/TexturedCubeES30/) but in my case I need to use a different OpenTK library for the Windows and for the Android project.
I also found this Do the Android and iOS versions of OpenTK have the same API? discussion. It is very similar to what I would like to do but in my case I am trying to setup a project for Windows and Android (for now).
Can I use only one OpenTK library (which one?) that is being called from both the Android and the Windows project and what will be the right way to set-up both projects so they share the same OpenTK code. This is the first time I am dealing with writing a cross-platform code so I am a bit lost.
Edit: I was able to get a prototype running using Shared Xamarin project and compiler flags as proposed below. Code was indeed not very pretty at places but I got over 70% code re-usability between the two platforms so it was worth the effort. This is how I used the compiler flags in case someone is looking for the same thing (credit to SKall from the Xamarin forums):
#if __ANDROID__
using OpenTK.Graphics.ES11;
#else
using OpenTK.Graphics.OpenGL;
#endif
I used the #if syntax similarly where there were small differences between the syntax of the routines.
It does not seem like OpenTK has its logic inside of a PCL in the first place, so your plans on putting it there are going to get hard to achieve.
However, if you split out your code, such that most of it is contained in classes, which are not highly dependent on the underlying platform, you will be able to create a Class Library Project for each platform and link your files between the platform specific projects. Inside of the classes it contains you will use #if definitions to choose whether to use AndroidGameView or GameWindow and the same goes for other platform specific types. It will make the code ugly, but this is the alternative to PCL.
You could try to see how much of the OpenTK code compiles inside of a PCL and inject the platform specific stuff at runtime, but it will require considerably more work from you. However, it will make the code a lot more cleaner to look at.
To ease the file linking, you could make one of those Shared Projects and chuck in all of the logic in there.
Some more info about code sharing here: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/
Dependency injection: https://en.wikipedia.org/wiki/Dependency_injection

LinqBridge Breaks Razor Views: .NET Version-Based File Output Restriction

Our project uses several NuGet packages, a few of which reference LinqBridge, a library that re-implements LINQ to Objects for C# 2.0. The LinqBridge.dll file lives under /packages/PackageName/lib/20/LinqBridge.dll, so it clearly is supposed to only apply to .NET 2.0.
The problem is that, even though every project in the solution is configured to build to .NET 4.0, the LinqBridge.dll binary gets copied over to the final /bin directory and wreaks havoc in Razor views. If I perform .Select() on an IEnumerable, there is an ambiguous call between the built-in LINQ call and the re-implemented one that LinqBridge provides.
I clearly do not need the re-implemented version; if I simply delete LinqBridge.dll from the output /bin directory, everything works just fine. However, that is not an acceptable permanent solution.
Is there any way I can configure something to quit copying that file, which is for an old .NET version, into the /bin output?
Edit: I duct-taped together a solution by adding this to the "Post-build event command line:" commands under "Build Events" in my solution properties:
del $(SolutionDir)\bin\LinqBridge.dll
It's still far from ideal, but at least it lets my project run for now.
NuGet has support for different binaries for different .NET versions so I would suggest that the packages you are using are built badly.
I would contact the authors of the packages and see if they can fix them so that only the net11 or net20 versions include LinqBridge.
Supporting Multiple .NET Framework Versions and Profiles
Many libraries target a specific version of the .NET Framework. For example, you might have one version of your library that's specific to Silverlight, and another version of the same library that takes advantage of .NET Framework 4 features. You do not need to create separate packages for each of these versions. NuGet supports putting multiple versions of the same library in a single package keeping them in separate folders within the package.
(more...)
A useful approach we found was using the LinqBridge.Embedded Nuget package instead of the standard LinqBridge package. This embeds Linqbridge as a C# file within your project, and hence does not get copied over to the bin folder and loaded into the context of the Razor view.
This was useful to us because an assembly we reference still needs to be built in .Net 2.0, as it is also referenced by a 2.0 application. Hence that assembly uses LinqBridge.Embedded, and the LinqBridge assembly does not end up in our 4.0 servers' bin folders.

Single code base for monodroid, WP7 and silverlight

I've been playing about with the monodroid (preview 8980) and I'm trying to create an application that will run on Android, WP7 and silverlight.
My plan is to create a single core class library and then a seperate project for each of the platforms that will contain the UI stuff - so one monodroid project, one for WP7 and a SL one, all of which will reference the core class library.
The main problem with this is that it will be possible to implement some functionality in the core library that will work fine on silverlight, but not on WP7 for instance. I believe the best way to make sure this doesn't happen is by making the core library a silverlight 3 project, as this will be the lowest common denominator.
The problem I am facing now is that I can't reference a SL3 library from the monodroid project. I get this warning - 'Warning 2 The project 'TMCore' cannot be referenced. The referenced project is targeted to a different framework family (Silverlight)'
Any ideas?
Bah I seam to always end up answering my own questions - http://www.gregshackles.com/2010/12/shared-libraries-for-windows-phone-7-monodroid-and-beyond/
according to
http://monodroid.net/Documentation/Assemblies
you must compile your core-stuffe into a seperate assembly you cannot share a core-dll.
> Note: MonoDroid is not ABI compatible with existing assemblies compiled for
> a different profile. You must recompile your source code to generate
> assemblies targeting the MonoDroid profile (just as you need to recompile
> source code to target Silverlight and .NET 3.5 separately).

Resources