I have some shared assemblies/projects that are used within Winforms apps, windows services and now Azure worker roles.
Is there any way that I can detect at runtime
Like you say, adding references to all your end-products is not the way to go. I would say this is a problem solved very easily using Dependency Injection.
Define an interface which yields this information (in a shared assembly):
public enum DeploymentType { WinForms, WinServices, Azure }
public interface IWhatDeploymentAmIUsing {
DeploymentType DeploymentType { get; }
}
And create a class that implements this interface.
WinForms (in your winforms project):
public class WinFormDeploymentType : IWhatDeploymentAmIUsing {
public DeploymentType DeploymentType { get { return DeploymentType.WinForms; } }
}
WinServices (in your windows service project):
public class WinServicesDeploymentType : IWhatDeploymentAmIUsing {
public DeploymentType DeploymentType { get { return DeploymentType.WinServices; } }
}
Azure (in your azure project):
public class AzureDeploymentType : IWhatDeploymentAmIUsing {
public DeploymentType DeploymentType { get { return DeploymentType.Azure; } }
}
Now wire it up using your favorite DI tool.