I have a C# project that is used as part of a Visual Studio Extension.
To support earlier versions of VS, the project set to Target framework
.NET Framework 3.5.
The project makes a reference to System.ServiceModel
.
Depending on which version of Visual Studio is running, a different version of System.ServiceModel
will be used. VS2008 will use the .NET 3.5 version DLL, while VS2012 will use the .NET 4.5 version at runtime, regardless of the project target framework.
My problem is that a Property was added to HttpTransportBindingElement in .NET 4, called DecompressionEnabled. Because I target .NET 3.5, I cannot compile with changes to this property; however, I do need to change its value.
The work around I am using to change the property at run time, is to use reflection:
public static void DisableDecompression(this HttpTransportBindingElement bindingElement)
{
var prop = bindingElement.GetType()
.GetProperty("DecompressionEnabled",
BindingFlags.Public | BindingFlags.Instance);
if (null != prop && prop.CanWrite)
{
prop.SetValue(bindingElement, false, null);
}
}
The solution works, but I am wondering if there is a better design pattern for handling this, without the need for reflection.