-1

Is there a way to detect the calling platform in C#?

In my scenario OnPropertyChanged should only be called if the platform is WPF. Our ASP.NET application should not use this method.

Stefan Schmid
  • 1,012
  • 10
  • 28
  • Why not make 2 build targets and incorporate `#if`? Checking against hosts is quite complex (there's also WinForms, Silverlight, a service, self-hosted Web, IIS hosted Web, ...) if you need different behaviours for each one ... –  Nov 12 '14 at 12:17

2 Answers2

2

The way you have phrased your question makes it sound like you have a code smell.

If you are using common data objects, then they should not discriminate between the platforms that use them. OnPropertyChanged normally just raises the PropertyChanged event - nothing more, and the consuming platform shouldn't care. If the consuming platform hasn't registered a handler for the event then it will be oblivious to the raising of the event.

One possibility to consider is that if the data entities are compiled as part of the calling platform (i.e. they are not in a common assembly referenced by both) then you could use compile time #defines.

slugster
  • 49,403
  • 14
  • 95
  • 145
  • We have DTOs which are used by our ASP.NET application and our WPF application. Behind the DTOs are normal entities, which are not used by the UIs. According to: http://stackoverflow.com/questions/19153987/asp-net-inotifypropertychanged-on-domain-objects Our DTOs are UI-Models and the entities are Domain-Models. I'm just afraid that calling `OnPropertyChanged` could have a negative performance impact for our ASP.NET application. – Stefan Schmid Nov 12 '14 at 12:22
  • 3
    @StefanSchmid That's premature optimization, which should be avoided. Benchmark if needed, otherwise care at the very last moment (= when it gets a problem) - YAGNI and KISS! –  Nov 12 '14 at 12:24
  • 1
    @StefanSchmid The impact will be negligible if nothing is listening for the event. The normal pattern for raising an event is to get the [invocation list for the event and invoke the delegates](http://stackoverflow.com/a/18027161/109702) if there are any - no delegates means no invoking, which means low performance overhead. – slugster Nov 12 '14 at 12:27
1

You can check the HttpRuntime.AppDomainAppId Property:

if(HttpRuntime.AppDomainAppId != null)
{
  //if it's not null it is a web app
}
else
{
  //if it's null it is a desktop app
}
Mark
  • 3,273
  • 2
  • 36
  • 54