swagrid
swagrid
CC#
Created by swagrid on 9/24/2023 in #help
✅ Get full paths to executables in %PATH%
Is there an easy way to get the full path of a program that's available in the shell because of the PATH environment variable (like gcm does in powershell or which on unix)? e.g. ResolvePath("dotnet") would return C:\Program Files\dotnet\dotnet.exe I don't want to start a process and I don't want to iterate over PATH myself.
4 replies
CC#
Created by swagrid on 7/10/2023 in #help
✅ Roslyn analyzer: System.Guid only has _dummyPrimitive field?
I'm currently writing an analyzer that inspects the fields of certain types. One of the tests failed because it analyzed System.Guid and I noticed that, according to this query:
ITypeSymbol type = ... // type is Guid
type.GetMembers().OfType<IFieldSymbol>().Where(f => !f.IsStatic)
ITypeSymbol type = ... // type is Guid
type.GetMembers().OfType<IFieldSymbol>().Where(f => !f.IsStatic)
System.Guid only has a single field: int _dummyPrimitive. I know that guids are larger than 4 bytes and given the field name, I assume this is a placeholder of some sort. I could just ignore System.Guid but I suspect this is not the only type that behaves this way. 1. Why is the ITypeSymbol of System.Guid a placeholder (or whatever this is) and not the original definition? 2. Can I somehow detect when a type is a placeholder?
19 replies
CC#
Created by swagrid on 6/18/2023 in #help
❔ Roslyn analyzer test project can't compile `where T : unmanaged` type constraint
I've made a roslyn analyzer for some marshaling stuff. In my test project I try to analyze this source code:
struct FooTest<T> where T : unmanaged
{
T good;
}
struct FooTest<T> where T : unmanaged
{
T good;
}
but when I try to compile this code I get:
error CS0518: Predefined type 'System.Runtime.CompilerServices.IsUnmanagedAttribute' is not defined or imported
Why the error? I know there are similar issues with IsExternalInit, but my test project runs on .net 6.0 and the test code above is compiled with the latest C# version. AFAIK, the unmanaged constrain came with C# 7.3 / .net core 2.0.
25 replies
CC#
Created by swagrid on 6/10/2023 in #help
✅ `DllImport` on a dll name only known at runtime
I have a bunch of native dlls that have the same interface (they export the same functions with identical signatures). My managed library knows at startup which of these dlls it has to load, but it doesn't know it at compile time, so I cannot hardcode the file name like [DllImport("LibA.dll")]. I'm not talking about the dlls being in different directories. All binaries are in the same folder, but I have to selectively load the dll depending on how the managed part is started. I know I could use LoadLibrary and GetProcAdress to import the native dll dynamically, but I'd rather not do all the manual work if I don't have to. Is there a way to load a native dll and map it to a C# class or interface with the native dll only being known at runtime?
15 replies
CC#
Created by swagrid on 6/9/2023 in #help
✅ `checked` on file level
Is there a way to enable checked arithmetic for a whole file?
7 replies
CC#
Created by swagrid on 4/20/2023 in #help
❔ Reference ProjectReference with non-standard file extension
Hey, I have a dotnet solution with two C# projects Foo and FooTest. Foo has to use a different file extension in its output (e.g. Foo.bla instead of Foo.dll), and FooTest references Foo as project reference. Unfortunately, I cannot execute the test project because it tries to load Foo.dll, which obviously doesn't exist. I can work around this issue by just having two copies of the same assembly with two different file extensions, but is there a more elegant way to solve this?
4 replies
CC#
Created by swagrid on 10/24/2022 in #help
.runtimeconfig.json in PackageReference not copied to OutDir
I have a nuget package that contains a managed dll and a corresponding runtimeconfig.json (both in /lib/net6.0). That nuget package is referenced in a C# project. When I build the project only the dll is copied to OutDir, the runtimeconfig.json is missing. What I have to do now is
<ItemGroup>
<PackageReference Include="Bla" Version="1.2.3" GeneratePathProperty="true" />
</ItemGroup>

<ItemGroup>
<None Include="$(PkgBla)/lib/net6.0/Bla.runtimeconfig.json" CopyToOutputDirectory="Always" Visible="false" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Bla" Version="1.2.3" GeneratePathProperty="true" />
</ItemGroup>

<ItemGroup>
<None Include="$(PkgBla)/lib/net6.0/Bla.runtimeconfig.json" CopyToOutputDirectory="Always" Visible="false" />
</ItemGroup>
Is there a better solution to this?
64 replies
CC#
Created by swagrid on 10/20/2022 in #help
Referencing a nuget package without ever restoring it
I have a nuget package A that only includes native binaries (packaged using a .nuspec file). I have a C# project B that depends on A. In the local build environment B uses the binaries of A directly, because I don't have a nuget package at that stage (A is packaged in a CI job later). B is a nuget package itself and I want A to show up as a dependency of B when it's packaged. Usually I'd just add A as a package reference in B.csproj but that doesn't work because A.nupkg doesn't exist yet. I tried to conditionally reference it and only include reference A when packing with dotnet pack B.csproj --no-build -p:ReferenceA in B.csproj:
<PackageReference Include="A" Condition=" $(ReferenceA) == 'true' " />
<PackageReference Include="A" Condition=" $(ReferenceA) == 'true' " />
but because I have the --no-build flag, properties don't seem to be reevaluated and thus the resulting nuget package for B doesn't contain the reference to A. I know that I could use a .nuspec for B as well but then I'd have to list all dependencies manually again, which I don't want. Essentially, I just want to always skip the restore of one specific nuget package (A). Is this somehow possible?
1 replies
CC#
Created by swagrid on 10/10/2022 in #help
Run msbuild target only during dotnet pack
Is it possible to run a msbuild target in a csproj only when it's packaged to a nuget package, i.e. dotnet pack MyProject should cause the target to run while dotnet build MyProject shouldn't.
6 replies
CC#
Created by swagrid on 9/5/2022 in #help
Change output directory of nuget package
Usually, nuget package assemblies and dependencies are copied to OutDir during build, e.g. bin\Debug\net6.0. Is it possible to 1. put all the assemblies and dependencies of a specific nuget package in a different folder (e.g. OutDir/sub) without having to fall back to manually Copy them in a target. 2. configure that output directory form within the nuget package itself (i.e. via some props or targets)
11 replies