RazorSharpFang
RazorSharpFang
CC#
Created by RazorSharpFang on 6/19/2023 in #help
❔ Visual Studio break when Module Loaded
3 replies
CC#
Created by RazorSharpFang on 5/18/2023 in #help
✅ Windows Joystick API
What's the recommended Windows API to use for Joystick/Gamepad input these days if one needs background (non-focused window, problem for Windows.Gaming.Input) input, and one is not willing to take a dependency on the GDK (Read: No GameInput) whilst also being able to read independently the two triggers of the Xbox-style gamepad controllers (Problem for DirectInput) and working with various types of controllers that are NOT XInput-compatible (Like the PS4 controllers, problem for XInput) ?
29 replies
CC#
Created by RazorSharpFang on 12/13/2022 in #help
❔ SQLite in-memory Database read from ReadOnlyMemory bytes
I'm receiving an SQLite database payload from an AMQP-0-9-1 message in the form of ReadOnlyMemory<byte> Rather than writing these bytes to disk and then opening that file with the regular SQLite toolset, I want to be able to work with the sqlite database in a read-only form without having to hit the disk. Is this possible with the currently available SQLite toolset, or is this an impossible task?
75 replies
CC#
Created by RazorSharpFang on 8/31/2022 in #help
Unmanaged struct backed INPC ViewModel, only raising PropertyChanged when backing field(s) changed
So, suppose we have a large unmanaged struct that we obtain from external source, suppose from a network resource. This unmanaged struct decomposes into a set of primitives, with various values.
public struct ExampleStruct
{
public int StructId;
public float StructRealAmount;
public DateTimeOffset StructTimestamp;
}
public struct ExampleStruct
{
public int StructId;
public float StructRealAmount;
public DateTimeOffset StructTimestamp;
}
and then we have our ViewModel class that implements INotifyPropertyChanged. I have made an example below, not using the above struct as a field.
public class ExampleViewModel : INotifyPropertyChanged
{
// INPC boilerplate, to be replaced perhaps with an MVVM toolkit later
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public static bool CheckChange<T>(ref T oldValue, T newValue, [CallerMemberName] string propertyName = "")
{
if (!Equals(oldValue, newValue))
{
oldValue = newValue;
OnPropertyChanged(propertyName);
return true;
}
return false;
}
// end INPC boilerplate

private int _viewModelId;
public int ViewModelId { get => _viewModelId; set => CheckChange(ref _viewModelId, value); }
private float _viewModelRealAmount;
public float ViewModelRealAmount { get => _viewModelRealAmount; set => CheckChange(ref _viewModelRealAmount, value); }
private DateTimeOffset _viewModelTimestamp;
public DateTimeOffset ViewModelTimestamp { get => _viewModelTimestamp; set => CheckChange(ref _viewModelTimestamp, value); }

// TODO: On receive ExampleStruct, set properties to fire events if data has changed
}
public class ExampleViewModel : INotifyPropertyChanged
{
// INPC boilerplate, to be replaced perhaps with an MVVM toolkit later
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public static bool CheckChange<T>(ref T oldValue, T newValue, [CallerMemberName] string propertyName = "")
{
if (!Equals(oldValue, newValue))
{
oldValue = newValue;
OnPropertyChanged(propertyName);
return true;
}
return false;
}
// end INPC boilerplate

private int _viewModelId;
public int ViewModelId { get => _viewModelId; set => CheckChange(ref _viewModelId, value); }
private float _viewModelRealAmount;
public float ViewModelRealAmount { get => _viewModelRealAmount; set => CheckChange(ref _viewModelRealAmount, value); }
private DateTimeOffset _viewModelTimestamp;
public DateTimeOffset ViewModelTimestamp { get => _viewModelTimestamp; set => CheckChange(ref _viewModelTimestamp, value); }

// TODO: On receive ExampleStruct, set properties to fire events if data has changed
}
I had a thought to in the View Model class just have a single field of the unmanaged struct, and then fire off events of property changed. But if I were to do that, how would one determine which properties were changed ? Would it be better to have the unmanaged struct as the sole backing field, or should I not do that?
3 replies
CC#
Created by RazorSharpFang on 8/18/2022 in #help
How do you pass into a method a generic after you've reflected? [Answered]
Suppose I suspect that this IEnumerable (non-generic) is also an IReadOnlyList<T> where T : struct. Unfortunately due to the rules of casting, an IReadOnlyList<T> where T : struct is not castable to an IReadOnlyList<object> How unfortunate! But we have reflection! Suppose I have an IEnumerable called source. I can probe its types as follows
// source is of type System.Collections.IEnumerable
var type = source.GetType();
var interfaces = type.GetInterfaces();
for (int i = 0; i < interfaces.Length; i++)
{
var @interface = interfaces[i];
if (@interface is null)
continue;
// null check satisifed
if(@interface.GenericTypeArguments.Length == 1 && @interface.GetGenericTypeDefinition() == typeof(IReadOnlyList<>))
{
// found it. It is in fact an IReadOnlyList<T> where T : struct
// but now what? Can I get a strongly typed reference to an IReadOnlyList<T> where T : struct ?
}
}
// source is of type System.Collections.IEnumerable
var type = source.GetType();
var interfaces = type.GetInterfaces();
for (int i = 0; i < interfaces.Length; i++)
{
var @interface = interfaces[i];
if (@interface is null)
continue;
// null check satisifed
if(@interface.GenericTypeArguments.Length == 1 && @interface.GetGenericTypeDefinition() == typeof(IReadOnlyList<>))
{
// found it. It is in fact an IReadOnlyList<T> where T : struct
// but now what? Can I get a strongly typed reference to an IReadOnlyList<T> where T : struct ?
}
}
Another class takes in as an argument to its constructor, IReadOnlyList<T> Defined below:
public ReadOnlyListWrapper(IReadOnlyList<T> backingList) { /* BODY HERE */ }
public ReadOnlyListWrapper(IReadOnlyList<T> backingList) { /* BODY HERE */ }
How do I after probing call this constructor now that I know it implements the interface as required?
42 replies