Hugh
Hugh
CC#
Created by Hugh on 6/10/2024 in #help
✅ Casting to enum when only having a Type object
Yup - this is exactly what I needed. Thanks!
4 replies
CC#
Created by Hugh on 6/10/2024 in #help
✅ Casting to enum when only having a Type object
aha - amazing - this looks like it's exactly what I was after. I'll give this a go. Thanks @reflectronic
4 replies
CC#
Created by Hugh on 12/15/2023 in #help
Previewing IL generated by ILGenerator
Thanks - interesting. I might keep hold of these links for next time around
10 replies
CC#
Created by Hugh on 12/15/2023 in #help
Previewing IL generated by ILGenerator
(still would love to know how to do the above, though)
10 replies
CC#
Created by Hugh on 12/15/2023 in #help
Previewing IL generated by ILGenerator
Ah, I figured out my actual issue here...
10 replies
CC#
Created by Hugh on 12/15/2023 in #help
Previewing IL generated by ILGenerator
From looking at my il.Emit() calls, it looks like I'm matching what happens from a normally compiled constructor, but it's telling me it's an invalid program
10 replies
CC#
Created by Hugh on 12/15/2023 in #help
Previewing IL generated by ILGenerator
yeah - that's my problem 🙂
10 replies
CC#
Created by Hugh on 12/15/2023 in #help
Previewing IL generated by ILGenerator
If I've dynamically generated IL, can I write it out?
10 replies
CC#
Created by Hugh on 12/15/2023 in #help
Previewing IL generated by ILGenerator
Where would I be able to get hold of the built assembly from for this?
10 replies
CC#
Created by Hugh on 12/14/2023 in #help
✅ Ensure that subclasses all have to override a specific method?
That way I can be confident that if all classes in this tree are either sealed or abstract, all classes that I might want to instance will have the abstract method defined
8 replies
CC#
Created by Hugh on 12/14/2023 in #help
✅ Ensure that subclasses all have to override a specific method?
What I've now done is made sure that all of the non-abstract classes are sealed, and where I was subclassing a class that could also be instanced, I've made a shared abstract base class with a sealed subclass.
8 replies
CC#
Created by Hugh on 12/14/2023 in #help
✅ Ensure that subclasses all have to override a specific method?
The only person I'm trying to protect here is myself - I had accidentally missed a couple of overrides
8 replies
CC#
Created by Hugh on 12/14/2023 in #help
✅ Ensure that subclasses all have to override a specific method?
Thanks - that's useful - I could certainly do that, but it would only be caught at runtime, not at compile time
8 replies
CC#
Created by Hugh on 12/13/2023 in #help
Using ILGenerator to create and populate a List<string>
Looking into it a little further, I believe that the !0 in the parameter type list for Add just refers to it being the first generic type argument (i.e. string). A different IL viewer shows that line as:
IL_00a8: callvirt instance void class [System.Collections]System.Collections.Generic.List`1<string>::Add(!0/*string*/)
IL_00a8: callvirt instance void class [System.Collections]System.Collections.Generic.List`1<string>::Add(!0/*string*/)
3 replies
CC#
Created by Hugh on 12/13/2023 in #help
Using ILGenerator to create and populate a List<string>
I've marked where I think the error is. If I'm creating an empty list, and I skip the code between the set list members comments, it works fine. My feeling is that the issue is something to do with the (!0) at the end. I'm not sure what I would do to make this. I've tried changing the potential issue lines to:
il.EmitCall(OpCodes.Callvirt, addMethod, null);
il.EmitCall(OpCodes.Callvirt, addMethod, null);
which does the same thing as just doing il.Call. I've also tried passing in an array of a single typeof(string) instead of null, but in that situation it says Calling convention must be VarArgs Any idea how I can re-create this IL dynamically?
3 replies
CC#
Created by Hugh on 12/11/2023 in #help
✅ Setting default values on fields when generating runtime type
(these are running in two processes, and I want to be able to only have some types defined in one of the processes, but able to be passed through the other - these two examples are the two ends of the processes)
10 replies
CC#
Created by Hugh on 12/11/2023 in #help
✅ Setting default values on fields when generating runtime type
cool stuff - thanks for the help
10 replies
CC#
Created by Hugh on 12/11/2023 in #help
✅ Setting default values on fields when generating runtime type
This is a simplified version of what I'm actually currently doing, but includes the default values
10 replies
CC#
Created by Hugh on 12/11/2023 in #help
✅ Setting default values on fields when generating runtime type
Ah, no - I have something like this:
public class MyType
{
public string ValueA { get; set; } = "default_a";
public string ValueB { get; set; } = "default_b";
public int ValueC { get; set; } = 5;
}
public class MyType
{
public string ValueA { get; set; } = "default_a";
public string ValueB { get; set; } = "default_b";
public int ValueC { get; set; } = 5;
}
And I want to take typeof(MyType) and be able to turn it into json something like this:
{
"name": "MyType",
"properties": [
{
"name": "ValueA",
"type": "string",
"default": "default_a"
},
{
"name": "ValueB",
"type": "string",
"default": "default_b"
},
{
"name": "ValueC",
"type": "int",
"default": 5
}
]
}
{
"name": "MyType",
"properties": [
{
"name": "ValueA",
"type": "string",
"default": "default_a"
},
{
"name": "ValueB",
"type": "string",
"default": "default_b"
},
{
"name": "ValueC",
"type": "int",
"default": 5
}
]
}
10 replies
CC#
Created by Hugh on 12/11/2023 in #help
✅ Setting default values on fields when generating runtime type
I guess if this is all dealt with in the constructor, that answers my follow-up question, which is how to determine what the default value is in a compiler-time type - I had thought I was doing this a hacky way by creating an instance and then querying it, but I guess that's how this should be done
10 replies