Tinister
Tinister
CC#
Created by Tinister on 6/4/2024 in #help
Reflection with InlineArrayAttribute?
After looking at how the compiler synthesizes converting inline arrays to span, this is what I'm thinking:
if (type.GetCustomAttribute<InlineArrayAttribute>() is { } inlineAttr)
{
Delegate helper = inlineHelper<int, int>;
MethodInfo helperMethod = helper.Method.GetGenericMethodDefinition().MakeGenericMethod(type, fields.Single().FieldType);
return helperMethod.Invoke(helper.Target, [o, inlineAttr.Length]);
}

TArray inlineHelper<TArray, TField>(TArray array, int length)
{
Span<TField> span = MemoryMarshal.CreateSpan(ref Unsafe.As<TArray, TField>(ref array), length);
for (int i; i < length; i++)
span[i] = (TField)visit(span[i]);
return array;
}
if (type.GetCustomAttribute<InlineArrayAttribute>() is { } inlineAttr)
{
Delegate helper = inlineHelper<int, int>;
MethodInfo helperMethod = helper.Method.GetGenericMethodDefinition().MakeGenericMethod(type, fields.Single().FieldType);
return helperMethod.Invoke(helper.Target, [o, inlineAttr.Length]);
}

TArray inlineHelper<TArray, TField>(TArray array, int length)
{
Span<TField> span = MemoryMarshal.CreateSpan(ref Unsafe.As<TArray, TField>(ref array), length);
for (int i; i < length; i++)
span[i] = (TField)visit(span[i]);
return array;
}
20 replies
CC#
Created by Tinister on 6/4/2024 in #help
Reflection with InlineArrayAttribute?
I remain undeterred
20 replies
CC#
Created by Tinister on 6/4/2024 in #help
Reflection with InlineArrayAttribute?
No that's a cast exception
20 replies
CC#
Created by Tinister on 6/4/2024 in #help
Reflection with InlineArrayAttribute?
I use the same utility method to walk List<T> and other sorts of BCL types. I can't add an interface there.
20 replies
CC#
Created by Tinister on 6/4/2024 in #help
Reflection with InlineArrayAttribute?
When I add inline arrays it doesn't walk each item in it
20 replies
CC#
Created by Tinister on 6/4/2024 in #help
Reflection with InlineArrayAttribute?
It's a utility method that walks the object graph using reflection
20 replies
CC#
Created by Tinister on 6/4/2024 in #help
Reflection with InlineArrayAttribute?
If I could cast it I wouldn't be using reflection in the first place.
20 replies
CC#
Created by SeiOkami on 3/15/2024 in #help
✅ How do I replace each character with a character that corresponds to the standard English layout?
I know there's utilities that let you "remap" keys, you could look at how those work but only read those mappings
17 replies
CC#
Created by SeiOkami on 3/15/2024 in #help
✅ How do I replace each character with a character that corresponds to the standard English layout?
This might be a win32 function or possibly registry
17 replies
CC#
Created by SeiOkami on 3/15/2024 in #help
✅ How do I replace each character with a character that corresponds to the standard English layout?
How are the characters related to each other?
17 replies
CC#
Created by Hugh on 12/11/2023 in #help
✅ Setting default values on fields when generating runtime type
Oh, yeah those default values aren't in the metadata. Creating an instance and querying them is likely your best bet.
10 replies
CC#
Created by Hugh on 12/11/2023 in #help
✅ Setting default values on fields when generating runtime type
Sorry, a little confused. You have a Type and want to know how to get a default(T) of that type? Or whether any given value is a default(T)?
10 replies
CC#
Created by Hugh on 12/11/2023 in #help
✅ Setting default values on fields when generating runtime type
You'd have to IL emit a constructor
10 replies
CC#
Created by Cin on 12/6/2023 in #help
MVVM - View model with collection of view models?
Hence adding a shim object (a.k.a. A view model)
17 replies
CC#
Created by Cin on 12/6/2023 in #help
MVVM - View model with collection of view models?
Right, and say there's no IsTrue property and you can't add it either
17 replies
CC#
Created by Cin on 12/6/2023 in #help
MVVM - View model with collection of view models?
What if MyItem is part of your model, perhaps even from an assembly you don't control
17 replies
CC#
Created by Cin on 12/6/2023 in #help
MVVM - View model with collection of view models?
But like, if you want to bind Checkbox.IsChecked for each item, how do you do that other than wrap every item in its own ItemViewModel instance which exposes a boolean for that?
17 replies
CC#
Created by Cin on 12/6/2023 in #help
MVVM - View model with collection of view models?
Sorry I'm a little confused. Yes a custom <DataTemplate DataType="{x:Type local:ItemViewModel}"> in xaml is how I would do it as well
17 replies
CC#
Created by Cin on 12/6/2023 in #help
MVVM - View model with collection of view models?
I don't see a problem with that and I tend to do it myself (e.g. Wanting to bind a list of objects to checkboxes needs some sort of intermediate VM)
17 replies