Note that there are some explanatory texts on larger screens.

plurals
  1. PODetecting if a class/type is defined prior to compiling
    text
    copied!<p>This is similar to a few other threads i have found, but I haven't found the answer I need yet. I would appreciate a direct answer, even if it is "no, you can't do that".</p> <p>Is there a way to use one block of code if a class/type exists and another if it doesn't. The result would be the same as using preprocessor directives but without the need to #define and manually comment or un-comment something in a file.</p> <p>This may be a special use-case. I'm not sure. I'm working in an environment where sets of files can be installed, or not, before anything is compiled. So someone could buy a plugin which gets "installed" (files added to the project) which makes classes/types available for use (like extending an API). I need to provide a workaround if someone doesn't have one of our other plugin packages. I hope that makes sense. </p> <p>It just wouldn't be user-friendly to ask someone to open up one of our files, if they have another plug-in, to un-comment a preprocessor directive, unless we have to.</p> <p>e.g. I know this doesn't work because it only tests boolean if #define is used, but it illustrates what I am trying to do...</p> <pre class="lang-cs prettyprint-override"><code>#if SomeType SomeType.DoSomething(); #else DefaultWay.DoSomething(); </code></pre> <p><strong>EDIT: I added this as a C# feature suggestion. Please vote here: <a href="http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2297494-add-type-testing-as-preprocessor-directive" rel="nofollow">http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2297494-add-type-testing-as-preprocessor-directive</a></strong></p> <p>I don't see how reflection would be able to do this, but I am new to C#, so examples using relection, if it is possible, would be great.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload