-2

I have 2 project. Nested project have 2 model: Model1in and Model1Out.

namespace Test.Nested
{
    public class Model1in
    {
#if NATIVE
        public static explicit operator Model1in(Model1Out model)
        {
            return model == null ? null : new Model1in();
        }
#endif
    }

    public class Model1Out
    {
#if NATIVE
        public static explicit operator Model1Out(Model1in model)
        {
            return model == null ? null : new Model1Out();
        }
#endif     
}

But at other project I want to convert object Model1in to Model1Out and back.

#define NATIVE

namespace Test.Native
{
     ....
     Model1Out model = (Model1Out)Model1in;
}

The compiler generates an error and does not recognize the model conversion block. Asks to implement. It turns out he just does not see the block #define NATIVE. What is wrong? I add reference to project Nested, Native use him and define constant at project settngs.

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DefineConstants>DEBUG;NATIVE</DefineConstants>
</PropertyGroup>

If i use this implement for constant at project Nested - no problems, but i have other project, where this block cannot be us and i wanna hidden implicit|explicit construction.

David Specht
  • 7,784
  • 1
  • 22
  • 30
Y K
  • 408
  • 1
  • 5
  • 21
  • "cannot be us" is a hopelessly vague constraint that has no sensible answer. Get ahead by not doing it this way at all, there's nothing wholesome about it anyway. Use normal runtime configuration and throw an exception when the conversion operator should not be used. – Hans Passant Feb 06 '20 at 22:39
  • @HansPassant i hope to use Define constant at nested project. I wanna to do more then 2 "nested" project with custom explicit function for everyone. – Y K Feb 10 '20 at 15:23
  • Very complicated thinking and may prone to hard to detect bugs. Nevertheless, not a bad attempt to understand something better. – jomegaA Feb 13 '20 at 20:48
  • Instead of complicated conversion you may write something trivial like a Debug.Write to see whether you reach #if block at all. @Riggy should deserve the bounty – jomegaA Feb 13 '20 at 20:49
  • @YauhenKavalenka :-) I understand your frustration. I personally liked the method shown by Riggy. All the best with your project. – jomegaA Feb 15 '20 at 16:53

1 Answers1

3

I don't completely understand your question. Are these the steps you are following?

  1. Compile Test.Nested without NATIVE defined, creating Test.Nested.dll
  2. Now in Test.Native, add a reference to Test.Nested.dll
  3. Try to compile Test.Native with NATIVE defined to create Test.Native.dll
  4. Receive a compiler error that operator Model1Out() is undefined

If that is your problem, then here is what is happening.

The #if directive is used only when first compiling a project. If you compile Test.Nested.dll without NATIVE defined, then the code between #if NATIVE and #endif will not be included in Test.Nested.dll at all. It is as if you compiled this code:

namespace Test.Nested
{
    public class Model1in
    {
    }

    public class Model1Out
    {
    } // I added a missing end brace here
}

Your classes are empty, and Test.Nested.dll has no conversions defined.

If Test.Nested is empty because it wasn't compiled with NATIVE defined, then when you compile Test.Native, you will get an error regardless of whether NATIVE is defined because the conversion block was never included in Test.Nested.dll.

In order to use the conversion blocks, you must define NATIVE when you compile Test.Nested.

Riggy
  • 89
  • 4
  • This is not entirely true. Sometimes there are completely different situations, even with certain constants. A similar question was already in the community, but there was a different essence. See code: https://github.com/ykavalenka/define_test – Y K Feb 15 '20 at 13:59