Skip to content

Commit

Permalink
Add DLLIMPORTGENERATOR_ENABLED when DllImportGenerator package is ref…
Browse files Browse the repository at this point in the history
…erenced (#1050)
  • Loading branch information
elinor-fung authored Apr 30, 2021
1 parent 4a232c1 commit e111609
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ partial class Test
using System.Runtime.InteropServices;
partial class Test
{{
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(""DoesNotExist"")]
public static partial int {{|CS8795:Method|}}(out int ret);
#else
Expand Down Expand Up @@ -74,7 +74,7 @@ partial class Test
partial class Test
{{
// P/Invoke
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(/*name*/""DoesNotExist"")] // comment
public static partial int {{|CS8795:Method1|}}(out int ret);
#else
Expand All @@ -83,7 +83,7 @@ partial class Test
#endif
/** P/Invoke **/
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(""DoesNotExist"") /*name*/]
// < ... >
public static partial int {{|CS8795:Method2|}}(out int ret);
Expand Down Expand Up @@ -135,15 +135,15 @@ partial class Test
using System.Runtime.InteropServices;
partial class Test
{{
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[System.ComponentModel.Description(""Test""), GeneratedDllImport(""DoesNotExist"")]
public static partial int {{|CS8795:Method1|}}(out int ret);
#else
[System.ComponentModel.Description(""Test""), DllImport(""DoesNotExist"")]
public static extern int Method1(out int ret);
#endif
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[System.ComponentModel.Description(""Test"")]
[GeneratedDllImport(""DoesNotExist"")]
[return: MarshalAs(UnmanagedType.I4)]
Expand Down Expand Up @@ -194,15 +194,15 @@ partial class Test
using System.Runtime.InteropServices;
partial class Test
{{
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(""DoesNotExist"", EntryPoint = ""Entry"")]
public static partial int {{|CS8795:Method1|}}(out int ret);
#else
[DllImport(""DoesNotExist"", EntryPoint = ""Entry"")]
public static extern int Method1(out int ret);
#endif
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(""DoesNotExist"", EntryPoint = ""Entry"", CharSet = CharSet.Unicode)]
public static partial int {{|CS8795:Method2|}}(out int ret);
#else
Expand Down Expand Up @@ -246,15 +246,15 @@ partial class Test
using System.Runtime.InteropServices;
partial class Test
{{
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(""DoesNotExist"", EntryPoint = ""Entry"")]
public static partial int {{|CS8795:Method1|}}(out int ret);
#else
[DllImport(""DoesNotExist"", BestFitMapping = false, EntryPoint = ""Entry"")]
public static extern int Method1(out int ret);
#endif
#if NET
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(""DoesNotExist"")]
public static partial int {{|CS8795:Method2|}}(out int ret);
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Test()
}

protected override ParseOptions CreateParseOptions()
=> ((CSharpParseOptions)base.CreateParseOptions()).WithPreprocessorSymbols("NET");
=> ((CSharpParseOptions)base.CreateParseOptions()).WithPreprocessorSymbols("DLLIMPORTGENERATOR_ENABLED");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ private async Task<Document> ConvertToGeneratedDllImport(
}
else
{
// #if NET
// #if DLLIMPORTGENERATOR_ENABLED
generatedDeclaration = generatedDeclaration.WithLeadingTrivia(
generatedDeclaration.GetLeadingTrivia()
.AddRange(new[] {
SyntaxFactory.Trivia(SyntaxFactory.IfDirectiveTrivia(SyntaxFactory.IdentifierName("NET"), isActive: true, branchTaken: true, conditionValue: true)),
SyntaxFactory.Trivia(SyntaxFactory.IfDirectiveTrivia(SyntaxFactory.IdentifierName("DLLIMPORTGENERATOR_ENABLED"), isActive: true, branchTaken: true, conditionValue: true)),
SyntaxFactory.ElasticMarker
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static bool IsSupportedTargetFramework(Compilation compilation, out Vers
// .NET Standard
"netstandard" => false,
// .NET Core (when version < 5.0) or .NET
"System.Runtime" => version >= MinimumSupportedFrameworkVersion,
"System.Runtime" or "System.Private.CoreLib" => version >= MinimumSupportedFrameworkVersion,
_ => false,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
-->
<CompilerVisibleProperty Include="DllImportGenerator_GenerateForwarders" />
</ItemGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);DLLIMPORTGENERATOR_ENABLED</DefineConstants>
</PropertyGroup>
</Project>

0 comments on commit e111609

Please sign in to comment.