Ruttie
Ruttie
CC#
Created by Ruttie on 10/29/2024 in #help
Try-catch in an iterator
Is it possible to do a try-catch, or alternatively a try-finally over a yield return statement? When I try something like this:
IEnumerator Method1(IDisposable disp) {
try {
yield return Method2();
}
finally {
disp.Dispose();
}
}
IEnumerator Method1(IDisposable disp) {
try {
yield return Method2();
}
finally {
disp.Dispose();
}
}
then the Dispose method is never called.
74 replies
CC#
Created by Ruttie on 10/27/2024 in #help
Conflicting System dlls
I have project that has DisableImplicitFrameworkReferences and NoStandardLib enabled and references custom mscorlib and System.* dlls. However, when I add a package like this:
<PackageReference Include="MonoMod" Version="22.7.31.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.2.0">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="22.7.31.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod" Version="22.7.31.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.2.0">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="22.7.31.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
I get a compile error because the compiler could not choose between the default System.dll and the custom one. How might I fix this issue? Using <ExcludeAssets>runtime</ExcludeAssets> works, but I need the MonoMod.exe created by the MonoMod package... Thanks in advance for any help.
42 replies
CC#
Created by Ruttie on 10/12/2024 in #help
EFCore - When to save changes
I have an application that uses EFCore. In this application, I have a method that gets some values from the database, and may or may not change them based on some conditions. How should I handle the saving of changes in this scenario? I've thought of a few solutions: - Meticulously try to only call SaveChanges if a values was changed (for example by having a local change boolean) - Simply always call SaveChanges at the end of the function - A hybrid approach, call DbContext.ChangeTracker.HasChanges() and only call SaveChanges if this returns true What would be the best approach of these? (alternate approaches are also welcome)
5 replies
CC#
Created by Ruttie on 6/29/2024 in #help
Files not copying to output directory - Linux
I have the following in my csproj:
<PackageReference Include="MonoMod" Version="21.4.29.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour" Version="21.4.29.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="21.4.29.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod" Version="21.4.29.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour" Version="21.4.29.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="21.4.29.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
however, when using <Exec WorkingDirectory="@(BuildDir)" Command="$(Mono) MonoMod.exe TestDll.dll" /> in one of my PostBuild targets, it fails saying that MonoMod.exe does not exist. Why does this happen?
2 replies
CC#
Created by Ruttie on 6/15/2024 in #help
Resx file problems
I'm trying to generate a designer file for my resx resources in vscode on linux, but my Language class is internal instead of public, what is wrong? My code:
<ItemGroup>
<EmbeddedResource Update="Localization/Language.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<!--<LastGenOutput>Language.Designer.cs</LastGenOutput>-->
<StronglyTypedFileName>$(MSBuildProjectDirectory)\Localization\Language.Designer.cs</StronglyTypedFileName>
<StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
<StronglyTypedNamespace>TestProject.Localization</StronglyTypedNamespace>
<StronglyTypedClassName>Language</StronglyTypedClassName>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Localization/Language.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<!--<LastGenOutput>Language.Designer.cs</LastGenOutput>-->
<StronglyTypedFileName>$(MSBuildProjectDirectory)\Localization\Language.Designer.cs</StronglyTypedFileName>
<StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
<StronglyTypedNamespace>TestProject.Localization</StronglyTypedNamespace>
<StronglyTypedClassName>Language</StronglyTypedClassName>
</EmbeddedResource>
</ItemGroup>
1 replies
CC#
Created by Ruttie on 5/25/2024 in #help
Source generators VSCode
I'm using VSCode on linux, but it seems like source generators do not work... In specific, the resx generator. It just does not generate the .Designer file. Is there a way to fix this? Are source generators not supported in VSCode?
4 replies
CC#
Created by Ruttie on 5/17/2024 in #help
duplicate key error efcore
I'm trying to use efcore but I'm getting a duplicate key value violates unique constraint "PK_Tags" error. My model contains:
[Required]
public List<string> Tags { get; set; } = [];
[Required]
public List<string> Tags { get; set; } = [];
however, this is not a key? I'm updating the value by getting it from the database, modifying it, then calling Update with the modified value.
34 replies
CC#
Created by Ruttie on 5/4/2024 in #help
Ensure folder can be read/written to by current user, even if program is admin
I want to ensure that a folder I create is fully accessible by the current user. I'm afraid that if the user runs my program as admin, the folder and its files will be created as the root user, and they will become inaccessible to other programs that are not running as admin/sudo. How can I make sure they can access it?
10 replies
CC#
Created by Ruttie on 10/25/2023 in #help
❔ ThreadStatic for tasks
I need something that acts like ThreadStatic, but for async/await code. In my case, I need to set a context value. For example, in ASP.NET I want to set the HttpContext to have a value I want to access later, but only from this task and its children. How might I do this?
3 replies
CC#
Created by Ruttie on 3/27/2023 in #help
❔ Better user experience inputting commands
How would I get input from a user, the fancy way? What I mean is that for example when a user types 'could you run {appname}?' that it runs the application. I want a project of mine to be like talking to the program instead of executing commands
6 replies
CC#
Created by Ruttie on 3/4/2023 in #help
❔ Analyzer trouble
This is my first time creating a C# analyzer, and I had the following question: I want to prevent users from creating and executing code in new threads in an event handler of an event that has a SyncOnly attribute. Does anyone know how I might do this?
4 replies
CC#
Created by Ruttie on 12/30/2022 in #help
✅ EntityFrameworkCore lists
This has probably been asked before, but is it possible to store a List<T> in a npsql database using EntityFrameworkCore? If so, how would I go about it? (For context, I have a DbSet<Guild>, and I want the model to store data on guilds, and one of the things I want to store is a collection of tags)
4 replies
CC#
Created by Ruttie on 12/28/2022 in #help
❔ CSProj DefineConstants
I have the following in my csproj:
<PropertyGroup>
...
<Configurations>Debug;Release;WIN-STEAM;MAC-STEAM;LIN-STEAM;WIN-GOG;MAC-GOG;LIN-GOG</Configurations>
</PropertyGroup>
<PropertyGroup>
<DefineConstants Condition="'$(Configuration)' == 'WIN-STEAM'">$(DefineConstants);WINDOWS;STEAM</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'MAC-STEAM'">$(DefineConstants);MACOS;STEAM</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'LIN-STEAM'">$(DefineConstants);LINUX;STEAM</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'WIN-GOG'">$(DefineConstants);WINDOWS;GOG</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'MAC-GOG'">$(DefineConstants);MACOS;GOG</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'LIN-GOG'">$(DefineConstants);LINUX;GOG</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'Debug'">$(DefineConstants);WINDOWS;STEAM</DefineConstants>
</PropertyGroup>
<PropertyGroup>
...
<Configurations>Debug;Release;WIN-STEAM;MAC-STEAM;LIN-STEAM;WIN-GOG;MAC-GOG;LIN-GOG</Configurations>
</PropertyGroup>
<PropertyGroup>
<DefineConstants Condition="'$(Configuration)' == 'WIN-STEAM'">$(DefineConstants);WINDOWS;STEAM</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'MAC-STEAM'">$(DefineConstants);MACOS;STEAM</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'LIN-STEAM'">$(DefineConstants);LINUX;STEAM</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'WIN-GOG'">$(DefineConstants);WINDOWS;GOG</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'MAC-GOG'">$(DefineConstants);MACOS;GOG</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'LIN-GOG'">$(DefineConstants);LINUX;GOG</DefineConstants>
<DefineConstants Condition="'$(Configuration)' == 'Debug'">$(DefineConstants);WINDOWS;STEAM</DefineConstants>
</PropertyGroup>
However, when I add the following code:
#if MACOS && STEAM
Console.WriteLine("Mac");
#endif
#if MACOS && STEAM
Console.WriteLine("Mac");
#endif
And set the configuration to MAC-STEAM (vs), the code does not get compiled into the project. Any idea what I'm doing wrong?
2 replies
CC#
Created by Ruttie on 12/25/2022 in #help
✅ Source Generator, dynamic assembly not added.
I have the following code:
var assemblyName = new AssemblyName("MyAssembly");
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");

