Changing an object property in C# for multiple versions of .NET -


i have c# project used part of visual studio extension.

to support earlier versions of vs, project set target framework .net framework 3.5.

the project makes reference system.servicemodel.

depending on version of visual studio running, different version of system.servicemodel used. vs2008 use .net 3.5 version dll, while vs2012 use .net 4.5 version @ runtime, regardless of project target framework.

my problem property added httptransportbindingelement in .net 4, called decompressionenabled. because target .net 3.5, cannot compile changes property; however, need change value.

the work around using change property @ run time, 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, wondering if there better design pattern handling this, without need reflection.

see: detect target framework version @ compile time

    public static void disabledecompression(this httptransportbindingelement bindingelement)     { #if running_on_4         bindingelement.decompressionenabled = false; #endif     } 

once build set release .net 3.5 references disabledecompression optimized out.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -