• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

How to compile only certain part of the code in .net.

Hi,
      I have a C# application in which i am using a Interop COM Dll properties of which are slightly different in two versions.I want  to have a single project  for this application which should support both version.But i now the version only at runtime. So i want to compile certain code based on the current version.How do i do that ?I tried with #if but could not succeed.Kindly help.
0
jisha123
Asked:
jisha123
  • 2
2 Solutions
 
Fernando SotoCommented:
Hi jisha123;

When the program starts up set a class level variable to indicate which version is to be run during the execution. This way when you come to a point when you want to call a function from the dll insert an if statement to determin which one to run, something like the following.

        private bool ver1;

        private void Form1_Load(object sender, EventArgs e)
        {
            // Run a check to see which version to run
            // ....
            if (version == 1)
            {
                ver1 = true;
            }
            else
            {
                ver1 = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ver1)
            {
                // Call the dll in version 1
            }
            else
            {
                // Call the dll in version 2
            }
        }

Fernando
0
 
existenz2Commented:
In this case, it's better to use reflection to call the correct dll. More information about reflection can be found here:
http://my.execpc.com/~gopalan/dotnet/reflection.html

or just search on reflection here on EE. Enough questions have been asked about it :)
0
 
jisha123Author Commented:
Hi,
   Thank for your suggestion.But in my case i will be adding COM dll reference at design time.There are certain properties or methods in each dll (each version) which are not supported in the other.So i cannot just write a
if {} else statement because , irrespective of version both code will compile and methods or properties which are not supported will through error while compling.Kindly help in this.
0
 
existenz2Commented:
Actually a combination from FernandoSoto's answer and mine would solve your problem i think.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now