4

I have a library targeting .NET Core and net45 at some point on my code I need to use reflection like this:

var type = obj.GetType();
var properties = type.GetProperties().ToList();

if (type.IsPrimitive || type.IsEnum || properties.Count == 0)
    return new Dictionary<string, object> { { unnamed, obj } };

Now I am porting my library to also support .net core so I made a new .net core library project, this is my project.json

{
  "version": "1.0.0",
  "dependencies": {
    "Wen.Logging.Abstractions": "1.0.0"
  },
  "frameworks": {
    "net45": {
      "frameworkAssemblies": {
        "System.Reflection": "4.0.0.0"
      },
      "dependencies": {
        "Newtonsoft.Json": "6.0.4",
        "NLog": "4.3.5"
      }
    },
    "netstandard1.6": {
      "imports": "dnxcore50",
      "dependencies": {
        "NETStandard.Library": "1.6.0",
        "System.Reflection.TypeExtensions": "4.1.0",
        "Newtonsoft.Json": "8.0.2",
        "NLog": "4.4.0-*"
      }
    }
  }
}

Added my classes and the compiler complains about the type.IsPrimitive and types.IsEnum properties so I thought to use compiler directives to do something like this:

var type = obj.GetType();
var properties = type.GetProperties().ToList();

#if net45    

if (type.IsPrimitive || type.IsEnum || properties.Count == 0)
    return new Dictionary<string, object> { { unnamed, obj } };

#elif netstandard16

//... some code

#endif

Once I do this the code inside the net45 is grayed out (I think because VS is seeing it as .net core) but then no matter what tag I set between the #elif and #endif is also grayed. I have also tried with #else and then I can do:

var typeInfo = type.GetTypeInfo();
if(typeInfo.IsPrimitive || typeInfo.IsEnum || properties.Count == 0)
    return new Dictionary<string, object> { { unnamed, obj } };

However my question remains:

What tags should I use on my code on the compiler directives #if to target several frameworks on the same project/library?

TTT
  • 1,848
  • 2
  • 30
  • 60
Luiso
  • 4,173
  • 2
  • 37
  • 60
  • @JonSkeet the post was created one year ago, so such attempt was acceptable. I agree today nobody should go that way any more. – Lex Li Sep 15 '17 at 19:00
  • @LexLi: Good point - I only saw it because it was edited 3 hours ago. Will remove my comment... – Jon Skeet Sep 15 '17 at 19:04

1 Answers1

0

While doing some research on .net core I came across this question and in the accepted answer there is something in the project.json file code that caught my eye, here is a snippet:

"frameworks": {
"net46": {
  "buildOptions": {
    "define": [ "NET46" ]
  }
},

as you can see inside the buildOptions there is a define: [ NET46 ] which is the tag you can use along the #if and #elif directives. So even if I don't know (or there isn't) a way to refer to those versions of the framework I could use my own.

Community
  • 1
  • 1
Luiso
  • 4,173
  • 2
  • 37
  • 60
  • 1
    Anyone using the latest .NET Core SDK tooling should follow this official article, https://learn.microsoft.com/en-us/dotnet/standard/frameworks, to use builtin defined symbols, instead of defining your own. – Lex Li Sep 15 '17 at 19:02