yeon
yeon
CC#
Created by yeon on 3/28/2025 in #help
✅ Dependency restores to incorrect package
I have a dotnet solution called A.sln which depends on another solution B.sln. Both A.sln and B.sln have some child projects which has some const value differences depending on the platform: * A.Win64.csproj, A.Win32.csproj, A.Posix64.csproj, A.Posix32.csproj * B.Win64.csproj, B.Win32.csproj, B.Posix64.csproj, B.Posix32.csproj For each of A's sub projects, it should depend on the right version of the B's subproject: * A.Win64.csproj -> B.Win64.csproj * A.Win32.csproj -> B.Win32.csproj * A.Posix64.csproj -> B.Posix64.csproj * A.Posix32.csproj -> B.Posix32.csproj But if I build the A.sln with dotnet build, the dependency is restored to only one version of B's subproject, for example: * A.Win64.csproj -> B.Posix64.csproj * A.Win32.csproj -> B.Posix64.csproj * A.Posix64.csproj -> B.Posix64.csproj * A.Posix32.csproj -> B.Posix64.csproj To avoid this issue, I had to manually iterate through all the sub projects like dotnet build A.Win64.csproj, dotnet build A.Win32.csproj, dotnet build A.Posix64.csproj, dotnet build A.Posix32.csproj. Is there any settings I can tweak to avoid this pitfall? Or should I file an issue about this?
136 replies
CC#
Created by yeon on 3/26/2025 in #help
How can I enforce `[StructLayout(LayoutKind.Sequential)]` metadata to be included in nuget package?
Background I have a C# library 'A' which is essentially a P/Invoke binding layer of a C++ library 'a'. And I have another C# library 'B' which depends on 'A' to add more P/Invoke functions to it. Some P/Invoke function parameters in 'B' uses the structs from 'A'. But it seems that the attribute [StructLayout(LayoutKind.Sequential)] is missing when I build a nuget package of 'A', which results in compilation error SYSLIB1051 on the 'B' side:
Runtime marshalling must be discarded in this project by applying the 'System.Runtime.CompilerService.DisableRuntimeMarshallingAttribute' to the assembly to enable marshalling this type. The generated source will not handle marshalling of parameter 'myParam'.
Is there any way to enforce [StructLayout(LayoutKind.Sequential)] to be included in the metadata of nuget package of 'A'?
16 replies
CC#
Created by yeon on 3/20/2025 in #help
Creating a method that takes a generic method and its variadic parameters as arguments and call it i
I need a way to pass generic static methods and its argument to another method, and the static method inside this another method. In C++, I can easily achieve this with variadic templates and std::forward:
#include <utility>
#include <cstdio>

template <typename Func, typename... Args>
void actual_call_func(Func func, Args &&...args) {
func(std::forward<Args>(args)...);
}

int main() {
auto func = std::printf;
constexpr const char* param_0 = "%d + %d = %d";
int param_1 = 3;
int param_2 = 4;
int param_3 = 3 + 4;

actual_call_func(func, param_0, param_1, param_2, param_3);
}
#include <utility>
#include <cstdio>

template <typename Func, typename... Args>
void actual_call_func(Func func, Args &&...args) {
func(std::forward<Args>(args)...);
}

int main() {
auto func = std::printf;
constexpr const char* param_0 = "%d + %d = %d";
int param_1 = 3;
int param_2 = 4;
int param_3 = 3 + 4;

actual_call_func(func, param_0, param_1, param_2, param_3);
}
But I don't know how to do this easily in C#; Previously I just captured all the required arguments into a lambda, which calls the static method inside of it, and passed it as an single Action.
// Let's just pass this to the actual call function
Action call = () => { Console.WriteLine(param0, param1); };
// Let's just pass this to the actual call function
Action call = () => { Console.WriteLine(param0, param1); };
But this breaks when I have non-capturable arguments such as Span<T>. And I don't want to use params object[]? to box the arguments. The only way I can think of is declaring the ActualCallFunc one by one:
void ActualCallFunc(Action func);
void ActualCallFunc<T>(Action<T> func, T param0);
void ActualCallFunc<T0, T1>(Action<T0, T1> func, T0 param0, T1 param1);
void ActualCallFunc<T0, T1, T2>(Action<T0, T1, T2> func, T0 param0, T1 param1, T2 param2);
...
void ActualCallFunc(Action func);
void ActualCallFunc<T>(Action<T> func, T param0);
void ActualCallFunc<T0, T1>(Action<T0, T1> func, T0 param0, T1 param1);
void ActualCallFunc<T0, T1, T2>(Action<T0, T1, T2> func, T0 param0, T1 param1, T2 param2);
...
But not only this is cumbersome, it would not work if sometimes the parameter needs to use ref. Is there any nice way to solve this like in C++?
16 replies