var typeBuilder = moduleBuilder.DefineType("MyNamespace.MyType", TypeAttributes.Public);
var methodBuilder = typeBuilder.DefineMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Static);
var ilGenerator = methodBuilder.GetILGenerator();

// Generate some IL code
ilGenerator.Emit(OpCodes.Ldstr, "Hello, world!");
ilGenerator.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }));
ilGenerator.Emit(OpCodes.Ret);

typeBuilder.CreateTypeInfo();
var sourceText = "namespace MyNamespace { public static class MyClass { public static void MyMethod() { MyType.MyMethod(); } } }";
context.AddSource("MyFile.cs", sourceText);
var assemblyName = new AssemblyName("MyAssembly");
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");

var typeBuilder = moduleBuilder.DefineType("MyNamespace.MyType", TypeAttributes.Public);
var methodBuilder = typeBuilder.DefineMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Static);
var ilGenerator = methodBuilder.GetILGenerator();

// Generate some IL code
ilGenerator.Emit(OpCodes.Ldstr, "Hello, world!");
ilGenerator.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }));
ilGenerator.Emit(OpCodes.Ret);

typeBuilder.CreateTypeInfo();
var sourceText = "namespace MyNamespace { public static class MyClass { public static void MyMethod() { MyType.MyMethod(); } } }";
context.AddSource("MyFile.cs", sourceText);
When trying to compile a project that references the source generator, I get a CS0103, saying "The name 'MyType' does not exist in the current context". Any ideas on how to fix?
11 replies
CC#
Created by Ruttie on 12/23/2022 in #help
✅ VerificationException
20 replies
CC#
Created by Ruttie on 12/23/2022 in #help
✅ Missing compiler required member, .NET 4.7.2
37 replies
CC#
Created by Ruttie on 12/20/2022 in #help
✅ Blazor as desktop
This might be a weird question, but I'm curious: Is there a way to make a cross-platform blazor desktop app? It would be like avalonia or maui, but the UI would be made in blazor.
16 replies
CC#
Created by Ruttie on 12/17/2022 in #help
✅ `??=` and `??`
This question may not fit here because it is not a question about C# but more about why it is implemented in such a way, but I'll ask anyways: Why is item ??= item2 implemented as
if ((object)item == null)
{
item = item2;
}
if ((object)item == null)
{
item = item2;
}
instead of
if (item == null)
{
item = item2;
}
if (item == null)
{
item = item2;
}
? The cast makes it so if the == operator is changed by the class, it will not use said operator, instead using the default == operator of object. This makes it so that in unity for example, where the == operator is changed by all unity objects, the ??= operator cannot be used. My other question is, does the ?? operator also cast to object before null checking, or does it use something else completely, like pattern matching? I've used https://sharplab.io to see the lowering of ??, but it doesn't appear to be lowered.
66 replies
CC#
Created by Ruttie on 11/20/2022 in #help
❔ Making two dlls interact
This is a bit of a weird question, but... I have a .net 4.7.2 dll which is loaded by a different program. I also have a .net 6/.net 7 executable that needs to be able to call functions and such from said .net 4.7.2 dll. Is there any way to achieve this? I can give more information if needed.
2 replies
CC#
Created by Ruttie on 11/18/2022 in #help
✅ Weird InvalidDataException in switch statement
3 replies